使用NUnit的TFS Build 2010代码覆盖率 [英] TFS Build 2010 Code Coverage using NUnit

查看:74
本文介绍了使用NUnit的TFS Build 2010代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你们中是否有人在运行NUnit测试时在TFS Build Server 2010中生成代码覆盖率报告的经验。

I was wondering if any of you guys had any experience generating code coverage reports in TFS Build Server 2010 while running NUnit tests.

我知道可以使用打包的替代方案(MSTest +启用对testrunco​​nfig文件的覆盖)轻松完成此操作,但是使用NUnit时要涉及更多的事情。我在这里和那里都找到了一些指向NCover的信息,但它似乎已经过时了。我想知道是否还有其他选择,以及是否有人确实实施了此操作。

I know it can be easily done with the packaged alternative (MSTest + enabling coverage on the testrunconfig file), but things are a little more involved when using NUnit. I've found some info here and there pointing to NCover, but it seems outdated. I wonder if there are other alternatives and whether someone has actually implemented this or not.

有关我们的环境/需求的更多信息:
-TFS Build Server 2010
-测试在普通类库中(不是测试库-即,没有关联的testrunco​​nfig文件),并且在NUnit中实现。我们没有MSTest。
-我们有兴趣在每个构建中运行覆盖率报告,并在可能的情况下为通过/失败条件设置覆盖率阈值要求。

Here's more info about our environment/needs: - TFS Build Server 2010 - Tests are in plain class libraries (not Test libraries - i.e., no testrunconfig files associated), and are implemented in NUnit. We have no MSTests. - We are interested in running coverage reports as part of each build and if possible setting coverage threshold requirements for pass/fail criteria.

推荐答案

我们已经使用NUnit-NCover做到了这一点,并对我们的结果感到非常满意。在
NUnit执行之后,先 NUnitTfs 执行,以便将我们的测试结果发布在构建日志中。然后NCover启动,生成我们的代码覆盖率结果。


不利的一件事是,设置正确调用NCover的参数并不是一件容易的事。但是自从我安装了它之后,就再也不需要维护它了。

We 've done it with NUnit-NCover and are pretty happy with our results.
NUnit execution is followed by NUnitTfs execution in order to get our testing results published in the Build Log. Then NCover kicks in, generating our code coverage results.

One major thing that poses as a disadvantage is fact that setting up the arguments for properly invoking NCover wasn't trivial. But since I installed it, I never had to maintain it.

有两个缺点:

