【问题标题】:TestNG Test Won't RunTestNG 测试不会运行
【发布时间】:2013-06-25 21:54:12
【问题描述】:

我在使用带有 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

【问题讨论】:

  • 你的依赖项中有什么版本的testng?
  • 您的导入是否具有 junit @test 导入而不是 testng @test 导入,这也可能导致此问题
  • @niharika_neo 我更新了问题

标签: java maven testng


【解决方案1】:

您知道 XML 文件定义了要运行的测试,但 TestNG 不会按照它们在类代码中的顺序运行您的测试。您的 XML 文件似乎指定了运行 class 的顺序,但没有指定执行 methods 的顺序。 (另外,我知道您可以指定包含/排除的方法,但我不确定即使这样定义了它们将运行的顺序。根据我的经验,测试总是按字母顺序运行。)

如果另一个测试有一个无限循环,这可以解释为什么 test_good_subkey_pass() 没有运行。尝试删除其他测试用例以查看是否可以解决问题(或使用@AfterSuite 或类似的注释来通知您所有测试完成)。

您可能还只想在 testng.xml 中指定方法名称

这可能是你最好的资源:@​​987654321@

【讨论】:

    猜你喜欢
    • 2017-01-09
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 2017-04-23
    • 1970-01-01
    相关资源
    最近更新 更多