简体   繁体   English

测试TestNG侦听器

[英]Testing TestNG listeners

I have a test A that programmatically runs another test B and test A passes only if test B fails. 我有一个测试A,该测试以编程方式运行另一个测试B,并且仅当测试B失败时,测试A才通过。 I want the build to succeed only when test A passes. 我希望构建仅在测试A通过时才能成功。

Precisely, I implemented TestNG listener ( MyListener ). 精确地,我实现了TestNG侦听器( MyListener )。 I need to verify whether it is handling results of failing tests properly. 我需要验证它是否正确处理了失败的测试结果。 The only approach to test it seems to have test A ( testListener ) run another test B ( AlwaysFailingTest ) programmatically and check the results. 测试它的唯一方法似乎是使测试A( testListener )以编程方式运行另一个测试B( AlwaysFailingTest )并检查结果。

@Test
public void testListener() {
    System.setProperty("knownBugs", "execute");
    tla = new TestListenerAdapter();
    TestNG testng = new TestNG();
    testng.setTestClasses(new Class[]{AlwaysFailingTest.class});
    testng.addListener(tla);
    testng.addListener(new MyListener());
    testng.run();
    assertThat(tla.getSkippedTests().size(), equalTo(0));
    assertThat(tla.getFailedTests().size(), equalTo(1));
    assertThat(tla.getPassedTests().size(), equalTo(0));
}

However, I don't want the test that is failing on purpose to impact test results of my library. 但是,我不希望故意失败的测试影响我的库的测试结果。 Currently, for mvn clean test it is like that: 当前,对于mvn clean test是这样的:

Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.407 sec <<< FAILURE!
shouldAlwaysFail(com.testing.listeners.MyListenerTest$AlwaysFailingTest)  Time elapsed: 0.01 sec  <<< FAILURE!
java.lang.AssertionError: Failing as always
        at org.testng.Assert.fail(Assert.java:94)
        at com.testing.listeners.MyListenerTest$AlwaysFailingTest.shouldAlwaysFail(MyListenerTest.java:99)

testListener() method is passing, that's great, but why results of the AlwaysFailingTest appear in the library test results, I don't understand. testListener()方法正在传递,这很好,但是为什么我总是不理解AlwaysFailingTest结果出现在库测试结果中的原因。 How can I avoid that? 我该如何避免呢?

I listed full code of MyListener and MyListenerTest here . 在这里列出了MyListenerMyListenerTest完整代码。

shouldAlwaysFail is being executed twice: once by the main test suite and once programatically by the testListener test. shouldAlwaysFail被执行两次:一次由主测试套件执行,一次由testListener测试编程testListener As it always fails the main test suite results will contain the failure. 由于它总是失败,因此主测试套件结果将包含失败。 To fix this you can exclude the shouldAlwaysFail test from the main test suite so that it only gets run programmatically by the testListener test. 要解决此问题,您可以从主测试套件中排除shouldAlwaysFail测试,以使其仅以编程方式由testListener测试运行。

You can configure Maven Surefire to exclude your test: 您可以配置Maven Surefire排除测试:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
          <excludes>
            <exclude>**/AlwaysFailingTest.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Or configure TestNG to exclude your test: 或配置TestNG以排除测试:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="SuiteName" verbose="1">
    <test name="TestName">
        <packages>
            <package name="com.testing.listeners"/>
        </packages>
        <classes>
            <class name="com.testing.listeners.AlwaysFailingTest">
                <methods>
                    <exclude name="shouldAlwaysFail"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

See Maven Surefire Plugin – Inclusions and Exclusions of Tests and/or TestNG Documentation - 3 - testng.xml for more details. 有关更多详细信息,请参见Maven Surefire插件-测试和/或TestNG文档的 包含和排除 -3-testng.xml

Why don't you use "expectedExceptions" attribute of the @Test annotation so that the test will expect it to always FAIL and will not stop your execution. 您为什么不使用@Test批注的“ expectedExceptions”属性,以便测试将期望它始终失败并且不会停止执行。

@Test(expectedExceptions=AssertionError.class)
    public void test(){
        Assert.fail();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM