使用 stijn 的建议,下面是内联任务。请注意,GroupSize 属性让我可以控制每个 Exec 任务包含的项目数。
<UsingTask
TaskName="GenerateTestOptionsCollection"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MicrosoftBuildTasksPath)"
>
<ParameterGroup>
<SolutionDir
ParameterType="System.String"
Required="true"
/>
<GroupSize
ParameterType="System.Int32"
Required="true"
/>
<TestNames
ParameterType="Microsoft.Build.Framework.ITaskItem[]"
Required="true"
/>
<TestOptions
ParameterType="Microsoft.Build.Framework.ITaskItem[]"
Output="true"
/>
</ParameterGroup>
<Task>
<Code
Type="Fragment"
Language="cs"
>
<![CDATA[
var optionGroups = new List<string>();
var optionItems = new List<TaskItem>();
for (var j = 0; j < TestNames.Length; j++)
{
optionGroups.Add("/test:" + TestNames[j].ItemSpec);
if (((j + 1) % GroupSize == 0) || j == (TestNames.Length - 1))
{
var options = string.Join(" ", optionGroups) + " " + "/resultsfile:" + SolutionDir + "\\AnalysisResults\\" + optionItems.Count + ".trx";
optionItems.Add(new TaskItem(options));
optionGroups.Clear();
}
}
TestOptions = optionItems.ToArray();
]]>
</Code>
</Task>
</UsingTask>
使用内联任务:
<ReadLinesFromFile
File="$(SolutionDir)\TestList.txt"
>
<Output
TaskParameter="Lines"
ItemName="TestsFromFile"
/>
</ReadLinesFromFile>
<GenerateTestOptionsCollection
SolutionDir="$(SolutionDir)"
GroupSize="$(GroupSize)"
TestNames="@(TestsFromFile)"
>
<Output ItemName="Commands" TaskParameter="TestOptions"/>
</GenerateTestOptionsCollection>
<Exec
Condition="'@(Tests)' != ''"
IgnoreExitCode="true"
Command='"$(VS100COMNTOOLS)\..\IDE\mstest.exe" /runconfig:$(ParsedTestSettings) /testcontainer:$(ParsedTestContainer) /unique %(Commands.Identity)'
>
<Output TaskParameter="exitcode" ItemName="exitcodes"/>
</Exec>