如何确定哪个目标正在调用我在南特的当前目标?

发布于 2024-07-11 12:22:12 字数 1635 浏览 7 评论 0原文

我正在修改 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”,

即:

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

如果我将 test.args 的属性放在属性“test.assemblies”之后的“do-unit-test”中,如果 run-unit-tests 和另一个调用 do-unit-test,则我无法指定一个 test.args用于运行单元测试 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 构建脚本中如何指定事物。

I am modifying a Nant build script to run some unit tests. I have different targets for locally run tests and tests to be run on team city.

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

in the target do-unit-tests I set up which test assemblies are run by setting a property and calling for NCover to do a code coverage run as follows:

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

As you can see in the ncover part I need a property called "test.args". This property depends on "test.assemblies"

ie: <property name="test.args" value="${test.assemblies} <!--snip -->" />

test.args needs to be set up differently between the locally run unit test and the one on team city...so I'm trying to figure out how to set this up.

if i put the property for test.args in "do-unit-test" after the property "test.assemblies" I can't specify one test.args if do-unit-test is called by run-unit-tests and another for run-unit-tests-teamcity.

I've been trying to do something like the following in "do-unit-test":

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

but obviously that doesn't work because the target will always exist.

What I'd like then is to test if my current target do-unit-test has been called by run-unit-tests-teamcity

Is this possible? I can't see it in the Nant documentation? Since its not there it either means that it will be a feature in the future or that I'm not understanding how things are specified in a Nant build script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剧终人散尽 2024-07-18 12:22:12

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

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


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


<?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
[...snip...]
run-unit-tests:
do-unit-test:
do-unit-test-coverage:
     [echo] test.executable = tools\nunit\nunit-console.exe, test.args = foo bar -assembly MyProject.dll baz
BUILD SUCCEEDED
Total time: 0 seconds.

user@host:/tmp/anttest$ nant -D:teamcity.dotnet.nunitlauncher=nunitlauncher run-unit-tests-teamcity
[...snip...]
run-unit-tests-teamcity:
do-unit-test:
do-unit-test-coverage:
     [echo] test.executable = nunitlauncher, test.args = foo,baz,quux /a:MyProject.dll xyzzy
BUILD SUCCEEDED
Total time: 0 seconds.

如果您真的、真的只需要知道您是否在 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>

You can define properties in one target, and use their values in the other... For example, you can define

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


Or if you need them to be structured completely differently, not just have some different values, take advantage of the fact that the property substitution is delayed:


<?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
[...snip...]
run-unit-tests:
do-unit-test:
do-unit-test-coverage:
     [echo] test.executable = tools\nunit\nunit-console.exe, test.args = foo bar -assembly MyProject.dll baz
BUILD SUCCEEDED
Total time: 0 seconds.

user@host:/tmp/anttest$ nant -D:teamcity.dotnet.nunitlauncher=nunitlauncher run-unit-tests-teamcity
[...snip...]
run-unit-tests-teamcity:
do-unit-test:
do-unit-test-coverage:
     [echo] test.executable = nunitlauncher, test.args = foo,baz,quux /a:MyProject.dll xyzzy
BUILD SUCCEEDED
Total time: 0 seconds.

If you really, really just need to know if you're running in TeamCity, then this should help:

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

烈酒灼喉 2024-07-18 12:22:12

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

我设置了一个名为 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>

I've managed to solve the problem. I don't know if it's the best solution but it is a solution.

I set a property called test.type and then use an if statement to determine which target it came from.

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文