.Net Unit Testing with Visual Studio 2010

47
UNIT TESTING Using Visual Studio 2010 By Karen Gayda

Transcript of .Net Unit Testing with Visual Studio 2010

Page 1: .Net Unit Testing with Visual Studio 2010

UNIT TESTINGUsing Visual Studio 2010

By Karen Gayda

Page 2: .Net Unit Testing with Visual Studio 2010

Creating the Test Project

The test project is created by selecting “New Project” from the File Menu.

Expand Visual C# from the tree view on the left-hand side of the dialog.

Select “Test” from the list of options. In the center pane, select “Test Project”. Give the project a name and add it to

your application’s solution.

Page 3: .Net Unit Testing with Visual Studio 2010

Creating the Test Project

Page 4: .Net Unit Testing with Visual Studio 2010

The Test Project

Contains special classes designated by the [TestClass] attribute which are intended to run groups of tests.

Generally speaking, each class in the application project will have a corresponding class in the unit test project.

Some classes, such as UI classes, may not be tested by the Unit Test class for UI interactivity. This is tested with a macro-style test class type called a Coded UI Test.

Non-UI methods/properties of a UI class can still be tested with a standard Unit Test class.

Page 5: .Net Unit Testing with Visual Studio 2010

The Test Project

Must have at least one test class in project.

Project may have test data files for use by the unit tests.

Project may have utility/helper classes for common functionality used by test classes and test methods.

Project may use configuration files to setup state or provide environment information to the tests.

Page 6: .Net Unit Testing with Visual Studio 2010

The Test Project

Page 7: .Net Unit Testing with Visual Studio 2010

Structure of a Unit Test Class

Microsoft.VisualStudio.TestTools.UnitTesting namespace: The namespace has many classes to help you with your

unit tests including: Assert classes that you can use to verify conditions in unit

tests Initialization and cleanup attributes to run code before or

after unit tests run to ensure a specific beginning and ending state

The ExpectedException attribute to verify that a certain type of exception is thrown during unit test execution

The TestContext class which stores information that is provided to unit tests such as data connection for data-driven tests and information that is required to run unit tests for ASP.NET Web Services

Page 8: .Net Unit Testing with Visual Studio 2010

Structure of a Unit Test Class

TestClass attribute: When you create a unit test, the TestClass

attribute is included in the test file to indicate that this particular class may contain methods marked with the [TestMethod()] attribute. Without the TestClass attribute, the test methods are ignored.

A test class can inherit methods from another test class that is in the same assembly. This means that you can create test methods in a base test class and then use those methods in derived test classes.

Page 9: .Net Unit Testing with Visual Studio 2010

Structure of a Unit Test Class

TestContext - When you create unit tests, a class private variable called testContextInstance is included for each test class. It is of type TestContext.

The properties of the TestContext class store information about the currently running test class.

Page 10: .Net Unit Testing with Visual Studio 2010

Structure of a Unit Test Class

Page 11: .Net Unit Testing with Visual Studio 2010

Unit Test Class Special Methods

ClassInitialize method Designated by the [ClassInitialize()] attribute. Used to setup anything needed by all the test

methods that will be run in the class. Examples of this would be to create test databases, to create tables, to copy test files, etc.

Takes TestContext variable as parameter. One class initialization method per unit test

class.

Page 12: .Net Unit Testing with Visual Studio 2010

Unit Test Class Special Methods

Page 13: .Net Unit Testing with Visual Studio 2010

Unit Test Class Special Methods

ClassCleanUp method Designated by the [ClassCleanUp()] attribute. Used to cleanup artifacts of the test class for

the test run. Usually used to delete data, files, tables or

databases created by the class. One class cleanup method per test class.

Page 14: .Net Unit Testing with Visual Studio 2010

Unit Test Class Special Methods

Page 15: .Net Unit Testing with Visual Studio 2010

Unit Test Class Special Methods

TestInitialize method Designated by the [TestInitialize()] attribut.e Method that runs before the execution of

every test in the class. Used to manage resources shared by the test

methods in the class. Used to setup test environment to an

initialized/known state prior to a test executing.

