在VSTS中排除C# Selenium测试项目中的指定测试方法
I’ve helped a few teams set up similar workflows in VSTS (now Azure DevOps) for their Selenium tests. Here’s a practical, step-by-step solution to get your scheduled build running only the tests you want:
1. Complete & Refine Your runsettings File
Your partial runsettings file needs a dedicated TestFilter section to exclude specific test classes. Here’s a full, working example tailored to your scenario:
<?xml version="1.0" encoding="utf-8"?> <RunSettings> <TestRunParameters> <!-- Add any test-specific parameters here, e.g., browser type for Selenium --> <Parameter name="Browser" value="Chrome" /> </TestRunParameters> <RunConfiguration> <TargetPlatform>x64</TargetPlatform> <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion> <!-- Match your project's .NET framework --> </RunConfiguration> <TestFilter> <!-- Exclude specific test classes using their fully qualified names --> <Exclude>FullyQualifiedName~YourNamespace.UnwantedTestClass1|FullyQualifiedName~YourNamespace.UnwantedTestClass2</Exclude> </TestFilter> </RunSettings>
Quick Notes on Filtering:
- Use
FullyQualifiedName~to match partial class names, or write the full name (e.g.,YourNamespace.UnwantedTestClass1) for exact exclusions. - Separate multiple filter rules with
|pipes. - If you’d rather include only specific classes instead of excluding, swap
<Exclude>with<Include>and list the classes you want to run.
2. Link runsettings to the VS Test Task
Once your runsettings file is ready, connect it to your build’s VS Test task:
- In your build definition, locate the VS Test task.
- Under the Settings section, click the folder icon next to Run settings file and select your runsettings file from the repo.
- Confirm the Test files field points to your
SeleniumTest.dll(e.g.,**\SeleniumTest.dllto find it in any subfolder). - Optional: Add extra filtering directly in the Test filter criteria field (e.g.,
TestCategory:Smoke) to narrow things down further.
3. Alternative: Skip runsettings & Use Direct Filtering
If you don’t need the extra configuration from runsettings, you can skip the file entirely and exclude classes directly in the VS Test task’s Test filter criteria field:
FullyQualifiedName!~YourNamespace.UnwantedTestClass1&FullyQualifiedName!~YourNamespace.UnwantedTestClass2
The !~ operator means "does not contain"—a simple way to exclude classes without a runsettings file.
4. Troubleshooting Tips
- Double-check class names: Typos in fully qualified names are the most common issue—verify them against your project’s namespace structure.
- Test locally first: Run the tests with the same runsettings file in Visual Studio’s Test Explorer to confirm filtering works before deploying to VSTS.
- Inspect build logs: If tests aren’t behaving as expected, check the VS Test task logs in the build results—they’ll show exactly which tests were included or excluded.
内容的提问来源于stack exchange,提问作者psycho

