如何在Jenkins中通过PowerShell跳过指定NUnit测试?
Absolutely! You can skip specific NUnit tests directly through PowerShell by leveraging NUnit console's built-in filtering parameters—no need to modify your test code (unless you want to use categorization for easier long-term management). Here are a few practical, actionable ways to do this:
1. Skip Tests by Category (Recommended for Long-Term Management)
If you want a reusable way to mark tests for skipping, first tag the tests you want to exclude with a [Category] attribute in your test code:
[Test, Category("SkipOnJenkins")] public void MyTestToSkip() { // Your test logic here }
Then update your PowerShell script's Invoke-Expression line to add the /exclude parameter, specifying the category name you created:
$tests = '' Get-ChildItem -path "." -Recurse -Include "*Test*.dll" | Where-Object {$_.DirectoryName -Match ".*\\bin\\Debug"} | foreach {$tests += ($_.toString() -ireplace ([regex]::Escape($env:WORKSPACE) + '\\'), ' ')} $env:COMPLUS_Version='v4.0.0000' # Add /exclude to skip tests in the "SkipOnJenkins" category Invoke-Expression "C:\Jenkins\workspace\APP\Build\NUnitForms\lib\nunit\nunit-console-x86.exe $tests /result:nunit-result.xml /exclude:SkipOnJenkins"
This will automatically exclude all tests tagged with that category every time you run the script.
2. Skip Tests by Name (Temporary/Ad-Hoc Skipping)
If you need to skip specific tests without touching your test code, use NUnit's name filtering with the /exclude parameter. You can use exact names or wildcard/regex patterns:
- Skip a single test by exact name:
/exclude:Name=MySpecificTestToSkip - Skip all tests containing a keyword (e.g., "Slow"):
/exclude:Name~Slow
Here's how your modified PowerShell command would look for skipping slow tests:
Invoke-Expression "C:\Jenkins\workspace\APP\Build\NUnitForms\lib\nunit\nunit-console-x86.exe $tests /result:nunit-result.xml /exclude:Name~Slow"
3. Note for NUnit 3+ Users
If you're using a newer NUnit 3 console runner (instead of the NUnit 2.x nunit-console-x86.exe), the parameter syntax changes slightly—use --where instead of /exclude:
# Skip tests in "SkipOnJenkins" category Invoke-Expression "C:\Jenkins\workspace\APP\Build\NUnitForms\lib\nunit\nunit3-console.exe $tests --result:nunit-result.xml --where 'cat != SkipOnJenkins'" # Skip tests with name containing "Slow" Invoke-Expression "C:\Jenkins\workspace\APP\Build\NUnitForms\lib\nunit\nunit3-console.exe $tests --result:nunit-result.xml --where 'name !~ Slow'"
内容的提问来源于stack exchange,提问作者Gopinath

