使用TestNG SoftAssertions通过测试时如何打印通过消息

时间:2019-03-27 21:54:05

标签: testng

在SoftAssertion中,当测试失败时,它仅打印失败消息,但没有规定在测试通过时打印通过消息。我想在SoftAssertion通过时打印通过消息。

我为AssertEquals编写了以下包装方法,如下所示: 1. SoftAssertion.java == >>

import org.testng.asserts.SoftAssert;

public class SoftAssertion {

    public SoftAssert softAssert = new SoftAssert();

    public SoftAssertion() {
    }

    public void assertAll(){
        softAssert.assertAll();
    }

public void assertEquals(String actual, String expected, String failMessage, String passMessage) {
       if (actual.equals(expected)){
               softAssert.assertEquals( actual,  expected,  failMessage);
               Report.testPass(passMessage);
           } else{
               softAssert.assertEquals( actual,  expected,  failMessage);
               Report.testFail(failMessage);
           }
       }
}
  1. 测试类== >>
public class NewSoftAssertTest {

    @Test
    public void myTest(){
        Report.startReport("New Soft Assert Test",
                "1) Test Soft Assert");

        SoftAssertion softAssert = new SoftAssertion();

        softAssert.assertEquals("A","B","Strings are not equal", "Strings are equal");
        softAssert.assertEquals("Hello", "Hello", "Hello is not equal to Hello", "Hello is equal to Hello");

        softAssert.assertAll();

    }
}

上面的代码工作得很好。我想知道这是在TestNG SoftAssert中记录通过消息的正确方法吗?

在其中一篇文章中,有人建议如下添加try catch块 1. SoftAssertion .java == >>

import org.testng.asserts.SoftAssert;

public class SoftAssertion {

    public SoftAssert softAssert = new SoftAssert();

    public SoftAssertion() {
    }

    public void assertAll(){
        softAssert.assertAll();
    }

public void assertEquals(String actual, String expected, String failMessage, String passMessage){
       try {
           softAssert.assertEquals(actual, expected, failMessage);
           System.out.println("Assertion passed");
           Report.testPass(passMessage);
       }catch (AssertionError e){
           System.out.println("Assertion failed");
           Report.testFail(failMessage);
           throw e;
       }
   }

}
  1. 测试类== >>
public class NewSoftAssertTest {

    @Test
    public void myTest(){
        Report.startReport("New Soft Assert Test",
                "1) Test Soft Assert");

        SoftAssertion softAssert = new SoftAssertion();
        softAssert.assertEquals("A","B","Strings are not equal", "Strings are equal");
        softAssert.assertEquals("Hello", "Hello", "Hello is not equal to Hello", "Hello is equal to Hello");
        softAssert.assertAll();

    }
}

在控制台上,它正确显示了A和B的断言失败,但是在报表中,它通过了步骤并显示“字符串相等”

Q1。我的问题是哪里出了问题?它应该进入try catch块并记录失败消息。

Q2。哪种方法更好,使用try catch的第一种还是第二种?

1 个答案:

答案 0 :(得分:1)

为了确保您记录了已通过和失败的断言的消息,您可以执行以下操作(我使用的是TestNG的最新发行版,即7.0.0-beta3):< / p>

通过扩展org.testng.asserts.SoftAssert来构建自定义的断言类[如果要扩展它,也可以看看org.testng.asserts.LoggingAssert]。

这是一个示例实现。

import org.testng.asserts.IAssert;
import org.testng.asserts.SoftAssert;

public class SimpleLoggingAssert extends SoftAssert {

  @Override
  public void onAssertSuccess(IAssert<?> assertCommand) {
    System.err.println(assertCommand.getMessage() + " <PASSED> ");
  }

  @Override
  public void onAssertFailure(IAssert<?> assertCommand, AssertionError ex) {
    String suffix =
        String.format(
            "Expected [%s] but found [%s]",
            assertCommand.getExpected().toString(), assertCommand.getActual().toString());
    System.err.println(assertCommand.getMessage() + " <FAILED>. " + suffix);
  }
}

这是测试代码:

import org.testng.annotations.Test;

public class NewSoftAssertTest {

  @Test
  public void myTest() {

    SimpleLoggingAssert softAssert = new SimpleLoggingAssert();

    softAssert.assertEquals("A", "B", "Test1: Ensure strings are equal");
    softAssert.assertEquals("Hello", "Hello", "Test2: Ensure strings are equal");

    softAssert.assertAll();
  }
}

这是执行输出

Test1: Ensure strings are equal <FAILED>. Expected [B] but found [A]
Test2: Ensure strings are equal <PASSED> 

java.lang.AssertionError: The following asserts failed:
    Test1: Ensure strings are equal expected [B] but found [A]
Expected :B
Actual   :A
 <Click to see difference>


    at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:47)
    at com.rationaleemotions.stackoverflow.qn55387064.NewSoftAssertTest.myTest(NewSoftAssertTest.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:131)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:570)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:170)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:790)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:143)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at org.testng.TestRunner.privateRun(TestRunner.java:763)
    at org.testng.TestRunner.run(TestRunner.java:594)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:398)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:392)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
    at org.testng.SuiteRunner.run(SuiteRunner.java:304)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1146)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1067)
    at org.testng.TestNG.runSuites(TestNG.java:997)
    at org.testng.TestNG.run(TestNG.java:965)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:73)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)


===============================================
Default Suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================