如何在VS单元测试框架中基于test_config.json条件运行测试
基于Microsoft.VisualStudio.TestTools.UnitTesting实现条件化测试执行
完全可以用该框架实现需求,以下是两种适配场景的具体方案:
1. 按测试类拆分的实现(Linux/Windows各一个测试类)
先定义配置实体类与读取逻辑,再在测试类的初始化阶段判断配置是否存在,为空则跳过整个类的测试:
步骤1:配置实体与读取逻辑
public class TestConfig { public ServerConfig? TestServerLinux { get; set; } public ServerConfig? TestServerWindows { get; set; } } public class ServerConfig { public string? Host { get; set; } public int Port { get; set; } public string? Username { get; set; } public string? Password { get; set; } }
步骤2:Linux测试类示例
[TestClass] public class LinuxSshTests { private static TestConfig _testConfig; [ClassInitialize] public static void LoadConfig(TestContext testContext) { // 读取配置文件(注意设置test_config.json的“复制到输出目录”为“如果较新则复制”) var configContent = File.ReadAllText("test_config.json"); _testConfig = JsonSerializer.Deserialize<TestConfig>(configContent) ?? new TestConfig(); // 配置为空则标记整个类的测试为跳过 if (_testConfig.TestServerLinux == null) { Assert.Inconclusive("Linux测试服务器配置为空,跳过所有Linux SSH测试"); } } [TestMethod] public void TestLinuxSshConnection() { // 编写Linux SSH连接测试逻辑,使用_testConfig.TestServerLinux } }
步骤3:Windows测试类示例
复制上述结构,将判断条件改为_testConfig.TestServerWindows == null即可。
2. 按测试方法拆分的实现(同一类中包含Linux/Windows测试方法)
在每个测试方法执行前判断对应配置是否存在,为空则跳过当前方法:
[TestClass] public class SshTests { private TestConfig _testConfig; [TestInitialize] public void LoadConfig() { var configContent = File.ReadAllText("test_config.json"); _testConfig = JsonSerializer.Deserialize<TestConfig>(configContent) ?? new TestConfig(); } [TestMethod] public void TestLinuxSshConnection() { if (_testConfig.TestServerLinux == null) { Assert.Inconclusive("Linux测试服务器配置为空,跳过该测试"); } // 编写Linux SSH测试逻辑 } [TestMethod] public void TestWindowsSshConnection() { if (_testConfig.TestServerWindows == null) { Assert.Inconclusive("Windows测试服务器配置为空,跳过该测试"); } // 编写Windows SSH测试逻辑 } }
进阶优化:自定义特性减少重复代码
如果测试方法较多,可以自定义特性封装判断逻辑,避免重复写条件:
public class ConditionalSshTestAttribute : TestMethodAttribute { private readonly bool _isConfigValid; private readonly string _skipMessage; public ConditionalSshTestAttribute(bool isLinuxTest) { var configContent = File.ReadAllText("test_config.json"); var testConfig = JsonSerializer.Deserialize<TestConfig>(configContent) ?? new TestConfig(); if (isLinuxTest) { _isConfigValid = testConfig.TestServerLinux != null; _skipMessage = "Linux测试服务器配置为空,跳过该测试"; } else { _isConfigValid = testConfig.TestServerWindows != null; _skipMessage = "Windows测试服务器配置为空,跳过该测试"; } } public override TestResult[] Execute(ITestMethod testMethod) { if (!_isConfigValid) { var result = new TestResult { Outcome = UnitTestOutcome.Inconclusive, TestFailureException = new AssertInconclusiveException(_skipMessage) }; return new[] { result }; } return base.Execute(testMethod); } }
使用时只需给测试方法标记特性:
[ConditionalSshTest(isLinuxTest: true)] public void TestLinuxSshConnection() { // Linux SSH测试逻辑 } [ConditionalSshTest(isLinuxTest: false)] public void TestWindowsSshConnection() { // Windows SSH测试逻辑 }
内容的提问来源于stack exchange,提问作者Aenye_Cerbin
相关产品推荐
相关产品推荐

