尝试...最终在MsBuild中等效

rip*_*234 12 msbuild

无论测试目标是成功还是失败(如C#/ Java中的try ... finally构造),如何在"Test"目标运行后运行某个清理任务.

Zac*_*ham 13

Target元素有一个OnError属性,您可以将其设置为目标,以便在出错时执行,但由于它仅在目标出错时才执行,因此它只解决了一半的场景.

您是否考虑过将目标链接在一起来代表您要执行的测试"步骤"?

<PropertyGroup>
    <TestSteps>TestInitialization;Test;TestCleanup</TestSteps>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

'TestInitialization'目标是您可以执行任何测试初始化​​的地方,'Test'目标执行测试,'TestCleanup'目标执行任何类型的测试后清理.

然后,使用CallTarget任务执行这些目标,并将RunEachTargetSeparately属性设置为True.这将执行所有目标,无论成功与否.

完整的样本如下:

<Project DefaultTargets = "TestRun"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >

    <!-- Insert additional tests between TestInitialization and TestCleanup as necessary -->
    <PropertyGroup>
        <TestSteps>TestInitialization;Test;TestCleanup</TestSteps>
    </PropertyGroup>

   <Target Name = "TestRun">

      <CallTarget Targets="$(TestSteps)" RunEachTargetSeparately="True" />

   </Target>

    <Target Name = "TestInitialization">
        <Message Text="Executing Setup..."/>
    </Target>

    <Target Name = "Test">
        <Message Text="Executing Test..."/>

        <!-- this will fail (or should unless you meet the conditions below on your machine) -->
        <Copy 
          SourceFiles="test.xml"
          DestinationFolder="c:\output"/>
    </Target>

    <Target Name = "TestCleanup">
        <Message Text="Executing Cleanup..."/>
    </Target>

</Project>
Run Code Online (Sandbox Code Playgroud)