Two things could pose as disadvantages:


  • NUnitTfs没有与NCover配合得很好(至少我找不到在同一步骤中执行这两种方法的方法,因此(由于NCover调用了NUnit),我必须两次运行单元测试:(1)获取测试结果,(2)

  • 设置适当调用NCover的参数并不是一件容易的事,但是自从我安装了它之后,我就不必再花时间了。保持它

无论如何,结果报告(尤其是趋势方面)在以下情况中非常有用监视我们的代码随时间变化的方式,尤其是在平台上(而不是短期项目),趋势报告非常有价值。

In any case, the resulting reporting (especially the Trend aspect) is very useful in monitoring how our code evolves within time. Especially if you 're working on a Platform (as opposed to short-timed Projects), Trend reports are of great value.

编辑

我将尝试以一种快速而肮脏的方式介绍如何实现此功能,希望它会有用。我们目前在NCover 3.4.12我们的构建服务器。

关于NUnit程序集的简单命名约定是,如果我们有生产程序集 123.dll,则存在另一个实现其测试的程序集 123_nunit.dll。因此,每个构建都有几个感兴趣的* _nunit.dll程序集。

EDIT
I 'll try to present in a quick & dirty manner how I 've implemented this, I hope it can be useful. We currently have NCover 3.4.12 on our build server.
Our simple naming convention regarding our NUnit assemblies is that if we have a production assembly "123.dll", then another assembly named "123_nunit.dll" exists that implements its tests. So, each build has several *_nunit.dll assemblies that are of interest.

构建过程模板中如果不禁用测试下的部分就是为了实现我们的目标而进行了重新设计,特别是名为为测试程序集运行MSTest的部分。进行了一些清理以使流程更容易后,整个实现是在此处

The part in the build process template under "If not disable tests" is the one that has been reworked in order to achieve our goals, in particular the section that was named "Run MSTest for Test Assemblies". The whole implementation is here, after some cleanups to make the flow easier to be understood (pic was too large to be directly inserted here).

首先,在构建过程模板&中添加了一些其他参数。然后可以在每个构建定义中进行设置:

At first, some additional Arguments are implemented in the Build Process Template & are then available to be set in each build definition:

然后在 Formulate nunitCommandLine中形成NUnit args:

We then form the NUnit args in "Formulate nunitCommandLine":

String.Format("{0} /xml={1}\\{2}.xml", nunitDLL, TestResultsDirectory, Path.GetFileNameWithoutExtension(nunitDLL))

然后用于调用NUnit

This is then used in the "Invoke NUnit"

如果成功,则&我们已经为此构建设置了覆盖率,我们移至 Generate NCover NCCOV(此特定程序集的覆盖率文件)。为此,我们调用NCover.Console.exe,并将其作为Args:

In case this succeeds & we have set coverage for this build we move to "Generate NCover NCCOV" (the coverage file for this particular assembly). For this we invoke NCover.Console.exe with the following as Args:

String.Format("""{0}"" ""{1}"" //w ""{2}"" //x ""{3}\{4}"" //literal //ias {5} //onlywithsource //p ""{6}""",
              NUnitPath,
              Path.GetFileName(nunitDLL),
              Path.GetDirectoryName(nunitDLL),
              Path.GetDirectoryName(Path.GetDirectoryName(nunitDLL)),
              Path.GetFileName(nunitDLL).Replace("_nunit.dll", ".nccov"),
              Path.GetFileNameWithoutExtension(nunitDLL).Replace("_nunit", ""),
              BuildDetail.BuildNumber)

所有这些都在foreach循环对于所有nunit dll中运行。退出循环时,我们输入最终NCover活动&首先是合并NCCovs部分,再次执行NCover.Console.exe-这次使用不同的args:

All these run in the foreach loop "For all nunit dlls". When we exit the loop, we enter "Final NCover Activities" & at first the part "Merge NCCovs", where NCover.Console.exe is executed again - this time with different args:

String.Format("""{0}\*.nccov"" //s ""{0}\{1}.nccov"" //at ""{2}\{3}\{3}.trend"" //p {1} ",
              Path.GetDirectoryName(Path.GetDirectoryName(testAssemblies(0))),
              BuildDetail.BuildNumber,
              NCoverDropLocation,
              BuildDetail.BuildDefinition.TeamProject
              )

运行该命令后,我们已经达到了此构建的所有NCCOV文件都合并到一个以该构建+趋势文件(在整个生命周期中监视该构建)的趋势文件中对该当前构建的元素进行了更新的NCCOV文件中。

When this has run, we have reached the point where all NCCOV files of this build are merged into one NCCOV-file named after the build + the Trend file (that monitors the build throughout its life) has been updated with the elements of this current build.

我们现在只需要生成最终的HTML报告,就可以在 Generate final NCover rep中完成,在该报告中,我们使用以下参数调用NCover.reporting:

We now have to only generate the final HTML report, this is done in "Generate final NCover rep" where we invoke NCover.reporting with the following args:

String.Format(" ""{0}\{1}.nccov"" //or FullCoverageReport //op ""{2}\{1}_NCoverReport.html"" //p ""{1}"" //at ""{3}\{4}\{4}_{5}.trend"" ",
              Path.GetDirectoryName(Path.GetDirectoryName(testAssemblies(0))),
              BuildDetail.BuildNumber,
              PathForNCoverResults,
              NCoverDropLocation,
              BuildDetail.BuildDefinition.TeamProject,
              BuildType
              )

这篇关于使用NUnit的TFS Build 2010代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