downloaded here.


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

downloaded here.

  1. 1. Unit 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
  2. 2. Unit Test – What is it? <ul><li>A unit test is a method used to verify that particular small unit of code (class) is working properly. </li></ul><ul><li>Each test case is separate from the others so that only the functions provided by the class are evaluated. </li></ul>
  3. 3. Unit Test Characteristics <ul><li>Automatic Requires neither intervention nor manual setup </li></ul><ul><li>Thorough Covers all important functionality, including protected and private methods. Boundary conditions should be checked. </li></ul><ul><li>Repeatable Tests run with the same data return the same results. </li></ul>
  4. 4. Unit Test Characteristics <ul><li>Independent Each test is independent of the results of previous tests and do not affect later tests. </li></ul><ul><li>Performant Performance is good enough to support running many tests at one session. </li></ul>
  5. 5. Unit Test Terminology <ul><li>A Test Fixture is a public instance class having the TestClass attribute. </li></ul><ul><li>A Test is a non-static public method returning void, accepting no parameters and having the Test attribute. </li></ul><ul><li>A Test Context is an instance of the TestContext class that persists throughout the life of a test fixture. </li></ul>
  6. 6. Unit Test Terminology <ul><li>The Unit Test Runner is the code that instantiates test fixtures, executes tests within the fixture capturing output and exceptions (assertions). </li></ul><ul><li>The Visual Studio Test Manager manages lists of tests and fixtures to be submitted to the test runner. </li></ul>
  7. 7. Unit Test Lifecycle Text Fixture Test Runner Load the class <ul><li>Inspect metadata to find all tests. </li></ul><ul><li>Copy all deployment items </li></ul><ul><li>Invoke assembly initialize method </li></ul><ul><li>Invoke class initialize method </li></ul>
  8. 8. Unit Test Lifecycle Text Fixture Test Runner For each test method create an instance <ul><li>Invoke the test initialize method. </li></ul><ul><li>Call the test method </li></ul><ul><li>Invoke the test cleanup method </li></ul>
  9. 9. Unit Test Lifecycle Text Fixture Test Runner <ul><li>Invoke the class cleanup method </li></ul><ul><li>Invoke assembly cleanup method </li></ul>
  10. 10. Life Cycle of a Unit Test Fixture <ul><li>An instance of the unit test runner is created (either through VS or MSTest) </li></ul><ul><li>The test runner loads the test fixture and inspects the test attributes using reflection. </li></ul><ul><li>From the test information, a test context instance is created. This instance exists throughout the life of the text fixture. </li></ul><ul><li>The assembly and class initialize methods are called (static methods with AssemblyIntitialize and ClassInitialize attributes). </li></ul>
  11. 11. Life Cycle of a Unit Test Fixture <ul><li>For each test (method) in the fixture, the following RUN procedure occurs: </li></ul><ul><ul><li>An instance of the test fixture is created. </li></ul></ul><ul><ul><li>A copy of the test context is passed to the fixture instance. Instance information is now in the test context. </li></ul></ul><ul><ul><li>The initialize method is called (a method with the TestInitialize attribute). </li></ul></ul><ul><ul><li>The test method is invoked </li></ul></ul><ul><ul><li>The cleanup method is called (a method with the TestCleanup attribute). </li></ul></ul>
  12. 12. Life Cycle of a Unit Test Fixture <ul><li>When all tests have run (or until the runner terminates) </li></ul><ul><ul><li>The class cleanup method (a static method with the ClassCleanup attribute) is called. </li></ul></ul><ul><ul><li>The assembly cleanup method (a static method with the AssemblyCleanup attribute) is called. </li></ul></ul>
  13. 13. Life Cycle of a Unit Test Fixture <ul><li>If the test has a data source associated with it, then the following occurs: </li></ul><ul><ul><li>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. </li></ul></ul><ul><li>If a test throws an exception (via an assertion or otherwise), the test is terminated with a failure. </li></ul>
  14. 14. Testing Non-Public Methods <ul><li>Visual Studio uses reflection to create accessor classes; the file is VSGenCodeAccessors.cs – do not modify! </li></ul><ul><li>It has the same namespace as your test container (test project) </li></ul><ul><li>Modify Visual Studio generated code within your test method for readability. </li></ul>
  15. 15. Testing Non-Public Methods <ul><li>Create a instance of your class using the public constructor. </li></ul><ul><li>Pass the object to accessor constructor. </li></ul><ul><li>Call methods on the returned accessor. </li></ul>_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[] { &quot;C&quot;, &quot;E&quot;, &quot;F&quot; }));
  16. 16. Best Practices <ul><li>Use the test context to store information that must be shared by different tests. The Properties property returns an IDictionary object. TotalAgents:1 </li></ul><ul><ul><li>ControllerName:BXLEVARO </li></ul></ul><ul><ul><li>AgentWeighting:100 </li></ul></ul><ul><ul><li>AgentName:BXLEVARO </li></ul></ul><ul><ul><li>TestDeploymentDir:C:Documents and Settings ichard.levaroLocal SettingsApplication DataVSEqtDeploymentRootf4e70d35-26e9-4c28-96c9-9a1891bf72bcOut </li></ul></ul><ul><ul><li>AgentId:1 </li></ul></ul><ul><ul><li>TestLogsDir:C:Documents and Settings ichard.levaroLocal SettingsApplication DataVSEqtDeploymentRootf4e70d35-26e9-4c28-96c9-9a1891bf72bcInBXLEVARO </li></ul></ul><ul><ul><li>TestDir:C:Documents and Settings ichard.levaroLocal SettingsApplication DataVSEqtDeploymentRootf4e70d35-26e9-4c28-96c9-9a1891bf72bc </li></ul></ul><ul><ul><li>TestName:AddBookTest </li></ul></ul><ul><li>To include output in the test details, use the WriteLine method of the test context. </li></ul><ul><li>Use the initialize and cleanup methods </li></ul>
  17. 17. Best Practices <ul><li>Don’t test the framework (or Hibernate) </li></ul><ul><li>Do test your algorithms; create mock objects if necessary </li></ul><ul><li>Do use unit testing as software scaffolding during development and then migrate to a unit test </li></ul><ul><li>Don’t test trivia (Properties that don’t do anything) </li></ul>
  18. 18. Best Practices <ul><li>Visual Studio places the test project in the same location as the source. </li></ul><ul><li>Create the test project first and then add tests to it. </li></ul><ul><li>Use the “Test Tools” toolbar </li></ul>
  19. 19. Integration with Team Suite <ul><li>MSTest allows unit tests to be run as part of the build project. </li></ul><ul><li>Create work items based upon failed results. </li></ul>
  20. 20. Documentation <ul><li>Contains all documentation for Assert methods </li></ul><ul><li>Attribute documentation </li></ul><ul><li>Found in Team Test API under Team Edition for Testers </li></ul><ul><li>Other namespaces implement load testing, Web testing and others </li></ul>Microsoft.VisualStudio.TestTools.UnitTesting
  21. 21. There’s More … <ul><li>Data-driven tests and how to create them </li></ul><ul><li>Code coverage </li></ul><ul><li>Detailed Team System integration – publishing test results </li></ul>