我在我的代码中使用 TestNG 软断言。
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();
}
}
当测试执行完成时,测试失败(如预期),但结果没有提供详细信息,而是提供如下信息,这无助于理解哪些断言失败。
FAILED: testSoftAssertion
java.lang.AssertionError: The following asserts failed:
null, null
我期望输出一些有助于理解结果的内容(这种类型的输出是在我们使用硬断言时生成的,即使用 Assert
类)。
FAILED: testSoftAssertion
java.lang.AssertionError: The following asserts failed:
expected [true] but found [false]
expected [India] but found [US]
这是 TestNG 软断言的已知缺陷/缺点还是有什么我遗漏的?
最佳答案
您需要在断言时提供失败消息,然后它才会出现在报告中。考虑下面的代码片段:
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
这就是软断言的工作原理。目的是在失败时显示错误消息。让我知道这是否有帮助。
关于java - TestNG 软断言输出不全面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29203274/