downloaded here
TRANSCRIPT
Unit Testing Redux Slide 1
Unit TestingUnit Testing"Beware of bugs in the above code; "Beware of bugs in the above code;
I have only proved it correct, not tried it."I have only proved it correct, not tried it." – Donald E. Knuth, March 29, 1977 in a note to Peter van Emde Boas titled
Notes on the van Emde Boas construction of priority deques: An instructive use of recursion
Unit Testing Redux Slide 2
Unit Test – What is it?Unit Test – What is it?
A unit test is a method used to verify that particular small unit of code (class) is working properly.
Each test case is separate from the others so that only the functions provided by the class are evaluated.
Unit Testing Redux Slide 3
Unit Test CharacteristicsUnit Test Characteristics
AutomaticRequires neither intervention nor manual setup
ThoroughCovers all important functionality, including protected and private methods. Boundary conditions should be checked.
RepeatableTests run with the same data return the same results.
Unit Testing Redux Slide 4
Unit Test CharacteristicsUnit Test Characteristics
IndependentEach test is independent of the results of previous tests and do not affect later tests.
PerformantPerformance is good enough to support running many tests at one session.
Unit Testing Redux Slide 5
Unit Test TerminologyUnit Test Terminology
A Test Fixture is a public instance class having the TestClass attribute.
A Test is a non-static public method returning void, accepting no parameters and having the Test attribute.
A Test Context is an instance of the TestContext class that persists throughout the life of a test fixture.
Unit Testing Redux Slide 6
Unit Test TerminologyUnit Test Terminology
The Unit Test Runner is the code that instantiates test fixtures, executes tests within the fixture capturing output and exceptions (assertions).
The Visual Studio Test Manager manages lists of tests and fixtures to be submitted to the test runner.
Unit Testing Redux Slide 7
Unit Test LifecycleUnit Test Lifecycle
Text FixtureTest RunnerTest RunnerLoad the classLoad the class
•Inspect metadata to find all tests.Inspect metadata to find all tests.•Copy all deployment itemsCopy all deployment items•Invoke assembly initialize methodInvoke assembly initialize method•Invoke class initialize method Invoke class initialize method
Unit Testing Redux Slide 8
Unit Test LifecycleUnit Test Lifecycle
Text FixtureTest RunnerTest Runner
For each test For each test method create an method create an instanceinstance
•Invoke the test initialize method.Invoke the test initialize method.•Call the test methodCall the test method•Invoke the test cleanup methodInvoke the test cleanup method
Unit Testing Redux Slide 9
Unit Test LifecycleUnit Test Lifecycle
Text FixtureTest RunnerTest Runner
•Invoke the class cleanup methodInvoke the class cleanup method•Invoke assembly cleanup methodInvoke assembly cleanup method
Unit Testing Redux Slide 10
Life Cycle of a Unit Test FixtureLife Cycle of a Unit Test Fixture
An instance of the unit test runner is created (either through VS or MSTest)
The test runner loads the test fixture and inspects the test attributes using reflection.
From the test information, a test context instance is created. This instance exists throughout the life of the text fixture.
The assembly and class initialize methods are called (static methods with AssemblyIntitialize and ClassInitialize attributes).
Unit Testing Redux Slide 11
Life Cycle of a Unit Test FixtureLife Cycle of a Unit Test Fixture
For each test (method) in the fixture, the following RUN procedure occurs: An instance of the test fixture is created. A copy of the test context is passed to the fixture
instance. Instance information is now in the test context.
The initialize method is called (a method with the TestInitialize attribute).
The test method is invoked The cleanup method is called (a method with the
TestCleanup attribute).
Unit Testing Redux Slide 12
Life Cycle of a Unit Test FixtureLife Cycle of a Unit Test Fixture
When all tests have run (or until the runner terminates) The class cleanup method (a static method with the
ClassCleanup attribute) is called. The assembly cleanup method (a static method with
the AssemblyCleanup attribute) is called.
Unit Testing Redux Slide 13
Life Cycle of a Unit Test FixtureLife Cycle of a Unit Test Fixture
If the test has a data source associated with it, then the following occurs: While there are more rows in the data source,
populate the DataRow property with the next row in the data source and then execute the RUN procedure.
If a test throws an exception (via an assertion or otherwise), the test is terminated with a failure.
Unit Testing Redux Slide 14
Testing Non-Public MethodsTesting Non-Public Methods
Visual Studio uses reflection to create accessor classes; the file is VSGenCodeAccessors.cs – do not modify!
It has the same namespace as your test container (test project)
Modify Visual Studio generated code within your test method for readability.
Unit Testing Redux Slide 15
Testing Non-Public MethodsTesting Non-Public Methods
Create a instance of your class using the public constructor.
Pass the object to accessor constructor. Call methods on the returned accessor. _matrix = new Matrix(new Collection<string>(headerLabels)); Digatto_Covering_Dlx_MatrixAccessor matrix = new Digatto_Covering_Dlx_MatrixAccessor(_matrix); matrix.PopulateRow(new Collection<string>(new string[] { "C", "E", "F" }));
Unit Testing Redux Slide 16
Best PracticesBest Practices Use the test context to store information that
must be shared by different tests. The Properties property returns an IDictionary object.TotalAgents:1ControllerName:BXLEVAROAgentWeighting:100AgentName:BXLEVAROTestDeploymentDir:C:\Documents and Settings\richard.levaro\Local Settings\Application Data\
VSEqtDeploymentRoot\f4e70d35-26e9-4c28-96c9-9a1891bf72bc\OutAgentId:1TestLogsDir:C:\Documents and Settings\richard.levaro\Local Settings\Application Data\
VSEqtDeploymentRoot\f4e70d35-26e9-4c28-96c9-9a1891bf72bc\In\BXLEVAROTestDir:C:\Documents and Settings\richard.levaro\Local Settings\Application Data\
VSEqtDeploymentRoot\f4e70d35-26e9-4c28-96c9-9a1891bf72bcTestName:AddBookTest
To include output in the test details, use the WriteLine method of the test context.
Use the initialize and cleanup methods
Unit Testing Redux Slide 17
Best PracticesBest Practices
Don’t test the framework (or Hibernate) Do test your algorithms; create mock
objects if necessary Do use unit testing as software scaffolding
during development and then migrate to a unit test
Don’t test trivia (Properties that don’t do anything)
Unit Testing Redux Slide 18
Best PracticesBest Practices
Visual Studio places the test project in the same location as the source.
Create the test project first and then add tests to it.
Use the “Test Tools” toolbar
Unit Testing Redux Slide 19
Integration with Team SuiteIntegration with Team Suite
MSTest allows unit tests to be run as part of the build project.
Create work items based upon failed results.
Unit Testing Redux Slide 20
DocumentationDocumentation
Contains all documentation for Assert methods
Attribute documentation Found in Team Test API under Team
Edition for Testers Other namespaces implement load
testing, Web testing and others
Microsoft.VisualStudio.TestTools.UnitTestingMicrosoft.VisualStudio.TestTools.UnitTesting
Unit Testing Redux Slide 21
There’s More …There’s More …
Data-driven tests and how to create them Code coverage Detailed Team System integration –
publishing test results