如何使用 NAnt 0.86 beta 运行 NUnit v2.4.8 测试?

6

最近我尝试使用NAnt(beta 0.86.2962.0)运行一些使用最新稳定版本的NUnit(v2.4.8)编译的单元测试,但没有成功。

我得到的错误信息如下:

[nunit2]程序集“C:\Dev\MySample\bin\tests\My.Sample.Tests.dll”不包含任何测试。

当然,该程序集包含测试,我可以从任何测试运行器(如NUnit、TestDriven或Resharper)中运行它们。我想使用<nunit2>任务,而不是直接使用<exec>任务,但我想知道是否仍然可能,即使使用app.config文件来绑定程序集版本。

1个回答

10

我记不清为什么放弃使用<nunit2>任务,现在我一直使用<exec>任务和nunit-console.exe来测试,并且使用得很开心。如果有帮助的话,这是我的测试目标,可以运行NUnit和FxCop。请注意,如果可执行文件不在Windows路径中,它将跳过它们。

<target name="test" description="Run unit tests" depends="build">
  <property name="windows-path" value="${string::to-lower(environment::get-variable('PATH'))}"/>
  <property name="nunit-in-path"
      value="${string::contains(windows-path, 'nunit')}"/>
  <echo message="Tests skipped because no NUnit folder was found in the Windows path."
      unless="${nunit-in-path}"/>
  <exec program="nunit-console.exe" if="${nunit-in-path}">
      <arg file="../MyProject/MyProjectTest.nunit"/>
  </exec>
  <property name="fxcop-in-path"
      value="${string::contains(windows-path, 'fxcop')}"/>
  <echo message="FxCop skipped because no FxCop folder was found in the Windows path."
      unless="${fxcop-in-path}"/>
  <fxcop projectFile="../MyProject/MyProject.fxcop" directOutputToConsole="true" 
      failOnAnalysisError="true" if="${fxcop-in-path}"/>
</target>

1
我赞同这个观点...我曾经尝试过使用app.config的技巧来让nunit2任务正常工作,但是在一段时间后放弃了,现在我使用上面描述的exec任务。 - Andy Whitfield
2
唐,安迪:我认为使用<exec>任务而不是<nunit2>的一个好理由是,如果您的测试套件有标记为[ExpectedException]的测试,并且实际上抛出了异常,则<nunit2> NAnt任务将其报告为失败而不是成功,从而导致构建失败。我现在遇到了这个问题。我将通过使用<exec>来解决它,但当然也会欣赏其他解决方法的建议。 - azheglov
大家好,我正在尝试在 arg 文件中使用我的单元测试 dll。但是它没有显示哪些测试失败了。arg 标签没有 verbose 或 fail on error,那么你如何找出你的测试是失败还是通过了呢? - alice7
我认为nunit-console会在任何测试失败时将返回值设置为非零,这应该会导致构建失败,@alice7。我认为它还会为任何失败的测试写入stdout消息。如果您没有看到这一点,那么我怀疑其他问题出现了。尝试在命令提示符下运行nunit-console,以便您可以清楚地看到输出内容。Nant可能无法找到nunit-console.exe,或者NUnit可能无法找到您的测试DLL。如果这没有帮助,那么我建议您提出一个单独的问题,并提供尽可能多的细节。 - Don Kirkby

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接