Your SlideShare is downloading. ×
downloaded here.
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

downloaded here.

465
views

Published on


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
465
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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. 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.
  • 3. Unit Test Characteristics
    • Automatic Requires neither intervention nor manual setup
    • Thorough Covers all important functionality, including protected and private methods. Boundary conditions should be checked.
    • Repeatable Tests run with the same data return the same results.
  • 4. Unit Test Characteristics
    • Independent Each test is independent of the results of previous tests and do not affect later tests.
    • Performant Performance is good enough to support running many tests at one session.
  • 5. Unit 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.
  • 6. Unit 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.
  • 7. Unit Test Lifecycle Text Fixture Test Runner Load the class
    • Inspect metadata to find all tests.
    • Copy all deployment items
    • Invoke assembly initialize method
    • Invoke class initialize method
  • 8. Unit Test Lifecycle Text Fixture Test Runner For each test method create an instance
    • Invoke the test initialize method.
    • Call the test method
    • Invoke the test cleanup method
  • 9. Unit Test Lifecycle Text Fixture Test Runner
    • Invoke the class cleanup method
    • Invoke assembly cleanup method
  • 10. Life 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).
  • 11. Life 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).
  • 12. Life 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.
  • 13. Life 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.
  • 14. Testing 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.
  • 15. Testing 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[] { &quot;C&quot;, &quot;E&quot;, &quot;F&quot; }));
  • 16. Best Practices
    • Use the test context to store information that must be shared by different tests. The Properties property returns an IDictionary object. TotalAgents:1
      • ControllerName:BXLEVARO
      • AgentWeighting:100
      • AgentName:BXLEVARO
      • TestDeploymentDir:C:Documents and Settings ichard.levaroLocal SettingsApplication DataVSEqtDeploymentRootf4e70d35-26e9-4c28-96c9-9a1891bf72bcOut
      • AgentId:1
      • TestLogsDir:C:Documents and Settings ichard.levaroLocal SettingsApplication DataVSEqtDeploymentRootf4e70d35-26e9-4c28-96c9-9a1891bf72bcInBXLEVARO
      • TestDir:C:Documents and Settings ichard.levaroLocal SettingsApplication DataVSEqtDeploymentRootf4e70d35-26e9-4c28-96c9-9a1891bf72bc
      • TestName:AddBookTest
    • To include output in the test details, use the WriteLine method of the test context.
    • Use the initialize and cleanup methods
  • 17. Best 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)
  • 18. Best 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
  • 19. Integration with Team Suite
    • MSTest allows unit tests to be run as part of the build project.
    • Create work items based upon failed results.
  • 20. Documentation
    • 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.UnitTesting
  • 21. There’s More …
    • Data-driven tests and how to create them
    • Code coverage
    • Detailed Team System integration – publishing test results