Testcop - a Resharper plugin to help with unit tests

First things first


Overview

TestCop is a resharper plugin that helps when working with unit tests. This plugin is designed for use with mstest & nunit but should work with any other unittest framework that requires you to assign a test attribute (e.g. TestFixture) to your unit test classes and/or methods. It has a number of resharper inspections that help ensure healthy test projects that follow an approach I've been using for a number of years where each code assembly has an associated test assembly.

Testing and how to write tests are a very personal thing...this is something I've developed in my spare time and found useful enough to share with the wider community...it won't suit every project/solution/individual.

Toggle between code and test

The plugin provides a keyboard shortcut (ctrl-g,ctrl-t) to switch between code and test files (the reason why I originally wrote the plugin). It uses the name of the test file to identify the name of the class under test and when toggling between code and test it can optionally look for usages within the test assembly. If the class file/test is missing it will offer to create it in the correct location. In the screen shot below the file named ClassB.SecurityTests.cs contains the class ClassBSecurityTests.

CreateFilePrompt.png

And a number of Resharper highlightings to help with writing and maintaining tests within larger solutions.

Highlightings

  • Methods using the test attribute must be public
TestMethodsMustBePublicHighlight.png
  • The class name within the test file name doesn't exist in a nontest project
ClassForTestNotFoundHighlight.png
  • The class name within the test file name exists but is in a different assembly to the one expected
ClassUnderTestNotFoundHighlight.png
  • The class name within the test file name doesn't match the test class
FilenameAndClassDifferHighlight.png

Find code that is not part of the project

Usually any code that isn't compiled as part of the project whether an orphaned file or commented code should be deleted. Testcop looks for *.cs files within project folders that aren't part of the project. Highlightings are created against AssemblyInfo.cs.

Rename associated test files when you rename class files

If you keep your tests in the correct folders and named the way TestCop likes them to be (e.g. ClassATests or ClassA.SomethingTests) then Testcop will rename those files when you do a rename refactor on the class (ClassA).

Usage (multiple test projects in a solution)

To be able to use this plugin you need to configure your solutions in the following way..

1. Test classes must end in the name Tests (configurable)
2. Tests are held in assemblies separate to the assembly under test. Each test assembly should correspond to one assembly under test though each code assembly can map to more than one test assembly. e.g. MyOrg.MyProj --> MyOrg.MyProj.Tests & MyOrg.MyProj.IntegrationTests
3. Assemblies containing tests must have a namespace (not necessariliy file name) that matches a testcop configurable RegEx. Typically your test namespace would end in .Tests (configurable)
4. The file name containing the unit tests must contain the test class name inline with the following rules
- The file named MyClassTests.cs would contain the class MyClassTests testing MyClass
- The file named MyClass.SomeCategoryTests.cs would contain the class MyClassSomeCategoryTests testing MyClass (this enables you to split up your unit tests into categories e.g. MyClass.SecurityTests.cs
5. The first part of the test filename (see point 4) must relate to a class within the associated code assembly. This helps ensure renames of code are reflected within the test classes. The only exceptions are BDD style class names which begin typically with Given or When (configurable).

Usage (single test projects in a solution)

A single test project for all code projects within an assembly is supported but configuration is a little more complex. To enable single test project support uncheck the option 'each test project maps to a single code project'
RegEx patterns need to be supplied to
  • Identify the test project and then to map the test namespace to the associated code assembly and to provide the namespace within it. E.g. MyCorp.MyApp.Tests.Infrastructure.NS1.Class1.cs --> to the namespace NS1 within the code project MyCorp.MyApp.Infrastructure.
  • Extract the namespace from the namespace of the code file that will be the subnamespace for the test E.g. MyCorp.MyApp.Infrastructure.NS1.Class1.cs --> to the namespace Infrastructure.NS1 which will be under the single test project of the solution

1. Test classes must end in the name Tests (configurable)
2. Tests are held in a single assembly separate to the code assembly under test. The sub-namespace within the test assembly should correspond to the code assembly under test
3. Assemblies containing tests must have a namespace (not necessariliy file name) that matches a testcop configurable RegEx. Typically your test namespace would end in .Tests (configurable)
4. The file name containing the unit tests must contain the test class name inline with the following rules
- The file named MyClassTests.cs would contain the class MyClassTests testing MyClass
- The file named MyClass.SomeCategoryTests.cs would contain the class MyClassSomeCategoryTests testing MyClass (this enables you to split up your unit tests into categories e.g. MyClass.SecurityTests.cs
5. The first part of the test filename (see point 4) must relate to a class within the associated code assembly. This helps ensure renames of code are reflected within the test classes. The only exceptions are BDD style class names which begin typically with Given or When (configurable).

Keyboard Shortcuts

Note: TestCop will register keyboard shortcut Ctrl-G,Ctrl-T (go test) - you may need to re-assign any conflicting shortcuts.

TestCop Installation R#8.2

Installation is via the Resharper -> Extensions Manager menu item. Very easy to use. http://www.jetbrains.com/resharper/plugins/

Other

How to automatically create nuget package as part of build.

Last edited Wed at 8:07 PM by SomethingUseful, version 77