Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

55
Getting to know MSTest With Visual Studio 2010 and Team Foundation Server 2010 Thomas Weller www.thomas-weller.de [email protected] July 2010

description

Intro to the MSTest framework (aka. Visual Studio Unit Testing), some additional tools (e.g. Moq, Moles, White), how this is supported in Visual Studio, and how it integrates into the broader context of the TFS environment.

Transcript of Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Page 1: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Getting to know MSTestWith Visual Studio 2010 and Team Foundation Server 2010

Thomas [email protected] July 2010

Page 2: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Outline

I. Authoring tests (with MSTest)II. Visual Studio 2010III. Team Foundation Server 2010IV. Advanced testing conceptsV. UI Automation

Page 3: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

What‘s in a test ?

• Anatomy of a test class • What makes a good (unit) test...• Test context• Initalize/Cleanup• Attributes:

• [ExpectedException]• [Description] • [TestProperty]• [TestCategory]

Page 4: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Sample „test target“

Page 5: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Anatomy of a test class (I)

Ordinary C# class with attributes:

Containing class: [TestClass] Test: [TestMethod], public void, paramless

Page 6: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Anatomy II: Assert class

To verify a fact:

Throws a special exception to fail a test StringAssert, CollectionAssert …

Page 7: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Anatomy III: AAA structure

General structure of a test method – AAA:o Arrangeo Acto Assert

Page 8: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

What makes a good (unit) test…

verify only one fact

try to isolate code under test

Test documentation, usage example

short – simple – fast (CI) – readable

no dependencies on other tests (random order!)

Page 9: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

TestContext class

run-time environment data (e.g. details, directories…) WriteLine() for additional info in test results:

Page 10: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

TestContext class (II)

simply declare an automatic property:

Page 11: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Inizalization and Cleanup

methods marked with attributes (xxxInitialize/xxxCleanup)

three levels:[TestInitialize][TestCleanup] Before/after each test method

[ClassInitialize][ClassCleanup]

- static - Before/after the first/last test method of a class

[AssemblyInitialize][AssemblyCleanup]

- static - Before/after any test class of the assembly

all methods are optional

Page 12: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

[ExpectedException]

testing, that a certain exception is thrown by the method under test:

counterpart to Assert class, very frequently used also Exception messages, if needed

Page 13: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

[Description]

short summary text (similar to xml comments):

shows up in the Properties window:

Page 14: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

[TestProperty]

arbitrary name/value pairs:

show up in the Properties window:

Page 15: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

[TestCategory]

assign (arbitrary) categories to a test:

filter by category on automated builds:

Page 16: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

[TestCategory]: Usage example

define some constants:

apply to test methods as appropriate:

Page 17: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Visual Studio 2010

• Test run configuration (*.testsettings) • Test lists/metadata (*.vsmdi) • Tool Windows:

• Test View window• Test List Editor window• Test Results window• Test Runs window• Test Results Details window

Page 18: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Test run config (*.testsettings)

set of test run and environment settings etc. (e.g. naming schemes, directories, depl. scripts etc.)

*.testsettings *.xml

Page 19: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Test lists/metadata (*.vsmdi)

*.vsmdi *.xml

organizing / grouping tests with lists e.g. for different test runs

similar use to that of TestCategory attribute edit with Test List Editor window (see below)

Page 20: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Test View window

run, view, group, filter, select tests on different criteria

Page 21: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Test List Editor window manage, create, and organize test lists ( *.vsmdi)

Page 22: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Test Results window

opens automatically when running a test inside VS

Page 23: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Test Results Details window

test details + error information (e.g. Stack trace, logs, console output ...)

Page 24: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Team Foundation Server 2010

• Walkthrough• Creating a Bug work item• Creating the test• Creating a Test Case work item for the Bug• Associating test and work item• Check in

• Tests and Automated Builds

Page 25: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Creating a (Bug) work item (I)

Project Portal (Web):

Visual Studio (Team Explorer):

Page 26: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Creating a (Bug) work item (II)

Page 27: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Writing the test

Page 28: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Create a Test Case for the Bug In Visual Studio (on Bug work item):

Page 29: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Associating test and work item From Test View window:

From work item:

Page 30: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Check in on check-in:

version history:

Page 31: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Tests and Automated Builds

Manual: „Dev build“ UI automation tests ClickOnce

CI run: only fast running tests Project „heartbeat“ Nightly run: longer running tests xml documentation

Page 32: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Some more advanced concepts

• Testing private code: PrivateObject/PrivateType• Testing internal code: InternalsVisibleTo• Mocking• „Moling“• Data-driven tests: xml• Data-driven tests: database

Page 33: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

PrivateObject/PrivateType

wrappers around reflectionMETHOD DESCRIPTION

GetArrayElement Returns the selected item from a private array.

GetField Returns the value of the target field.

GetFieldOrProperty Returns the value of the target field or property.

GetProperty Returns the value of the target.

Invoke Invokes a method, optionally passing arguments.

SetArrayElement Assigns the given value to the indicated array element.

SetField Assigns the supplied object to the target field.

SetFieldOrProperty Assigns the supplied object to the target field or property.

SetProperty Assigns the supplied object to the target property.

PrivateType class static elements

Page 34: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

PrivateObject : Example

method to test:

test method:

Page 35: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

InternalsVisibleTo

better alternative (no reflection)

in AssemblyInfo.cs:

test method:

Page 36: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Mocking

http://code.google.com/p/moq/wiki/QuickStart

Creating dummy objects on the fly

Isolating the tested code from dependencies

Moq

Specify arguments and return values

Verify method calls

http://code.google.com/p/moq/

Other (OS) alternatives: Rhino.Mocks, NMock...

Page 37: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Mocking: Example (I)

Page 38: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Mocking: Example (II)

Page 39: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

„Moling“

Mocking: Virtuals and Interface implementations MS Moles: Replace anything with a delegate

Visual Studio Gallery download: http://visualstudiogallery.msdn.microsoft.com/en-us/b3b41648-1c21-471f-a2b0-f76d8fb932ee

Generates moles-assembly, uses runtime instrumentation (performance!)

Page 40: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

„Moling“: Example (I)

Add New Item ...:

Assembly references:

Page 41: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

„Moling“: Example (II)

Page 42: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

„Moling“: Example (III) method to test:

test method:

Page 43: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Data-driven tests: xml (I)

create an xml file:

Page 44: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Data-driven tests: xml (II)

test method:

data accessible via TestContext

Page 45: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Data-driven tests: xml (III)

Test Results Details window:

Page 46: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Data-driven tests: database (I) create a database/table:

Page 47: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Data-driven tests: database (II)

Properties window (wizard) …

… or manually

Page 48: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Automated UI testing

• The White Framework• The recorder• UI Spy• UI object model• Example (Concurrency)• how to proceed…

Page 49: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

The White Framework

http://white.codeplex.com/

.NET based, OS, by ThoughtWorks (CC.Net) OO wrapper around MS UIAutomation library

Page 50: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

The recorder (alpha)

generates C# sourcecode

Page 51: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

UI Spy

Similar to Spy++ Windows SDK 6.0

http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=4377f86d-c913-4b5c-b87e-ef72e5b4e065

Page 52: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

UI object model (partial)

Page 53: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Example - Concurrency (I)

Page 54: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

Example - Concurrency (II)

Page 55: Introduction to testing with MSTest, Visual Studio, and Team Foundation Server 2010

?