使用maxcpucount在msbuild中并行化单元测试?

时间:2011-09-28 09:46:44

标签: msbuild nunit

在我正在开发的特定SOA样式的应用程序中,我们在每个服务中都有大量的单元测试。每个服务的测试都是串行运行的,我想测试并行运行每个服务的测试。

在我们的msbuild文件中有以下内容:

<Target Name="RunUnitTests">
    <ItemGroup Condition="'@(UnitTestsOutput)' == ''">
        <UnitTestsOutput Include="$(UnitTestsOutputFolder)\**\*Tests.dll" />
    </ItemGroup>

    <Exec Command="$(NUnitRunner) /nologo /labels /timeout=30000 @(UnitTestsOutput, ' ')" />
</Target>

因此,所有测试项目程序集都以名称“Tests”结束,因此可以通过该搜索找到/拾取它们。 做这样的事情会有可能/可行吗?

<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 /labels /timeout=30000 @(UnitTestsOutputService4, ' ')" />
    <Exec Command="$(NUnitRunner) /nologo /labels /timeout=30000 @(UnitTestsOutputService5, ' ')" />
</Target>

然后如果使用/ maxcpucount运行构建,它可能会将这些测试集并行化吗?

1 个答案:

答案 0 :(得分:1)

您可以使用BuildInParallel = true和自定义目标:

<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>