Archive for 2013年8月

I was surprised there is no straight setting to automate unit tests after build for Visual C# projects. (Visual Studio 2010 Professional)

Quick answer: Set the Post-build event command line of the Test Project’s Build Events to the following script:

"$(DevEnvDir)mstest.exe" /runconfig:"$(SolutionDir)\Local.testsetting"s /testcontainer:"$(TargetPath)"

The idea is to use mstest.exe to run the Test Project automatically after build. If any of the unit tests fails, the build fails.


Long answer:

Say we have a simple Visual C# Console Application. The template generates a Program class. We add a member function int Program::Foo(int i), as the following:

public int Foo(int i)
    return i * 2;

We then add unit test(s) for this method, by right clicking on the Foo method, and selecting Create Unit Tests. Accept to create a new Visual C# Test Project.



Edit the unit test as needed.

Edit the Test Project’s Post-build event property: Project-> TestProject1 Properties … –> Build Events, and add the mstest.ext script from the top of this article.

Now press F7 to build the solution. You may find the following output of executing the unit tests.

Starting execution…
  Results               Top Level Tests
  ——-               —————
  Passed                TestProject1.ProgramTest.FooTest
  1/1 test(s) Passed
  Test Run Completed.
    Passed  1
    Total   1
  Results file:  … 

Test Settings: Local

Build succeeded.


Note that, if any of the unit tests fails, the build fails.

Read Full Post »