2

我在使用带有 maven 的 TestNG 时遇到了一些奇怪的问题。我有太多代码要在这里发布,但我会发布一个相关的例子。

我的 pom.xml 中有这个用于 TestNG 测试:

....other pom stuff....

<dependency>
     <groupId>org.testng</groupId>
     <artifactId>testng</artifactId>
     <version>6.2</version>
     <type>jar</type>
</dependency>

....other pom stuff....   

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.14.1</version>
    <configuration>
        <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
 </plugin>

 ....other pom stuff....

我的 testng.xml 文件如下所示:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SOTest">
  <test name="SOTest">
    <classes>
      <class name="SOTest"/>
   </classes>
 </test>
</suite>

SOTest.java 看起来像这样:

import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;

public class SOTest {
    @BeforeSuite
    public void setup() {
        ...DO SOME STUFF...
        System.out.println("foo");
    }

    @BeforeTest
    public void setupTest() {
        ...DO SOME STUFF...
        System.out.println("bar");
    }   


    @Test
    public void test_good_subkey_pass() {
        System.out.println("baz");
        ...DO SOME STUFF...
    }
}

运行时mvn test,“foo”和“bar”被打印出来,但是它挂起并且“baz”永远不会被打印出来?有谁知道什么会阻止带有注释的方法@Test运行?

更新

在那之后还有另一个测试test_good_subkey_pass()有一个无限循环。为什么这会阻止第一个测试的运行?请注意,该preserve-order属性未设置为false

4

1 回答 1

2

You know the XML file defines the tests to be run, but TestNG does not run your tests in the order they exist in the class code. It appears your XML file specifies the order to run the class, but not the order to execute the methods. (Also, I know you can specify methods to include/exclude, but I'm not sure that even that defines the order they will run in. From my experience, tests have always run alphabetically.)

If another test had an infinite loop, that could explain why test_good_subkey_pass() wasn't run. Try removing the other test cases to see if that resolves the problem (or use the @AfterSuite or similar annotation to notify you of all test completion).

You may also just want to specify the method names in testng.xml

This is likely your best resource: http://testng.org/doc/documentation-main.html#testng-xml

于 2013-06-26T18:47:52.700 回答