One test initialize method per unit test class.

Page 16: .Net Unit Testing with Visual Studio 2010

Unit Test Class Special Methods

TestCleanUp method Designated by the [TestCleanUp()] attribute Method runs after the execution of every test

method in the unit test class. Used to return the test environment to a

known state or clean up the artifacts created by a test.

One test clean up method per unit test class.

Page 17: .Net Unit Testing with Visual Studio 2010

Test Methods

Designated by the [TestMethod] attribute. 1 – n test methods per Unit Test class. 1 method per major application

requirement. 1 method per public

property/method/web method as well (in most cases).

A test method should test a single requirement only.

A test method should be atomic.

Page 18: .Net Unit Testing with Visual Studio 2010

Test Methods

IMPORTANT: Test methods must run independently of one another!!!

Test methods can run in any order, therefore their operation cannot rely on state set by other test methods.

Test methods expecting any state or environment setup should always have that set by ClassInitialze or TestInitialize.

It is not recommended that any state be setup in the test method itself. Avoid doing so whenever possible.

Page 19: .Net Unit Testing with Visual Studio 2010

Test Methods

Test method names should clearly indicate the purpose of the test.

For tests that test a public property/class method/web method, use the name with the word Test appended. An example of this would be “ConnectionStringTest”.

For tests that test a specific requirement, use a clear requirement descriptor with the word Test appended to the end. An example of this would be “AllFilesProcessedTest”.

Page 20: .Net Unit Testing with Visual Studio 2010

Test Methods

Page 21: .Net Unit Testing with Visual Studio 2010

Test Methods

Page 22: .Net Unit Testing with Visual Studio 2010

Test Methods

Tests are deterministic. For a given environment state and known input

value(s), an expected result is the outcome of an assertion.

Hard-coded, expected values are normal/acceptable. Use the Assert class to check for various expected

conditions/state such as equality, inequality, Boolean value, etc.

Use the ExpectedException attribute of the test method to insure that a specific exception is thrown if the application should throw it for expected errors.

If anyAssert method fails within a test, the test result will indicate failure.

Unhandled exceptions are treated as failures

Page 23: .Net Unit Testing with Visual Studio 2010

Test Methods

Test methods can have information about resources they should use passed to them via attributes.

The [DeploymentItem] attribute is used to specify deployment items such as a file or a directory to be used by a specific test.

The [DeploymentItem] can also be applied to an entire test class.

There can be multiple deployment items specified for a method or class.

The [TestProperty] attribute is used to pass general name/value pair data to a test.

Test methods can have multiple test property attributes specified.

Page 24: .Net Unit Testing with Visual Studio 2010

Test Method Comments

Best practice to have a good comment header for each test method to clearly indicate what is being tested.

Should include the following:Purpose: Short description of aspect of application

being tested.Prerequisites: Assumptions being made before test can

run such as a database with certain data exists and is accessible to the test project.

Test Data: Data being used and list of possible values or ranges.

Steps: List of steps in test.Remarks: Any pertinent information about test not

covered elsewhere but important to note.

Page 25: .Net Unit Testing with Visual Studio 2010

Creating Unit Tests

Usually created manually in test class by developer.

Skeleton tests can be generated by Visual Studio for existing code.

Generated tests can be created only for properties/methods in existing code classes or interfaces.

Tests for specific requirements must be coded manually.

Page 26: .Net Unit Testing with Visual Studio 2010

Creating Unit Tests

There are several ways to add tests to a test project.

To add a standard unit test class to your project, go to the “Test” main menu option of the VS 2010 IDE and select “New Test”

Select the “Unit Test” item from the dialog box. (The other test types available in VS 2010 are not covered in this presentation).

Give your test class a descriptive name and click OK.

Page 27: .Net Unit Testing with Visual Studio 2010

Creating Unit Tests

Page 28: .Net Unit Testing with Visual Studio 2010

Creating Unit Tests

Page 29: .Net Unit Testing with Visual Studio 2010

Creating Unit Tests

To add your own unit tests to class, add public void, parameterless methods decorated with the test method attribute.

