简体   繁体   English

在Visual Studio 2010中使用代码覆盖率运行NUnit测试

[英]Running NUnit tests in Visual Studio 2010 with code coverage

We have recently upgraded from Visual Studio 2008 to Visual Studio 2010. As part of our code base, we have a very large set of NUnit tests. 我们最近从Visual Studio 2008升级到Visual Studio 2010.作为代码库的一部分,我们有一组非常大的NUnit测试。 We would like to be able to run these unit tests within Visual Studio, but with code coverage enabled . 我们希望能够在Visual Studio中运行这些单元测试,但启用了代码覆盖 We have ReSharper, so can run the tests within Visual Studio, but it does not allow the code coverage tool to do its thing and generate the coverage statistics. 我们有ReSharper,因此可以在Visual Studio中运行测试,但它不允许代码覆盖工具执行其操作并生成coverage统计信息。

Is there any way to make this work, or will we have to convert the tests over to MSTest? 有没有办法让这项工作,或者我们是否必须将测试转换为MSTest?


EDIT: We've already tried the suggested tools: 编辑:我们已经尝试过建议的工具:

  • TestDriven.Net TestDriven.Net
    This runs the NUnit tests fine within Visual Studio, and even works with the code coverage, but it also includes the tests as part of the covered assemblies. 这在Visual Studio中运行NUnit测试很好,甚至可以使用代码覆盖,但它也包括测试作为覆盖程序集的一部分。 There doesn't seem to be a way to exclude the tests, but include the assembly being tested (our tests are in a different assembly from the code they test). 似乎没有办法排除测试,但包括正在测试的程序集(我们的测试与他们测试的代码在不同的程序集中)。 We also don't really want to spend the additional £2000 it would cost us to get TestDriven.net for the whole team. 我们也不想花费额外的2000英镑花费我们来为整个团队获得TestDriven.net。

  • Gallio 加利奥
    This didn't work. 这没用。 In order to get coverage output, the tests need to be run in the Visual Studio test runner. 为了获得覆盖输出,测试需要在Visual Studio测试运行器中运行。 Gallio does not provide this support for NUnit. Gallio不为NUnit提供此支持。

Any further ideas? 还有什么想法? We did find a couple of open source attempts to get this type of thing working, but they are stale and haven't been touched for a long time (and therefore don't support VS2010). 我们确实找到了几个开源尝试来让这种类型的东西工作,但它们已经陈旧并且长时间没有被触及(因此不支持VS2010)。

The gallio automation framework is an open source project which should allow you to accomplish this. gallio自动化框架是一个开源项目,应该允许您完成此任务。 The site is at http://www.gallio.org/ There is initial VS2010 support in the last release with more work on it in the nightly builds. 该网站位于http://www.gallio.org/ 。在上一版本中有最初的VS2010支持,在夜间版本中有更多的工作。

There is more information on this here: http://weblogs.asp.net/astopford/archive/2010/04/13/gallio-and-vs2010.aspx 这里有更多相关信息: http//weblogs.asp.net/astopford/archive/2010/04/13/gallio-and-vs2010.aspx

However, if you have money to spend on this take a look at http://TestDriven.Net 但是,如果你有钱花在这上面,请看看http://TestDriven.Net

Jetbrains appears to be tackling this issue with a new product called "dotCover". Jetbrains似乎正在使用名为“dotCover”的新产品解决这个问题。 It's still in it's infancy, but given their track record it should be a major player soon. 它还处于初期阶段,但考虑到他们的记录,它应该很快就会成为主要参与者。

Till it releases you can "test" the nightly builds located here . 直到它发布,你可以“测试” 这里的夜间版本。

You can get the VS 2010 code coverage functionality to work while running NUnit tests, but it's a hell of configuration work: 您可以在运行NUnit测试时使VS 2010代码覆盖功能正常工作,但这是配置工作的一部分:

Set up a generic test that runs the nunit-console-x86.exe like this: 设置运行nunit-console-x86.exe的通用测试,如下所示:

Does VS2010 Code Coverage support nUnit? VS2010代码覆盖率是否支持nUnit? (simplified description for xUnit) (xUnit的简化描述)

http://msdn.microsoft.com/en-us/library/ms182624(v=vs.100).aspx (general description) http://msdn.microsoft.com/en-us/library/ms182624 (v=vs.100) .aspx (一般说明)

Make sure: 确保:

  • you have "instrument in place" activated. 你已经激活了“仪器到位”。 Trusting the deployment of VS won't work 信任VS的部署将无法正常工作
  • you run nunit-console-x86.exe. 你运行nunit-console-x86.exe。 The 64-Bit version won't work. 64位版本无法使用。
  • all libraries that need code coverage results are build into a single folder. 所有需要代码覆盖率结果的库都构建到一个文件夹中。 Copying them (eg by build event) won't work. 复制它们(例如通过构建事件)将不起作用。 (For advanced: You can instrument the libraries in another location than their build output folder, but that's quite tricky.) (对于高级:您可以在其构建输出文件夹之外的其他位置检测库,但这非常棘手。)
  • the folder with all libraries is working directory for the generic test and you start NUnit from it. 包含所有库的文件夹是通用测试的工作目录,您可以从中启动NUnit。
  • you provide the argument to NUnit as relative path to the working folder. 您将NUnit的参数作为工作文件夹的相对路径提供。
  • you additionally provide NUnit with the command line option /noshadow 另外为NUnit提供了命令行选项/ noshadow

When you copy your dlls from different folders into a single folder for tests, strange things may happen. 当您将来自不同文件夹的dll复制到单个文件夹中进行测试时,可能会发生奇怪的事情。 I for instance, got the reassurring result that almost all of the code for unit tests is coverred when running the unit tests . 例如,我得到了令人反复的结果,即在运行单元测试时几乎所有的单元测试代码都被覆盖 What a shame, that code coverage did not find the library tested by the unit tests! 太可惜了,该代码覆盖率没有 找到单元测试测试图书馆!

Well, after setting up all build output directories I ran into the strangest problem: From that folder NUnit does not run. 好吧,在设置所有构建输出目录后,我遇到了最奇怪的问题:从该文件夹NUnit不运行。 I have it running from 2 separate locations, but here it goes "nunit-console-runner.dll not found". 我让它从2个不同的位置运行,但在这里它“nunit-console-runner.dll not found”。

I only got it right with instrumenting the tested library at another location via: 我只是通过以下方式在另一个位置检测测试库:

  • tests > edit settings > local 测试>编辑设置>本地
  • Data and diagnostics > code coverage 数据和诊断>代码覆盖
  • deactivate the original location and add the library from the test folder 停用原始位置并从测试文件夹中添加库

Today it worked instantly, but the other day I tried to get the locations right for about an hour. 今天它立即起作用了,但是前几天我试图将这些位置正确地运行了大约一个小时。 I still don't know what I messed up that day. 我仍然不知道那天我搞砸了什么。

Adrian, 阿德里安,

Have a look at TestMatrix - it will run your unit tests, does code coverage and unit test performance profiling - and all of these are natively integrated into Visual Studio. 看看TestMatrix - 它将运行您的单元测试,代码覆盖和单元测试性能分析 - 所有这些都本机集成到Visual Studio中。

I found the Code Coverage Runner project on CodePlex , which bridges the VS test and NUnit APIs via an adapter. 我在CodePlex上找到了Code Coverage Runner项目,它通过适配器连接VS测试和NUnit API。 As of writing this, the project hasn't been updated since March 2008, but it may suit your needs. 在撰写本文时,该项目自2008年3月以来尚未更新,但它可能适合您的需求。

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

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