简体   繁体   English

Visual Studio 2010中的代码覆盖率测试? 怎么样?

[英]Code coverage test in Visual Studio 2010? How?

I have a simple project on C#.NET and have written it in Visual Studio 2010. I am a newbie in C# and Visual Studio. 我在C#.NET上有一个简单的项目,并在Visual Studio 2010中编写过。我是C#和Visual Studio的新手。

I have written some unit tests, and they pass ok, but how can I test code coverage? 我已经编写了一些单元测试,但是它们通过了,但我怎样才能测试代码覆盖率?

Thanks in advance. 提前致谢。

I use NUnit as my testing framework and use TestDriven.Net to run VS2010's code coverage. 我使用NUnit作为我的测试框架,并使用TestDriven.Net来运行VS2010的代码覆盖。 This also works with MSTest ie Visual Studio tests. 这也适用于MSTest即Visual Studio测试。 First you need to get hold of TestDriven.Net: 首先,你需要掌握TestDriven.Net:

http://www.testdriven.net/download.aspx http://www.testdriven.net/download.aspx

Once TestDriven.Net is installed, you will get some extra items added to your Solution Explorer context menu eg right-click a project and you'll have some TestDriven options under "Test With". 安装TestDriven.Net后,您将获得一些额外的项目添加到您的解决方案资源管理器上下文菜单,例如右键单击项目,您将在“测试使用”下有一些TestDriven选项。 One of those options is "Coverage" which will run your unit tests with code coverage and then display the code coverage results window. 其中一个选项是“Coverage”,它将使用代码覆盖率运行您的单元测试,然后显示代码覆盖率结果窗口。 Works very well. 效果很好。

Update 更新

Of course you can only get code coverage in Visual Studio 2010 if you have a version that actually supports it ie Premium or Ultimate. 当然,如果你有一个实际支持它的版本,即Premium或Ultimate,你只能在Visual Studio 2010中获得代码覆盖率。

  1. In Solution Tree, under the Solution Items Folder, create a new Test Settings file . 在“解决方案树”中的“解决方案项目文件夹”下, 创建新的“测试设置”文 (TestSettings1.testsettings) (TestSettings1.testsettings)

  2. In the Test Setting dialog screen, Select Data and Diagnostics on the left. 在“测试设置”对话框屏幕中,选择左侧的“ 数据和诊断 ”。 Then check enabled for Code Coverage 然后选中启用代码覆盖率

  3. On the same screen, double-click Code Coverage , then check the .dlls you want to test. 在同一屏幕上, 双击“代码覆盖率” ,然后选中要测试的.dll。 Click Apply. 单击“应用”

  4. In the Visual Studio menu, select Test, Run, All Tests in Solution . 在Visual Studio菜单中,选择“ 测试”,“运行”,“解决方案中的所有测试”

  5. Once the test run, in the Visual Studio menu, select Test, Windows..., Code Coverage Results . 测试运行后,在Visual Studio菜单中,选择Test,Windows ...,Code Coverage Results

Assuming you have a product that supports Code Coverage Analysis (Visual Studio Premium or Ultimate, see more details on their features here ) you can follow the steps listed here: 假设您有一个支持代码覆盖率分析的产品(Visual Studio Premium或Ultimate,请在此处查看有关其功能的更多详细信息),您可以按照此处列出的步骤操作:

http://msdn.microsoft.com/en-us/library/ms182534(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/ms182534(v=vs.100).aspx

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

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