简体   繁体   English

寻求代码覆盖率和单元测试的建议

[英]asking for advice for code coverage and unit test

I am using VS2010 + C# + .Net 4.0. 我正在使用VS2010 + C#+ .Net 4.0。 I want to do code coverage for unit tests. 我想对单元测试进行代码覆盖。 I have two choices after investigating, my question is, I want to know the pros and cons of such two tools/choices. 在调查之后,我有两种选择,我的问题是,我想知道这两种工具/选择的优缺点。

  1. Using non-VS built-in tools, like ncover; 使用非VS内置工具,例如ncover;
  2. Using VSTS built-in tool, for example, 例如,使用VSTS内置工具,

http://msdn.microsoft.com/en-us/library/dd299398%28VS.90%29.aspx http://msdn.microsoft.com/en-us/library/dd299398%28VS.90%29.aspx

http://msdn.microsoft.com/en-us/library/ms379625%28VS.80%29.aspx#vstsunittesting_topic7 http://msdn.microsoft.com/en-us/library/ms379625%28VS.80%29.aspx#vstsunittesting_topic7

Having used both tools, they both work pretty much the same way. 使用这两种工具后,它们的工作方式几乎相同。 You run your tests and then you view the report. 您运行测试,然后查看报告。 Both tools provide code highlighting to show, which lines of code have coverage and which ones do not. 两种工具都提供了突出显示代码的功能,以显示哪些代码行具有覆盖范围,而哪些则没有。 MsTest does not provide much more in terms of information, but NCover provides lots of HTML reports and can trend code coverage over time. MsTest在信息方面没有提供更多信息,但是NCover提供了许多HTML报告,并且可以随着时间的流逝覆盖代码趋势。 These sorts of things can be helpful. 这些事情可能会有所帮助。

Now for the differences. 现在来看差异。 The MsTest coverage tools really only work with MsTest unit tests. MsTest覆盖工具实际上仅适用于MsTest单元测试。 It cannot easily cover tests written using NUnit (or other testing frameworks) from the IDE. 它无法轻松涵盖使用IDE的NUnit(或其他测试框架)编写的测试。 It can be done, but takes some time and requires invoking the coverage tools from the command line and instrumenting your assemblies manually. 可以完成,但是要花一些时间,并且需要从命令行调用coverage工具并手动检测装配。 Again, it can be done, but not very easily, especially for a beginner. 再说一次,可以做到,但不是很容易,特别是对于初学者。 NCover is more flexile and can be run with most testing frameworks. NCover更加灵活,可以在大多数测试框架中运行。 Also, I have found that it is easier to run NCover with an automated build server. 另外,我发现使用自动构建服务器运行NCover更容易。 I never had much luck automating MsTest coverage in CruiseControl, but I am guessing MS makes it work in Team Foundation Server. 我从来没有在CruiseControl中实现MsTest覆盖自动化的运气,但是我猜想MS可以使它在Team Foundation Server中工作。

Just a couple of small notes that might be helpful: 只是一些小笔记可能会有所帮助:

  1. Unfortunately, the freeware version of NCover does not work with the .Net 4.0 runtime 不幸的是,NCover的免费版本不适用于.Net 4.0运行时
  2. If you're interested in intgration with CruiseControl.Net, you can use the Gallio test runner to run the MSTest tests. 如果您对CruiseControl.Net的集成感兴趣,则可以使用Gallio测试运行程序来运行MSTest测试。 See Gallio: Cannot run tests because the MSTest executable was not found 请参见Gallio:无法运行测试,因为未找到MSTest可执行文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM