Testcop - a Resharper plugin to help with unit tests

Effortlessly switch between code and unit test...

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 1 : Multiple test projects identified by namespace

This is the default approach. Where each test project maps to one code project via the namespace. Detail : Each test project maps to a code project via namespace

testProjectPerCodeProjectNS.png

Usage 2 : Multiple test projects identified by project name

Where each test project maps to one code project via the project name. In this configuration the namespace of the test project and associated code project are the same. Detail : Each test project maps to a code project via project name

testProjectPerCodeProjectPrjNaming.png

Usage 3 : Single test project in a solution

A single test project for all code projects within an assembly is supported but configuration is a little more complex. Detail : Single Test Project Within Solution

singleTestProject-MyCorp-MyApp.png

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#9.0

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

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 Feb 11 at 7:22 PM by SomethingUseful, version 99