24
DEV322 Unit Testing Best Practices With Visual Studio 2005 Team System Mark Seemann Senior Consultant Microsoft Consulting Services

DEV322 Unit Testing Best Practices With Visual Studio 2005 Team System Mark Seemann Senior Consultant Microsoft Consulting Services

Embed Size (px)

Citation preview

DEV322Unit Testing Best Practices With Visual Studio 2005 Team System

Mark SeemannSenior ConsultantMicrosoft Consulting Services

Agenda

• What is a unit test?

• When to test?

• Who should test?

• How to test?

• What to test

• Choosing methodologies

• Best practices

Unit Testing Defined

• Tests a single assembly in isolation

• Volatile dependencies must be abstracted away

• Fully automated tests

• Unit tests constitute automated regression tests

• Binary results (pass or fail)

When To Write Unit Tests

• Before writing code

• Test-driven development, agile, XP, etc.

• After code is written

• Quality assurance, bug discovery, regression testing

Writing Unit Tests Before And After Writing Code

Mark Seemann

Senior Consultant

Microsoft Consulting Services

Who Should Write Unit Tests?

• Depends on the purpose of the test:

• Developers write their own tests in test-driven development

• Testers write quality assurance tests

• Developers should not write their own quality assurance tests

How To Write Tests

• What to test

• Choosing methodologies

• Best practices

What To Test

• Unit test all of your own code, except

• User interfaces

• (Web) service façades

• UI and service façades should be subjected to other types of tests

• Test only against your public API

Testing Against Public Only API

Mark Seemann

Senior Consultant

Microsoft Consulting Services

Approaches

• Pragmatic

• Formal

• Approaches can be mixed

Pragmatic Unit Testing

• Usage-oriented

• Can involve sequences of operations

• Suitable for test-driven development

Writing a Pragmatic Unit Test

Mark Seemann

Senior Consultant

Microsoft Consulting Services

Formal Unit Testing

• Systematic

• Atomic

• Check boundary conditions

• Exercise error paths

• Suitable for QA

Atomic Unit Test Structure

1. Build initial state using alternative, low-level APIs if possible

2. Invoke test target member

3. Validate results

Writing a Formal Unit Test

Mark Seemann

Senior Consultant

Microsoft Consulting Services

Best Practices

• Test cases must be independent

• Test cases must be deterministic

• Reproduce bugs as test cases

• Place tests in separate projects

• Have a test project per test target

• Use source control on test projects

Applying Best Practices with Visual Studio 2005 Team System

Mark Seemann

Senior Consultant

Microsoft Consulting Services

Summary

• Unit test all relevant libraries

• Test only public API

• Set high code coverage goal

Resources

• Unit Testing Tips: Write Maintainable Unit Tests That Will Save You Time And Tears (http://msdn.microsoft.com/msdnmag/issues/06/01/UnitTesting)

• http://blogs.msdn.com/ploeh

Ask The ExpertsGet Your Questions Answered

You can find me at the Microsoft Ask the Experts area, located in the Exhibition Hall:

Friday 10 November 10.15 – 10.45

© 2006 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only.MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.