如何从 Jenkins 运行 NUnit 测试?
我希望在每晚以及每次提交到 svn 时为 C# 应用程序运行自动化 NUnit 测试。
这是 Jenkins-CI 可以做的事情吗?
是否有在线教程或操作文档,其中记录了我可以查看的类似设置?
I'm looking to run automated NUnit tests for a C# application, nightly and on each commit to svn.
Is this something that Jenkins-CI can do?
Is there an online tutorial or how-to document which documents a similar setup that I can look at?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我需要做的正是你所做的,以下是我如何设置 Jenkins 来执行此操作:
单个 dll 测试:
使用NUnit 测试项目进行多个 dll 测试:
项目构建完成后,NUNit 将立即运行,结果将在仪表板上(如果将鼠标悬停在天气报告图标上)或项目页面上的 最后一次测试结果。
您还可以从 Visual Studio 中运行该命令或作为本地构建过程的一部分。
这是我参考的两篇博文。我没有找到完全符合我要求的:
1 小时持续集成指南设置:Jenkins 遇见 .Net (2011)
使用 Hudson 构建 .NET 项目指南 (2008 )
I needed to do exactly what you do, here's how I setup Jenkins to do this:
Single dll test:
Multiple dll test using NUnit test projects:
Once you project has been built, NUNit will now run and the results will be viewable either on the Dashboard(if you hover over the Weather report icon) or on the project page under Last Test Result.
You could also run the command from within Visual Studio or as part of you local build process.
Here's two blog posts I used for reference. I didn't find any that fitted my requirements exactly:
1-Hour Guide to Continuous Integration Setup: Jenkins meets .Net (2011)
Guide to building .NET projects using Hudson (2008)
如果您不想对单元测试项目进行硬编码,那么最好编写一个脚本来获取所有单元测试项目 dll。我们使用 Powershell 来完成此操作,并遵循命名单元测试项目的特定约定。以下是运行我们的单元测试的 powershell 文件的内容:
该脚本足够强大,我们可以在所有构建作业中重用。如果您不喜欢 NUnit 控制台的完整路径,您可以随时将该位置放入 PATH 环境变量中。
然后我们将 RunUnitTests.ps1 文件放在构建服务器上并使用以下批处理命令:
If you don't want to hardcode your unit test projects, you are better off writing a script to grab all of your Unit Test project dll's. We do it with Powershell and follow a specific convention for naming our Unit Testing Projects. Here is the content of the powershell file that runs our unit tests:
The script is robust enough that we are reusing for all of our build jobs. If you don't like the full path to NUnit console, you could always put that location in your PATH environment variable.
Then we put the RunUnitTests.ps1 file on our build server and use this batch command:
对于 Nunit 3 或更高版本的 farmework:
构建步骤(Windows 命令行)
<代码>“c:\ Program Files(x86)\ NUnit.org \ nunit-console \ nunit3-console.exe”c:\ AutomationTraining \ CSharpSelenium \ bin \ Debug \ test.dll --result = TestR.xml;格式=nunit2
Nunit 报告发布的后步骤,它仅显示 Jenkins 工作区目录中的测试结果文件,而不是您的项目中的文件:
TestR.xml
我们需要以 nunit2 格式生成测试结果,因为现在 Jenkins Nunit 插件无法识别 Nunit3 结果格式。
选项字符串格式也不同:
--result=TestR.xml;format=nunit2
不是
/xml=nunit-result.xml
For Nunit 3 or above farmework:
Building Step (Windows command line)
"c:\Program Files (x86)\NUnit.org\nunit-console\nunit3-console.exe" c:\AutomationTraining\CSharpSelenium\bin\Debug\test.dll --result=TestR.xml;format=nunit2
Post step for Nunit report publishing, it shows only test results file in Jenkins workspace directory, not in your project:
TestR.xml
We need to make test results in nunit2 format because now Jenkins Nunit plugin doesn't recognize Nunit3 results format.
Also options string format is different:
--result=TestR.xml;format=nunit2
NOT
/xml=nunit-result.xml
这个效果很好,我之前已经设置过。
配置 NUnit 将结果输出到 XML 文件并配置 NUnit Jenkins 插件来使用此 XML 文件。结果将显示在仪表板上。
现在,如何调用 NUnit 由您决定。我们的做法是:
Jenkins 作业执行 NAnt 目标执行 NUnit 测试套件。
您可以将 Jenkins 作业配置为在提交时运行和/或计划在特定时间运行。
This works nicely, I've set this up before.
Configure NUnit to output the results to an XML file and configure the NUnit Jenkins Plugin to consume this XML file. The results will be available on the dashboard.
Now, how you invoke NUnit is up to you. The way we did it was:
Jenkins job executes NAnt target executes NUnit test suite.
You can configure Jenkins jobs to run on commit and/or scheduled at a certain time.
Ralph Willgoss 的解决方案运行良好,但我更改了两件事以使其变得更好:
a) 我使用 NUnit 项目而不是直接使用 DLL 文件。这使得在 NUnit GUI 中添加更多程序集或配置测试变得更加容易。
b)我在批处理中又添加了一行,以防止测试失败时构建失败:
提到的 NUnit 插件会自动标记构建不稳定,这正是我想要的,每当测试失败时。它显示有一个黄点。
The solution from Ralph Willgoss is working good, but i changed 2 things to make it great:
a) I used a NUnit project instead of the DLL file directly. This makes it more easy to add more assemblies or configure the test in the NUnit GUI.
b) I added one more line to the batch to prevent the build from failing when a test fails:
The NUnit Plugin mentioned marks the build UNSTABLE automatically, which is exactly what i want, whenever a test fails. It shows with a yellow dot.
我认为当构建未通过时最好让构建失败,这样你就不会部署它。做这样的事情:
参考:http://www.greengingerwine.com/index.php/2013/01/tip-check-errorlevel-in-your-post-build-steps-when-using-nunit/
I think it's better to fail the build when it doesn't pass so you don't deploy it. Do something like this:
Reference: http://www.greengingerwine.com/index.php/2013/01/tip-check-errorlevel-in-your-post-build-steps-when-using-nunit/
Jenkins 确实有支持它的插件。确切的配置在很大程度上取决于您的项目设置。 nUnit、MSBuild、nAnt 等都有特定的插件。首先查看插件页面,但应该不会很难弄清楚。
Jenkins does have plugins that will support that. The exact configuration is going to depend quite a bit on your project setup. There are specific plugins for nUnit, MSBuild,nAnt etc. Start by looking at the plugins page, but it shouldn't be terribly difficult to figure out.
这是我在 Jenkins 中使用 vstest 运行 OpenCover 的解决方案:
每个测试 dll 都在自己的进程中执行,因为我们在单个进程中执行所有测试 dll 时遇到了麻烦(问题与装配加载)。
This is my solution for running OpenCover with vstest in Jenkins:
Each test dll is executed in an own process because we had troubles to execute all test dlls in a single procress (probmels with assembly loading).
对于 .Net Core,只需使用以下脚本添加“执行 shell”构建步骤即可:
之后添加“发布 MSTest 测试结果报告”构建后操作以使测试结果可见。
默认测试报告路径应为
**/*.trx
并将发布所有生成的.trx
文件。For .Net Core it suffices to add "execute shell" build step with following script:
After that add "Publish MSTest test result report" post-build action to make test results visible.
Default test reports path should be
**/*.trx
and will publish all produced.trx
files.