You are on page 1of 23

MS Test 101

Overview
What is MS Test? What does an MS Test look like? Developing with MS Test Setting up a MS Test project Writing Unit Tests with MS Test Running Tests Demo Unit Testing Strategy and Tactics Benefits of writing automated Tests Building Resilient Tests To Automate or Not to Automate Test Driven Development Conclusion / References / Questions?

What is MS Test?
MS Test is Microsofts collection of class libraries that facilitate automated testing.
This set of class libraries is integrated with Visual Studio 2008: Unit Test File Types Test Project Type Integration with MSBuild, support for targeted environments

Basic functionality and support is included with all versions of Visual Studio 2008
More advanced features such as Web UI testing are supported in Visual Studio for Testers Think record test, the point and click, in your browser

Like all things .NET, an MS Test is just a class


using Microsoft.VisualStudio.TestTools.UnitTesting;

What does a MS Test Look Like?

Developing with MS Test


Setting up a MS Test project Writing Unit Tests with MS Test Running Tests A Few Notes Behind The Scenes Demo

Setting up a MS Test Project


Place tests in a separate project
I usually use something like: AssemblyName.Tests Or AssemblyName.WebService.Tests if tests are only for the webservice

Can have more than one test project per solution Depending on the number of tests, may be a good idea to have 1:1 project to test project ratio.

Use the InternalsVisibleTo assembly attribute


Place in AssemblyInfo.cs to allow your test library to see internal classes/methods.

Setting up a MS Test Project

Writing Unit Tests with MS Test


Write small unit tests that test individual bits of functionality
Features are often interdependent, but try to test the smallest subset possible.

It is acceptable to call other test methods to reuse code


Any failure of an Assert method will cause the current test to fail

Abstract test code wherever possible


Break out duplicate code into private or protected methods. Inherit from abstract classes. Initialize class attributes can be located in abstract base classes

Dont check in code or tests that do not pass


Use the [Ignore()] attribute instead

Writing Unit Tests with MS Test


Make the order of unit test execution independent
Although you can order unit tests (not covered in this presentation) unit tests should be able to be run in any order. For dependent steps, use [TestInitialize] or [ClassInitialize] attributes.

Make the execution of unit tests independent


Do not make the test depend on functionality in the app being tested to run. E.g. Do not use functionality in the app to retrieve some information, then use that information to initiate a test of the desired functionality. Problems in other areas of the app could mask the real source of the issue.

Automated Generation of Unit Tests


Nice Toy, but take care Very generic, and generally do not execute perfectly the first time.

A peek at the subject class

Writing Unit Tests with MS Test

Writing Unit Tests with MS Test

Running Tests with MS Test


The MS Test Toolbar

Running Tests with MS Test


Test Results Window

Running Tests with MS Test


Individual Test Results Window

A few notes behind the scenes


Test Results

Live Demo

Unit Testing Strategy and Tactics


Benefits of writing automated Tests Test Driven Development Building Resilient Tests To Automate or Not to Automate

Benefits of Writing Automated Tests


Known baseline We know the tests passed! Faster verification of functionality More frequent verification of functionality
Can be chained with an automated build for CI

Forces work to be done in smaller increments


Smaller more focused, modular, and decoupled classes

Greater trust in the code for all involved


Fewer code defects, defects are detected faster

Test Driven Development

Add A Test

Refactor code

Watch the new test fail

Watch the new test pass

Write some code

Building Resilient Tests


Inevitably your tests will probably depend on certain data being returned from the database. This data is often an unknown, and can change. Try not to make your tests depend on a single DB value A better approach is to retrieve a collection of results.
Then iterate through the results until you find a match indicating that the code functions properly. Flag a Boolean as true, and Assert this value.

To Automate or not to Automate


The answer should be to automate
Sometimes the answer is not to automate, but a great effort should be made to automate
You have the life of the product to recoup the investment

Once you accept that one test is not automated, it is far too tempting not to automate others.
If all is automated, you have one-click verification of proper functionality

Conclusion / Reference / Questions?


Conclusions
Automated testing saves you time! Automated testing increases the number of times software is tested, increases quality as a result. Automated testing can improve the modularity of an application.

References
Test Driven Development from Wikipedia (Must Read!!!)
http://en.wikipedia.org/wiki/Test-driven_development

Famous Continuous Integration Article by Martin Fowler (Must Read!)


http://martinfowler.com/articles/continuousIntegration.html

Assert Class Members:


http://msdn2.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert_members(VS.80).aspx

Any Questions? Feel free to consult with me on your specific project

You might also like