Using maxcpucount to parallelise unit testing in msbuild?
In a particular SOA-styled application I'm working on we have a large number of unit tests in each service. The tests for each service run serially, and I want to test running the tests for each service in parallel.
In our msbuild file is the following:
<Target Name="RunUnitTests">
<ItemGroup Condition="'@(UnitTestsOutput)' == ''">
<UnitTestsOutput Include="$(UnitTestsOutputFolder)\**\*Tests.dll" />
</ItemGroup>
<Exec Command="$(NUnitRunner) /nologo /labels /timeout=30000 @(UnitTestsOutput, ' ')" />
</Target>
So all tests project assemblies end with the name "Tests", thus they get found/picked up with that search. Would it be possible/feasible to do something like this?
<Target Name="RunUnitTests">
<ItemGroup Condition="'@(UnitTestsOutput)' == ''">
<UnitTestsOutputService1 Include="$(UnitTestsOutputFolder)\*Service1*\*Tests.dll" />
<UnitTestsOutputService2 Include="$(UnitTestsOutputFolder)\*Service2*\*Tests.dll" />
<UnitTestsOutputService3 Include="$(UnitTestsOutputFolder)\*Service3*\*Tests.dll" />
<UnitTestsOutputService4 Include="$(UnitTestsOutputFolder)\*Service4*\*Tests.dll" />
<UnitTestsOutputService5 Include="$(UnitTestsOutputFolder)\*Service5*\*Tests.dll" />
</ItemGroup>
<Exec Command="$(NUnitRunner) /nologo /labels /timeout=30000 @(UnitTestsOutputService1, ' ')" />
<Exec Command="$(NUnitRunner) /nologo /labels /timeout=30000 @(UnitTestsOutputService2, ' ')" />
<Exec Command="$(NUnitRunner) /nologo /labels /timeout=30000 @(UnitTestsOutputService3, ' ')" />
<Exec Command="$(NUnitRunner) /nologo /lab开发者_开发技巧els /timeout=30000 @(UnitTestsOutputService4, ' ')" />
<Exec Command="$(NUnitRunner) /nologo /labels /timeout=30000 @(UnitTestsOutputService5, ' ')" />
</Target>
And then if the build is run using /maxcpucount it could possibly parallelise those sets of tests?
You can use BuildInParallel = true and a custom target:
<Project .... DefaultTargets="RunAll" />
...
<Target Name="RunSingle">
<ItemGroup>
<UnitTestsOutputService Include="$(UnitTestsOutputFolder)\*Service$(ServiceNum)*\*Tests.dll" />
</ItemGroup>
<Exec Command="$(NUnitRunner) /nologo /labels /timeout=30000 @(UnitTestsOutputService , ' ')" />
</Target>
<ItemGroup>
<Parallel Include="1;2;3;4;5" />
<ItemGroup>
<Target Name="RunAll">
<ItemGroup>
<Projects Include="$(MSBuildProjectFile)" > <-- for recursive call to same build file -->
<Properties>ServiceNum=%(parallel.identity)</Properties> <-- Service1, Service2 .. -->
</Projects>
</ItemGroup>
<MSBuild Projects="@(Projects)" BuildInParallel="true" Targets="RunSingle" />
</Target>
精彩评论