I was looking for an ability to run automated tests from MSBuild, my demands are:
1) Be able to run tests from many solutions (that contains many projects and many tests).
2) Be able to control the entire test running definition from one file(and not to open each proj file to add a new test every time someone in my team writes one).
3) Be able to separate sets of tests that take more time to run from those that run with no time – usually it will be divided into unit and integration tests.
4) Do all this without using .vsmdi file – because from some reason this file not work properly within TFS source safe, which me and my team are using.
MSBuild have no task for running units. Those are the alternatives I found for units to run from MSBuild.
1) Run the MSTest command directly,by using exec task, like this
<Exec Command='"C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\mstest.exe" /testcontainer:c:\projects\MyTests\Sampe.Tests.dll />
This solution is pretty good if you have one test project to run, if you have more than one you have to specify the "/testcontainer:…" parameter for every project that you want to run. Or add new exec task.
If you have to run it for, let say five solutions, when each one contains more the one test project, you have already ten projects that you have to maintain, and I don’t want to be the one that doing this.
2) Running TestToolsTask task which comes with TFS and can be found in Microsoft.TeamFoundation.Build.targets file.
This one can be used only with .vsmdi files, and this differing number four in my demands.
3) Writing task of my own, and make all my dreams come true. And this is what I did.
The task has all the functionality that MSTest command line tool has.
How to use the task?
Import the task in you .proj file:<UsingTask TaskName="Dobkin.Common.Tasks.MSTest"AssemblyFile="D:\MyFolder\Dobkin.Common.Tasks.dll"/>
And call the task:
<ItemGroup Condition="$(ProjectName) == 'Project1'"> <Tests Include="c:\Project1\Project1.BL.Test.dll"/> <Test Include="c:\Project1\Project1.Dal.Tests.dll"/> </ItemGroup> <ItemGroup Condition="$(ProjectName) == 'Project2'"> <Tests Include="c:\Project2\Project2.BL.Test.dll"/> <Test Include="c:\Project2\Project2.Dal.Tests.dll"/> <Test Include="c:\Project2\Project2.Utils.Tests.dll"/> </ItemGroup>
And that’s it. For now on, every new test project or even a whole new solution, you will add, you have to change only this file (for new solution of course you have to import this file first)
Documentation of how to use the task is included in the attachment.