Testcop - a Resharper plugin to help with unit tests
First things first
- The quickest way to understand what this plugin does is to view the demo:
- The actively maintained download location is here:
TestCop is a resharper plugin
that helps when working with unit tests. This plugin is designed for use with mstest &
but should work with any other unittest framework that requires you to assign a test attribute (e.g.
) 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
(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.
And a number of Resharper highlightings to help with writing and maintaining tests within larger solutions.
- Methods using the test attribute must be public
- The class name within the test file name doesn't exist in a nontest project
- The class name within the test file name exists but is in a different assembly to the one expected
- The class name within the test file name doesn't match the test class
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
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
. Typically your test namespace would end in
4. The file name containing the unit tests must contain the test class name inline with the following rules
- The file named MyClass
Tests.cs would contain the class MyClass
- The file named MyClass
.SomeCategoryTests.cs would contain the class 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
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
Note: TestCop will register
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.
TestCop Installation R#7.1
This version is not maintained and is the last version of TestCop that worked with R#7. Installation is via a simple batch file that copies the DLL to %AppData%\JetBrains\ReSharper\v7.1\Plugins\TestCop if you prefer to manually deploy follow the instructions
How to automatically create nuget package as part of build