Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Run Unit Tests As You Modify Your Code

0.00/5 (No votes)
25 Jun 2016 1  
Run unit tests as you modify your code

Introduction

Tired of save/build/run tests manually? I've written a Powershell script for watching *.cs file changes, rebuild and run unit tests as you go. Since it runs on a Powershell command-line, you can still work on your Visual Studio IDE while building and running the unit tests.

The approch here was to write a powershell script which will watch for *.cs file changes in the solution using a New-Object System.IO.FileSystemWatcher. It will refresh every second and when there is a modification, it will call msbuild for the test project followed by a vstest.console call which will run the tests itself.

Requirements

  • Windows with Powershell
  • At least RemoteSigned permission for executing scripts: Set-ExecutionPolicy RemoteSigned
  • Visual Studio 2012+

Installation and Usage

You can clone this repository wherever you like:

git clone https://github.com/caiohamamura/CSharp-TestWatcher.git

Put C-TestWatcher path into environmental PATH or copy .bat and .ps1 to a folder included into environmental PATH.

The .bat file is configured to run in Visual Studio 2015. If this is not your version, edit watchTest.bat.

REM edit this
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"

In your test file, you have to specify a TestCategory:

[TestMethod, TestCategory("AnyCategory")]
public void TestMethod() ...

Then in a command prompt inside your test project path and run the command:

watchTest {AnyCategory} {TargetSetting [Debug]} {BuildPlatform [AnyCPU]} {TestPlatform [x64]}

How It Works

The Powershell script itself is really simple, the majority of the complexity is for parsing input parameters.

The script works this way:

  1. Parse the input parameters or use default
  2. Create an object $watcher = New-Object System.IO.FileSystemWatcher
  3. Configure the $watcher to watch for changes in ..\*.cs files including subdirectories
  4. Configure the action to run whenether there is a change. The action will:
    1. Build the test project calling msbuild with /p:Configuration=[AnyCPU] and /p:Platform=[x64] switches.
    2. Run the tests calling vstest.console using the DLL compiled in .\bin\[ProjectName].dll with /Platform:[x64] and /TestCaseFilter:TestCategory switches.
  5. Bind the action to file modification events.

MSTest

The MSTest is a MS Visual Studio 2005+ command line tool for building projects, documentation here.

VSTest.console

The VSTest.console is a MS Visual Studio 2012+ command line tool for running test projects, documentation here. It relies on a built DLL test project to run the tests.

You can run all tests, specify which tests will run by calling its name using /Tests:Test1,Test2 switch or filter which tests will run using /TestCaseFilter switch. The TestCaseFilter switch can filter through:

  • TestCategory: Set before method declaration through the attribute [TestCategory("MyCategory")]
  • Priority: Set before method declaration through the attribute [Priority(0)]
  • Name: Providing the full name including the namespace

The documentation also claims that it can use a *.runsettings xml file, but I couldn't find how to create such file properly in the documentation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here