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

如何不使用TestNG.xml配置Selenium Grid实现测试用例并行执行?

可行性结论

完全可以不借助TestNG.xml实现Selenium Grid的测试用例并行运行,基于JDK自带的Thread/Runnable接口的方案是可行的,同时也有更易用的TestNG编程式API方案可选。

两种主流实现方式

基于JDK原生线程/线程池实现

直接将单条测试用例的全流程封装为Runnable任务,通过线程池批量提交即可实现并行,核心逻辑如下:

// 测试任务封装类
public class GridTestTask implements Runnable {
    private String browserType;
    public GridTestTask(String browserType) {
        this.browserType = browserType;
    }
    @Override
    public void run() {
        try {
            // 初始化连接Selenium Grid的远程驱动
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setBrowserName(browserType);
            WebDriver driver = new RemoteWebDriver(new URL("http://你的Grid Hub地址:4444/wd/hub"), capabilities);
            // 测试用例执行逻辑
            driver.get("测试目标地址");
            // 断言、操作步骤等业务逻辑
            // 测试结束销毁驱动
            driver.quit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

// 并行执行入口
public class CustomParallelRunner {
    public static void main(String[] args) throws InterruptedException {
        // 初始化固定大小线程池,控制并行度
        ExecutorService threadPool = Executors.newFixedThreadPool(4);
        // 提交多个测试任务并行执行
        threadPool.submit(new GridTestTask("chrome"));
        threadPool.submit(new GridTestTask("firefox"));
        threadPool.submit(new GridTestTask("edge"));
        threadPool.submit(new GridTestTask("safari"));
        // 等待所有任务执行完成
        threadPool.shutdown();
        threadPool.awaitTermination(2, TimeUnit.HOURS);
    }
}

该方案优势是灵活性极高,不需要依赖任何测试框架的配置规则;缺点是测试报告生成、用例失败重试、用例依赖管理、异常统计等测试通用能力都需要自行实现,维护成本随用例规模增长快速上升。

基于TestNG编程式API实现(无XML)

TestNG本身支持完全脱离XML配置,通过Java代码直接定义测试套件、并行规则,既可以保留TestNG的成熟能力,也不需要维护TestNG.xml文件,核心逻辑如下:

public class NoXmlTestNGRunner {
    public static void main(String[] args) {
        TestNG testNG = new TestNG();
        // 定义测试套件
        XmlSuite testSuite = new XmlSuite();
        testSuite.setName("Grid并行测试套件");
        // 配置并行规则:按测试方法级别并行,最大并行线程数5
        testSuite.setParallel(XmlSuite.ParallelMode.METHODS);
        testSuite.setThreadCount(5);
        // 配置要执行的测试类
        XmlTest testSet = new XmlTest(testSuite);
        testSet.setName("Grid测试集合");
        List<XmlClass> testClasses = Arrays.asList(
            new XmlClass(你的测试类1.class),
            new XmlClass(你的测试类2.class)
        );
        testSet.setClasses(testClasses);
        // 装配套件并执行
        testNG.setXmlSuites(Arrays.asList(testSuite));
        testNG.run();
    }
}

该方案优势是既不需要维护XML配置文件,又可以直接复用TestNG自带的测试报告、失败重试、用例分组、参数传递等成熟能力,开发成本极低。

业界推荐度说明
  • 只有当你的测试场景非常轻量化,用例数量极少且不需要复杂的测试管理能力时,才可以选择原生线程实现方案,该方案极少用于企业级生产测试体系。
  • 绝大多数企业级测试场景下,更推荐使用TestNG编程式API的无XML实现,兼顾了无配置文件的灵活性和测试框架的成熟能力,是当前的主流方案。
  • 不推荐针对复杂测试套件完全手写原生线程实现并行,测试过程的资源管控、异常处理、结果统计的重复造轮子成本远高于引入TestNG能力的成本。

内容的提问来源于stack exchange,提问作者PRITEN PATEL

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 16:24:03