TestNG方法来自TestNG.xml的序列

时间:2014-03-21 20:58:20

标签: java unit-testing selenium-webdriver testng

我有两个类,我把它放在TestNG.xml上,但它没有按顺序运行我的类就像那样

    public class TestBase {

    @Parameters({"paraTest"})

@Test(groups = "gp1",singleThreaded = true)
public void runMethodGP1(String a) throws InterruptedException {
        //Thread.sleep(2000);
        System.out.println("Invoked testString " + a);
    System.out.println("runMethodGP1()");
}

@Test(groups = "gp2",singleThreaded = true)
public void runMethodGP2() {
    System.out.println("runMethodGP2()");
}

@Test(groups = "gp3",singleThreaded = true)
public void runMethodGP3() {
    System.out.println("runMethodGP3()");
}

@Test(groups = "gp1",singleThreaded = true)
public void runMethod2GP1() {
    System.out.println("runMethod2GP1()");
}

@Test(groups = "gp2",singleThreaded = true)
public void runMethod2GP2() {
    System.out.println("runMethod2GP2()");
}


public class TestNGAnotationClass1 {

@Test(groups = "gp1")
public void runMethod3GP1CL1() throws InterruptedException {
    Thread.sleep(1000);
    System.out.println("runMethod3GP1CL1()");
}

@Test(groups = "gp3")
public void runMethod2GP3CL1() {
    System.out.println("runMethod2GP3CL1()");
}
}

我的TestNG.xml就是那样

  <?xml version='1.0' encoding='UTF-8' ?>
  <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  <suite name="Suite1" verbose="1" order-by-instances="true" preserve-order="true">

<test name="TestNGAnotationClass2" time-out="1" preserve-order="true" annotations="JDK">
    <parameter name="paraTest" value="Test">
    </parameter>
    <classes>
        <class name="excelfilereadapachepoi.TestBase">
        </class>
        <class name="excelfilereadapachepoi.TestNGAnotationClass1">
        </class>
    </classes>
  </test>
</suite>

输出:

runMethod2GP1() runMethod2GP2() 调用testString测试 runMethodGP1() runMethodGP2() runMethodGP3() runMethod2GP3CL1()

但它应该是那样的

调用testString测试 runMethodGP1() runMethodGP2() runMethod2GP1() runMethod2GP2() runMethodGP3() runMethod2GP3CL1()

任何人都可以告诉我哪里错了。

我正在使用TestNG 6.8.1

1 个答案:

答案 0 :(得分:0)

如果我正确理解你的问题,你想以指定的顺序运行测试,可以使用TestNG IMethodInterceptor。请查看http://beust.com/weblog2/archives/000479.html如何利用它们。

OR

使用dependsOnMethods和/或dependsOnGroups: