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

问题描述 投票:0回答:1

在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?

testng
1个回答
1
投票

为了确保您获得为已通过和失败的断言记录的消息,您可以执行类似的操作(我使用的是最新发布的TestNG版本,即今天,7.0.0-beta3):

通过扩展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
===============================================
© www.soinside.com 2019 - 2024. All rights reserved.