Configuration 我如何确定哪个目标调用我在Nant的当前目标?
我正在修改Nant构建脚本以运行一些单元测试。对于本地运行的测试和要在TeamCity上运行的测试,我有不同的目标Configuration 我如何确定哪个目标调用我在Nant的当前目标?,configuration,nant,build-script,Configuration,Nant,Build Script,我正在修改Nant构建脚本以运行一些单元测试。对于本地运行的测试和要在TeamCity上运行的测试,我有不同的目标 <target name="run-unit-tests"> <property name="test.executable" value="tools\nunit\nunit-console.exe"/> <call target="do-unit-tests&qu
<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>
在targetdo 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”
ie:
您可以在一个目标中定义属性,并在另一个目标中使用它们的值。。。例如,您可以定义
<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>
我设法解决了这个问题。我不知道这是否是最好的解决方案,但它是一个解决方案
我设置了一个名为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>
您的第二个示例不起作用,因为它需要在目标do单元测试中创建的{test.assemblies}……但是,是的,我确实需要为每个对象完全不同地构造属性啊-我忽略了NAnt与Java Ant的工作方式不同-您需要显式地设置dynamic=“true”在这些属性定义中,它可以像我描述的那样工作。这太棒了!谢谢你。我不知道如何将dynamic设置为true。。。。
<?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.
<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>
<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>