在构建时自动运行单元测试
我很想知道是否有一种方法可以让 Visual Studio 在构建给定程序集时运行与给定程序集相对应的单元测试。
给定一个包含如下结构的项目的解决方案:
Assembly1
Assembly1.Tests
Assembly2
Assembly2.Tests
有没有办法让 Assembly2.Tests
中的单元测试在构建 Assembly2
时运行?
那真是太棒了。
我使用的是 Visual Studio 2008 标准版。
I would love to know if there is a way I can get Visual Studio to run unit tests corresponding to a given assembly whenever I build it.
Given a solution containing projects structured like this:
Assembly1
Assembly1.Tests
Assembly2
Assembly2.Tests
Is there a way I can get the unit tests in Assembly2.Tests
to run whenever Assembly2
is built?
That would be amazing.
I'm using Visual Studio 2008 Standard Edition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 nUnit 控制台实用程序将测试作为单个项目的构建后事件来运行。
您调用 nunit-console.exe 并提供包含测试的程序集作为参数。
"C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit-console.exe" "PathToTestAssembly.dll"
或者
您可以在 GUI 中运行测试:
"C:\Program Files\NUnit 2.5.7 \bin\net-2.0\nunit.exe" "PathToTestAssembly.dll" /run
编辑:
删除了有关测试程序集项目的构建后事件的部分。
You can use the nUnit console utility to run the tests as a post-build event for the individual project.
You call the nunit-console.exe and supply your assembly containing your tests as an argument.
"C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit-console.exe" "PathToTestAssembly.dll"
or
You can run the tests in the GUI:
"C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit.exe" "PathToTestAssembly.dll" /run
Edit:
Removed the part about the post-build event for the test assembly project.
Visual Studio 2019
更新,其中包含Test Explorer >设置>构建后运行测试
。适用于针对 NUnit、xUnit 或 MSTest 任意组合的单元测试,前提是测试由测试资源管理器
选取。值得注意的是,如果一个项目中发生编译错误,不针对失败项目的单元测试仍然会运行。似乎没有“仅在所有项目构建成功时才运行测试”的选项。
Update for
Visual Studio 2019
which hasTest Explorer > Settings > Run Tests After Build
. Works with unit tests targeting any combination of NUnit, xUnit or MSTest, provided the tests are picked up by theTest Explorer
.Worth noting that if a compilation error occurs in one project, unit tests which do not target the failed project still run. There does not appear to be an option to "Only run tests if all projects build successfully".