Any method decorated with the [TestMethod] attribute is considered a test by the unit testing framework if it is contained within a test class.

Uncomment the initialize/cleanup methods in the region “Additional Test Attributes” if you wish to use and customize them for your test class.

Page 30: .Net Unit Testing with Visual Studio 2010

Creating Unit Tests

To generate tests from existing code, use the Unit Test Wizard.

From the “Test” main menu option of the VS 2010 IDE, select “New Test”.

Select the “Unit Test Wizard” option. Select your unit test project from

dropdown list. Click OK to run the wizard.

Page 31: .Net Unit Testing with Visual Studio 2010

Creating Unit Tests

Page 32: .Net Unit Testing with Visual Studio 2010

Creating Unit Tests

Expand the namespaces for your application assembly in the tree view on the wizard.

Expand the classes in the tree view to view the individual proprties/methods of the classes.

Select the classes and/or methods for which you would like to generate unit tests.

Click the settings button if you would like to modify the default code generation settings.

Click the Add Assembly button if you want to generate tests from an additional assembly.

Click OK to generate skeleton tests.

Page 33: .Net Unit Testing with Visual Studio 2010

Creating Unit Tests

Page 34: .Net Unit Testing with Visual Studio 2010

Creating Unit Tests

Additional tests are easily added to existing test class from application project.

Right-clicking from within your application class method gives context menu option to “Create unit test”.

Wizard automatically selects current method in application (where cursor is positioned) and chooses the same target class in the test project as existing test methods for application class.

Page 35: .Net Unit Testing with Visual Studio 2010

Running Unit Tests

The Test List Editor window is the easiest way to manually run tests from the Visual Studio 2010 IDE.

They can also be run from the command line in an automated fashion, from the Microsoft Test Manager application (both not covered in this presentation), from the Test Results window of the VS2010 IDE and from the Test View window of the VS 2010 IDE.

Page 36: .Net Unit Testing with Visual Studio 2010

Running Unit Tests

From the “Test” main menu option of the VS 2010 IDE, select “Windows”

Choose the “Test List Editor” option The test list editor window gives you the

ability to group, run and debug your tests and then view the results of the test run’s execution.

Page 37: .Net Unit Testing with Visual Studio 2010

Running Unit Tests

Page 38: .Net Unit Testing with Visual Studio 2010

Running Unit Tests

Test results are displayed in the Test Results window.

This window is automatically displayed by the Test List Editor when your test run completes

All tests have a green “Passed” icon by them if the tests executed correctly

Any test with a red “Failed” icon should be debugged to determine the reason for failure.

Page 39: .Net Unit Testing with Visual Studio 2010

Running Unit Tests

Page 40: .Net Unit Testing with Visual Studio 2010

Running Unit Tests

Test result window displays a summary of all of the test results.

Clicking an individual result in the results grid will open the results detail window and display more detailed information about the outcome of the selected test.

Test Result Detail window provides detail error information if a test fails.

Page 41: .Net Unit Testing with Visual Studio 2010

Running Unit Tests

Page 42: .Net Unit Testing with Visual Studio 2010

Running Unit Tests

Each test run creates a log of the results of the test run execution.

Visual Studio will save the last 25 test runs by default.

Different test runs can be viewed in the Test Results window.

Test Results window will load most recent run by default.

Select run name from dropdown at top of test results window to view other than most recent test run.

Page 43: .Net Unit Testing with Visual Studio 2010

Regression Testing

Unit tests help with regression testing so that you may determine if new requirements/code changes have broken existing functionality in the code.

The VS 2010 test framework includes a feature called Test Impact Analysis which helps to streamline regression testing.

You can use Test Impact Analysis to record a baseline after initial unit test development.

From then on, Test Impact Analysis will keep track of what tests need to be run after code changes to insure proper regression testing without running all of the tests.

Page 44: .Net Unit Testing with Visual Studio 2010

Summary

Visual Studio 2010 testing framework makes it easy to create and run unit tests for your Visual Studio 2010 projects.

Good unit tests must follow industry standard best practices.

Visual Studio does not impose any restrictions on what you put in your test.

It is incumbent on the unit test creator to write good tests.