You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Selenium+NUnit框架中运行时传入测试类别?

解决方案

1. 运行时传入环境参数(推荐)

利用NUnit的TestContext.Parameters,可以在运行测试时通过命令行或IDE配置传入环境参数,完全不需要修改测试用例的Category标记。

修改Setup方法:

[SetUp]
public void SetUpTest()
{
    // 从运行参数读取环境,默认设为Dev
    string environment = TestContext.Parameters.Get("Environment", "Dev");
    
    // 根据环境匹配对应URL
    _url = environment switch
    {
        "SIT" => "SIT-URL",
        "UAT" => "UAT-URL",
        _ => "DEV-URL" // 默认 fallback 到 Dev
    };

    // 原有ChromeDriver配置保持不变
    ChromeOptions options = new ChromeOptions();
    options.AddArgument("--headless=new");
    options.AddArgument("--use-fake-ui-for-media-stream");
    options.AddArguments("--disable-gpu");
    options.AddArguments("--start-maximized");
    options.AddArgument("--incognito");
    options.AddArguments("--allow-insecure-localhost");

    Driver = new ChromeDriver(options);
    Driver.Manage().Window.Maximize();
}

运行方式:

  • 命令行:使用nunit控制台运行时附加参数
    nunit3-console YourTestAssembly.dll --params:Environment=SIT
    
  • Visual Studio:在测试项目的运行配置中,将--params:Environment=UAT添加到「附加命令行参数」栏。

2. 参数化测试夹具(类级别批量切换)

如果希望一次性给整个测试类指定环境,可以用NUnit的参数化TestFixture,通过构造函数传递环境参数:

基类改造:

public class TestBase
{
    protected string _url;
    protected IWebDriver Driver;

    public TestBase(string environment)
    {
        _url = environment switch
        {
            "SIT" => "SIT-URL",
            "UAT" => "UAT-URL",
            _ => "DEV-URL"
        };
    }

    [SetUp]
    public void SetUpTest()
    {
        // 原有ChromeDriver配置保持不变
        ChromeOptions options = new ChromeOptions();
        options.AddArgument("--headless=new");
        options.AddArgument("--use-fake-ui-for-media-stream");
        options.AddArguments("--disable-gpu");
        options.AddArguments("--start-maximized");
        options.AddArgument("--incognito");
        options.AddArguments("--allow-insecure-localhost");

        Driver = new ChromeDriver(options);
        Driver.Manage().Window.Maximize();
    }

    [TearDown]
    public void TearDownTest()
    {
        Driver?.Quit();
    }
}

测试类使用参数化Fixture:

// 单个环境测试
[TestFixture("SIT")]
// 如需同时跑多环境,添加多个Fixture标记即可
// [TestFixture("Dev")]
// [TestFixture("UAT")]
public class YourTests : TestBase
{
    public YourTests(string environment) : base(environment)
    {
    }

    [Test]
    public void SampleTest()
    {
        Driver.Navigate().GoToUrl(_url);
        // 测试逻辑
    }
}

3. 配置文件管理环境URL

将环境URL存入App.config配置文件,通过修改配置项或结合运行参数切换环境:

App.config添加配置节点:

<appSettings>
  <add key="DevUrl" value="DEV-URL"/>
  <add key="SitUrl" value="SIT-URL"/>
  <add key="UatUrl" value="UAT-URL"/>
  <add key="CurrentEnvironment" value="Dev"/> <!-- 默认环境 -->
</appSettings>

Setup方法读取配置:

[SetUp]
public void SetUpTest()
{
    // 优先读取运行参数,没有则用配置文件默认值
    string currentEnv = TestContext.Parameters.Get("Environment", ConfigurationManager.AppSettings["CurrentEnvironment"]);
    
    _url = currentEnv switch
    {
        "SIT" => ConfigurationManager.AppSettings["SitUrl"],
        "UAT" => ConfigurationManager.AppSettings["UatUrl"],
        _ => ConfigurationManager.AppSettings["DevUrl"]
    };

    // 原有ChromeDriver配置保持不变
    ChromeOptions options = new ChromeOptions();
    options.AddArgument("--headless=new");
    options.AddArgument("--use-fake-ui-for-media-stream");
    options.AddArguments("--disable-gpu");
    options.AddArguments("--start-maximized");
    options.AddArgument("--incognito");
    options.AddArguments("--allow-insecure-localhost");

    Driver = new ChromeDriver(options);
    Driver.Manage().Window.Maximize();
}

内容的提问来源于stack exchange,提问作者Mandar Nageshkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 06:29:59