使用 testng.xml 文件时,TestNG 是否允许多个数据集?

Does TestNG allow multiple data sets when using the testng.xml file?

我已经能够通过将测试值硬编码到 DataProvider 中来进行数据驱动测试:

@DataProvider(name = "XXXData")
public Object[][] createXXXData() {
    // Our test values here are hard-coded, which is OK
    // You could also, say, read an Excel file and create the parameter sets on the fly
    // The three values are: string to use, expected alphabetic, expected numeric count
    return new Object[][] {
            { "a1a", 2, 1},
            { "b2", 1, 2},  // Wrong num count - will fail
            { "cc333", 2, 3},
            { "d44dd44", 3, 4},
            { "eeee55555", 5, 5},   // Wrong alpha count - will fail
            { "ffff", 4, 0},
            { "000", 0, 3},
    };

我想尝试使用 testng.xml 做同样的事情。 (我的一些同事对将测试数据与代码放在不同的文件中有偏见)。

我可以执行一组数据,如下。然而,none 我在网上看到的许多例子有多个值,与我上面显示的相似。

testng.xml不支持这个吗?

<?xml version="1.0" encoding="UTF-8"?>
<suite name="ParameterExampleSuite" parallel="false">
    <test name="ParameterTest">
        <classes>
            <parameter name="message" value="a1a"></parameter>
            <parameter name="alphaCount" value="2"></parameter>
            <parameter name="numericCount" value="1"></parameter>
            <class name="com.Acme.Business.Safes.XXX.XXXTest"/>
        </classes>
    </test>
</suite>

我知道我可以在 createXXXData() 中编写代码来读取外部文件并基于该文件创建数据集。 这都是使用 IntelliJ。 本题类似于TestNG DataProvider reading test data from the testng.xml config file?。但是,那里的答案似乎与用例不匹配。当然,答案并不比在程序本身中读取 Excel 或 xml 文件更方便。

您可以在不同的 class 上使用静态方法,并在您的测试中通过 dataProviderClass 使用它。这样一来,您的所有数据都将在一个单独的文件中。

您将必须定义多个测试,而不是在 类 标记中定义参数。 (我仍然认为您应该将数据保存在外部文件中,然后在 xml 中执行此操作)

<test name="Automation Test Results" >
    <parameter name="name" value="NV"></parameter>
    <classes>
        <class name="framework.tests.TestParams">
            <methods><include name="test1"></include></methods>
        </class>
    </classes>
  </test>
  <test name="Automation Test Results2" >
    <parameter name="name" value="NV1"></parameter>
    <classes>
        <class name="framework.tests.TestParams">
            <methods><include name="test2"></include></methods>
        </class>
    </classes>
  </test>