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 & 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.
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
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 RegEx
. Typically your test namespace would end in .Tests
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
Tests testing MyClass
- The file named MyClass
.SomeCategoryTests.cs would contain the class MyClass
SomeCategoryTests 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
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.1
Installation is via the Resharper -> Extensions Manager menu item. Very easy to use. http://www.jetbrains.com/resharper/plugins/
TestCop Installation R#7.1
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 at http://confluence.jetbrains.net/display/ReSharper/1.5+Deployment
How to automatically create nuget package as part of build