【问题标题】:How do I determine what target is calling my current target in Nant?如何确定在 Nant 中调用我当前目标的目标是什么?
【发布时间】:2010-09-28 03:36:02
【问题描述】:

我正在修改一个 Nant 构建脚本来运行一些单元测试。对于本地运行的测试和要在团队城市运行的测试,我有不同的目标。

<target name="run-unit-tests">  
   <property name="test.executable" value="tools\nunit\nunit-console.exe"/>
   <call target="do-unit-tests"/>
</target>

<target name="run-unit-tests-teamcity"> 
   <property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>      
   <call target="do-unit-tests"/>
</target>

在目标 do-unit-tests 我通过设置属性并调用 NCover 执行代码覆盖运行来设置运行哪些测试程序集,如下所示:

<target name="do-unit-test">
   <property name="test.assemblies" value="MyProject.dll">
   <call target="do-unit-test-coverage" />
</target>

<target name="do-unit-test-coverage">
   <ncover <!--snip -->
           commandLineArgs="${test.args}"
   <!--snip-->
   </ncover>
</target>

正如您在 ncover 部分中看到的,我需要一个名为 "test.args" 的属性。此属性取决于 "test.assemblies"

即:&lt;property name="test.args" value="${test.assemblies} &lt;!--snip --&gt;" /&gt;

test.args 需要在本地运行的单元测试和团队城市中的单元测试之间进行不同的设置...所以我试图弄清楚如何设置它。 p>

如果我将 test.args 的属性放在属性“test.assemblies”之后的“do-unit-test”中,如果 run-unit- 调用 do-unit-test,我就无法指定一个 test.args测试和另一个用于 run-unit-tests-teamcity。

我一直在尝试在“do-unit-test”中执行以下操作:

<if test="${target::exists('run-unit-tests-teamcity')}">
 <property name="test.args" value="..." />
</if>

但显然这不起作用,因为目标永远存在。

然后我想测试我当前的目标 do-unit-test 是否已被 run-unit-tests-teamcity

调用

这可能吗?我在 Nant 文档中看不到它?由于它不存在,这要么意味着它将成为未来的一个功能,要么我不了解 Nant 构建脚本中的内容是如何指定的。

【问题讨论】:

    标签: configuration nant build-script


    【解决方案1】:

    您可以在一个目标中定义属性,并在另一个目标中使用它们的值...例如,您可以定义

    <target name="run-unit-tests">
       <property name="test.executable" value="tools\nunit\nunit-console.exe"/>
       <property name="test.extratestargs" value="foo,bar,baz"/>
       <call target="do-unit-tests"/>
    </target>
    
    <target name="run-unit-tests-teamcity">
       <property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>         
       <property name="test.extrtestargs" value="foo,baz,quux,xyzzy"/>
       <call target="do-unit-tests"/>
    </target>
    
    <target name="do-unit-test-coverage">
       <property name="test.args" value="${test.assemblies} ${test.extratestargs} <!--snip -->" />
       <ncover <!--snip -->
               commandLineArgs="${test.args}" >
       <!--snip-->
       </ncover>
    </target>
    
    
    

    或者,如果您需要它们的结构完全不同,而不仅仅是具有一些不同的值,请利用属性替换延迟的事实:

    
    <?xml version="1.0"?>
    
    <project name="nanttest">
    
            <target name="run-unit-tests">
               <property name="test.executable" value="tools\nunit\nunit-console.exe"/>
               <property name="test.args" value="foo bar -assembly ${test.assemblies} baz" dynamic="true"/>
               <call target="do-unit-test"/>
            </target>
    
            <target name="run-unit-tests-teamcity">
               <property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>
               <property name="test.args" value="foo,baz,quux /a:${test.assemblies} xyzzy" dynamic="true"/>
               <call target="do-unit-test"/>
            </target>
    
            <target name="do-unit-test-coverage">
               <echo message="test.executable = ${test.executable}, test.args = ${test.args}" />
            </target>
    
            <target name="do-unit-test">
               <property name="test.assemblies" value="MyProject.dll"/>
               <call target="do-unit-test-coverage" />
            </target>
    
    
    </project>
    
    
    user@host:/tmp/anttest$ nant run-unit-tests [……剪断……] 运行单元测试: 做单元测试: 做单元测试覆盖: [echo] test.executable = tools\nunit\nunit-console.exe, test.args = foo bar -assembly MyProject.dll baz 构建成功 总时间:0秒。 user@host:/tmp/anttest$ nant -D:teamcity.dotnet.nunitlauncher=nunitlauncher run-unit-tests-teamcity [……剪断……] 运行单元测试团队城市: 做单元测试: 做单元测试覆盖: [回声] test.executable = nunitlauncher, test.args = foo,baz,quux /a:MyProject.dll xyzzy 构建成功 总时间:0秒。

    如果您真的,真的只需要知道您是否在 TeamCity 中运行,那么这应该会有所帮助:

    <target name="run-unit-tests-teamcity">
       <property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>         
       <property name="running.in.teamcity" value="true"/>
       <call target="do-unit-tests"/>
    </target>
    
    

    【讨论】:

    • 您的第二个示例不起作用,因为它需要在目标 do-unit-tests 中创建的 {test.assemblies} ......但是是的,我确实需要完全构建属性每个人都不一样
    • 啊 - 我错过了 NAnt 与 Java Ant 的工作方式不同 - 您需要在这些属性定义中显式设置 dynamic="true" 才能按我描述的那样工作。
    • 效果太棒了!感谢那。我不知道将动态设置为 true....
    【解决方案2】:

    我已经设法解决了这个问题。我不知道这是否是最好的解决方案,但它是一个解决方案。

    我设置了一个名为 test.type 的属性,然后使用 if 语句来确定它来自哪个目标。

    <target name="run-unit-tests">  
       <property name="test.executable" value="tools\nunit\nunit-console.exe"/>
       <property name="test.type" value="unit-tests" />
       <call target="do-unit-tests"/>
    </target>
    
    <target name="run-unit-tests-teamcity"> 
       <property name="test.executable" value="${teamcity.dotnet.nunitlauncher}"/>          
       <property name="test.type" value="unit-tests-teamcity" />
       <call target="do-unit-tests"/>
    </target>
    
    <target name="do-unit-test">
       <property name="test.assemblies" value="MyProject.dll">
       <call target="do-unit-test-coverage" />
    </target>
    
    <target name="do-unit-test-coverage">
    
       <if test="${test.type=='unit-tests'}">
          <property name="test.args" value="${test.assemblies} ..."/>
       </if>
            
       <if test="${test.type=='unit-tests-teamcity'}">
          <property name="test.args" value="... ${test.assemblies}"/>
       </if>
    
       <ncover <!--snip -->
               commandLineArgs="${test.args}"
       <!--snip-->
       </ncover>
    </target>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多