Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在TestNG框架中为多个输入文件运行相同的代码?_Java_Automated Tests_Testng_Testng Eclipse - Fatal编程技术网

Java 如何在TestNG框架中为多个输入文件运行相同的代码?

Java 如何在TestNG框架中为多个输入文件运行相同的代码?,java,automated-tests,testng,testng-eclipse,Java,Automated Tests,Testng,Testng Eclipse,我的测试类有一些代码,可以执行所需的验证 测试等级: @Parameters({ "InputFile01"}) @Test public void testCase01(String InputFile01) { //Code xyz } @Parameters({ "InputFile02"}) @Test public void testCase01(String InputFile02) { //Code xyz (Same code as above) } 我必须多次复制上

我的测试类有一些代码,可以执行所需的验证

测试等级:

@Parameters({ "InputFile01"})
@Test
public void testCase01(String InputFile01) {
  //Code xyz
}

@Parameters({ "InputFile02"})
@Test
public void testCase01(String InputFile02) {
  //Code xyz (Same code as above)
}
我必须多次复制上述代码才能为不同的输入文件运行它,我该如何处理

我正在从xml运行测试套件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Regression">

<test name="PI01_Sprint1_ID12345">

        <classes>
            <class name="org.PI01.PI01_Sprint1_ID12345">
                <methods>
                    <parameter name="InputFile01" value="PI01\TC01.xml" />
                    <include name="testCase01" />
                     <parameter name="InputFile02" value="PI01\TC02.xml" />
                    <include name="testCase02" />

                </methods>
            </class>
        </classes>
    </test>

</suite>

您不需要使用参数化测试重复代码,这就是它的用途:)

在您的案例中,正确的用法似乎是:

@Parameters({ "filename"})
@Test
public void testCase01(String filename) {
  //openFile(filename)
  //do something
}
在配置调用测试中使用此参数的不同值:

<test name="test file1">
    <parameter name="filename" value="file1.txt" />
...    
</test>
<test name="test file2">
    <parameter name="filename" value="file2.txt" />
 ...    
</test>
更多:


Ps:我实际上并没有在testng中尝试过,但据mkyong称,它应该可以工作。我对junit也做了同样的工作,所以这个概念很熟悉
public class TestParameterDataProvider {

    @Test(dataProvider = "provideFilenames")
    public void test(String filename) {
        //openFile(filename)
        //assert stuff...
    }

    @DataProvider(name = "provideFilenames")
    public String[] provideData() {
        return new String[] { 
            "filename1", "filename2", "filename3" 
        };
    }

}