Posts Tagged ‘Unit Testing’

How to run unit testing tests from command line?

(Also co-authored on

I googled and found the below helpful references. Currently I want to run all from the command-line (for easy of execution & quickness) in cases:

  1. A specific test (ie. a test written by a method marked [TestMethod()])
  2. All tests in a class
  3. All impacted tests of the current TFS pending change of mine.
  4. All tests
  5. All tests except the ones marked as category [TestCategory(“some-category”)]

I’m not sure how can I write a correct command for my needs above.

If you know know, please share! All helps are welcomed and very much appriciated!


  1. the MSTest.exe
  2. the MSTest.exe’s detailed options
  3. obtaining the result


After a while, I found the below useful tips.

  1. run Visual Studio unit tests by using MSTest.exe, located at %ProgramFiles%\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe in my case.
  2. using /container:Path\To\Your\TestProjectAssembly.dll to indicate where your tests are coded.
  3. using /test:TestFilter to filter the tests to run. Note that this filter is applied to the full test method name (ie. FullNamespace.Classname.MethodName)

Hope this helps you guys.



How to get unit-testing code-coverage run from command line?

For efficiency & quickness, I prefer to run unit test & get the code coverage from the command line – this helps me spare the Visual Studio IDE to do other tasks. For me, running testing related tasks in the IDE is very slow, especially if you are working in a big project.

Here’s how to do it

1) Launch VS command prompt or change to the VS location which contains the tools, e.g. %ProgramFiles%\Microsoft Visual Studio 10.0\Team Tools\Performance Tools.

2) Instrument the product dlls using
vsinstr /coverage “Path\To\You\TestedAssembly.dll”

3) Start coverage monitor by running the command
vsperfcmd /start:coverage /output:”Path\Of\Report.code-coverage”

4) Run your tests which are testing the functionality of Path\To\You\TestedAssembly.dll
(manual or automated, doesnt matter)

5) Stop the coverage monitor by running the command
vsperfcmd /shutdown

Here’s the details

The first main step to collecting code coverage information is to instrument the assemblies that you are interested it. Instrumentation will insert code into the assemblies so that when they are run, code coverage information will be collected. You can instrument both managed and native dll files, exe files and assemblies. The tool that you use to instrument is called VsInstr.exe and it is found in the following directory

Microsoft Visual Studio 8\Team Tools\Performance Tools

Instrumenting the assembly is done by passing the –coverage option to the VsInstr.exe tool:

vsinstr –coverage MyAssembly.exe

When you instrument the assembly you are modifying it permanently, so the VsInstr tool automatically makes a backup of the file you instrument. In this case the backup will be called MyAssembly.orig.exe.

The next step in collecting code coverage information is to make sure that the collection monitor is running so that our collected coverage data gets written somewhere. The coverage monitor is called VsPerfMon.exe and is located in the same location as VsInstr.exe was located. To start it up for code coverage collection just use the following command:

start vsperfmon –coverage –output:mytestrun.coverage

Now the command shell will be running your monitor, waiting for some data to collect and write to the output file. Now is the time where you run your test suites or exercise your instrumented code in whatever manner you want to collect coverage information for. The command window will wait until the instrumented code has exited before it will close down and create the coverage file. Once it has, you will now have a code coverage file called mytestrun.coverage that details what code you exercised in your instrumented assembly while the monitor was running. You can open this file directly from Visual Studio to see a code coverage results window that will give you a breakdown of what percent of the code in your instrumented assembly was covered.


  1. (the idea)
    TN_1214: Gathering code coverage information from the command line
  2. (the details)
    Code coverage of generic unit testing

Unit Testing: An Introduction – Deborah’s Developer MindScape

This is a greate unit testing tutorial I ever found.

Unit Testing: An Introduction – Deborah’s Developer MindScape.

Unit Testing: Exposing Private Members – Deborah’s Developer MindScape

A case of mocking the un-mockable objects, ie. accessing the private members.

Unit Testing: Exposing Private Members – Deborah’s Developer MindScape.

Getting started with Unit Testing with Moq

I note down here how I did get started with Unit Testing which use Moq framework.
Hope it would help you to save your learning-new-tech time!

Unit Test
“The Clean Code Talks — Unit Testing”
Equipping yourself before digging into Unit Testing!


QuickStart – moq – Project Hosting on Google Code
TDD : Introduction to Moq
Beginning Mocking With Moq 3 – Part 1
Introduction to Mocking with Moq (Video)
Moq: Linq, Lambdas and Predicates applied to Mock Objects
Decaying Code: Part 1 – Introduction to Moq

Mocking the Unmockable: Using Microsoft Moles with Gallio

Hope it’s helpful to you!

Questions about Moq:
What is the differences between “Strict” and “Loose” behavior? – Stack Overflow