问题描述
我在我的代码中使用了TestNG软断言,如。
I'm using TestNG soft assertions in my code like.
public class AssertionTest{
private SoftAssert softAssert = new SoftAssert();
@Test(enabled = true)
public void testSoftAssertion(){
softAssert.assertTrue(false);
softAssert.assertTrue(true);
softAssert.assertEquals("India", "US");
softAssert.assertAll();
}
}
当测试执行完成时测试失败(按预期)但是结果不提供详细信息,而是提供如下信息,这无助于了解哪些断言失败。
When test execution completes test fails(as expected) but result doesn't give details info, rather it gives info like following which doesn't help to understand which asserts failed.
FAILED: testSoftAssertion
java.lang.AssertionError: The following asserts failed:
null, null
我期待输出有助于理解结果的东西(当我们使用硬断言时生成这种类型的输出,即使用 Assert
类)。
I'm expecting output something which will help to understand the result(This type of output is generated when we use hard assertion i.e with Assert
class).
FAILED: testSoftAssertion
java.lang.AssertionError: The following asserts failed:
expected [true] but found [false]
expected [India] but found [US]
这是否已知缺陷/缺点使用TestNG软断言还是有一些我不知道的东西?
Is this known defect/drawback with TestNG soft assertion or there is something, which I'm missing?
推荐答案
你需要在屁股时提供失败信息只有这样它才会在报告中占据。请考虑以下代码段:
you need to provide the failure message while asserting only then it will take up in the report. Consider below code snippet:
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class SoftAsert
{
@Test
public void test()
{
SoftAssert asert=new SoftAssert();
asert.assertEquals(false, true,"failed");
asert.assertEquals(0, 1,"brokedown");
asert.assertAll();
}
}
输出:
FAILED: test
java.lang.AssertionError: The following asserts failed:
failed, brokedown
这就是软断言的工作方式。目的是在出现故障时显示错误消息。如果这有帮助,请告诉我。
That is how the softassertions are suppose to work. The intent is to display the error message in case of failure. Let me know if this helps.
这篇关于TestNG软断言输出不全面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!