我在MSBuild脚本中创建的项目组的作用域有一些问题。基本上,我想要做的是有两个不同的目标-让我们称它们为RunUnitTests和RunIntegrationTests -它们生成一个名为TestAssemblies的项组,然后调用RunTests,它使用TestAssemblies来确定从哪个程序集运行测试。
单元测试和集成测试的两个不同的目标都依赖于构建目标,并从那里获得一个包含所有已编译程序集的项组,但由于RunTests目标将从不同的位置调用,因此它不能真正依赖于其中任何一个。因此,我需要以某种方式将项目组传递给通用的testrunner目标。然而,这似乎是不可能的,因为对目标内的项目组的更改似乎被限定为仅在该目标内工作。
我见过these posts,但他们似乎只是证实了我的恐惧,并建议DependsOnTarget作为一种变通办法-这对我不起作用,因为我需要在不同的运行中从不同的地方获取物品。
这就是我到目前为止所知道的:
<Target Name="RunAllTests" DependsOnTarget="BuildProject">
<!-- In here, items created in BuildProject are available. -->
<CallTarget Targets="RunUnitTests;RunIntegrationTests">
</Target>
<Target Name="RunUnitTests" DependsOnTarget="BuildProject">
<!-- In here, items created in BuildProject are available. -->
<!-- One of those is @(UnitTestAssemblies) -->
<CreateItem Include="@(UnitTestAssemblies)">
<Output TaskParameter="Include" ItemName="TestAssemblies" />
</CreateItem>
<CallTarget Targets="RunTests" />
</Target>
<!-- Then there's a similar target named RunIntegrationTests, which does the
same as RunUnitTests except it includes @(IntegrationTestAssemblies) -->
<Target Name="RunTests">
<!-- Here, I'd like to access @(TestAssemblies) and pass them to the NUnit
task, but they have fallen out of scope. -->
</Target>有没有办法绕过这个问题,或者我必须完全重构我的构建脚本?
发布于 2011-07-18 17:32:46
对目标中的项目组的更改仅在更改目标退出后才对其他目标可见。因此,要获得要粘贴的测试程序集列表,您可能必须将实际设置的目标移动到它自己的目标,如下所示:
<Target Name="PrepareUnitTestList" DependsOnTarget="BuildProject">
<ItemGroup>
<TestAssemblies Include="@(UnitTestAssemblies)"/>
</ItemGroup>
</Target>
<Target Name="RunUnitTests" DependsOnTargets="PrepareUnitTestList">
<CallTarget Targets="RunTests"/>
</Target>
<Target Name="RunTests">
<Message Text="Test: %(TestAssemblies.Identity)"/>
</Target>发布于 2011-07-18 17:30:37
在"MSBuild“任务中,您可以将属性传递给目标,但我不确定它是否适用于ItemGroup。但是你绝对可以通过批处理来实现--一次传递一个程序集。
<Target Name="RunUnitTests">
<MSBuild Projects="$(MSBuildProjectFullPath)" Targets="RunTests" Properties="TestAssemblies=%(TestAssemblies.Identity)"/>
</Target>它一次只为一个程序集运行"RunTests“,因此如果您在运行测试时需要其他程序集的知识,那么它将是无用的。但也许它会给出一些更好的想法来解决这个问题……
https://stackoverflow.com/questions/6730710
复制相似问题