Title: Unit Testing
 1Unit Testing
- "Beware of bugs in the above code 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  
  2Unit 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.  
  3Unit 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.  
  4Unit 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. 
  5Unit 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. 
  6Unit 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.  
  7Unit Test Lifecycle 
 8Unit Test Lifecycle 
 9Unit Test Lifecycle 
 10Life 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). 
  11Life 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).  
  12Life 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. 
  13Life 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. 
  14Testing 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. 
  15Testing 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 Collectionltstringgt(heade
rLabels)) Digatto_Covering_Dlx_MatrixAccessor 
matrix  new Digatto_Covering_Dlx_MatrixAccessor(_
matrix) matrix.PopulateRow(new 
Collectionltstringgt(new string  "C", "E", "F" 
)) 
 16Best Practices
- Use the test context to store information that 
must be shared by different tests. The Properties 
property returns an IDictionary 
object.TotalAgents1  - ControllerNameBXLEVARO 
 - AgentWeighting100 
 - AgentNameBXLEVARO 
 - TestDeploymentDirC\Documents and 
Settings\richard.levaro\Local Settings\Application
 Data\VSEqtDeploymentRoot\f4e70d35-26e9-4c28-96c9-
9a1891bf72bc\Out  - AgentId1 
 - TestLogsDirC\Documents and Settings\richard.leva
ro\Local Settings\Application Data\VSEqtDeployment
Root\f4e70d35-26e9-4c28-96c9-9a1891bf72bc\In\BXLEV
ARO  - TestDirC\Documents and Settings\richard.levaro\L
ocal Settings\Application Data\VSEqtDeploymentRoot
\f4e70d35-26e9-4c28-96c9-9a1891bf72bc  - TestNameAddBookTest 
 - To include output in the test details, use the 
WriteLine method of the test context.  - Use the initialize and cleanup methods 
 
  17Best Practices
- Dont 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  - Dont test trivia (Properties that dont do 
anything) 
  18Best 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
 
  19Integration with Team Suite
- MSTest allows unit tests to be run as part of the 
build project.  - Create work items based upon failed results.
 
  20Documentation
Microsoft.VisualStudio.TestTools.UnitTesting
- 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 
  21Theres More 
- Data-driven tests and how to create them 
 - Code coverage 
 - Detailed Team System integration  publishing 
test results