对于回归测试(不是单元测试),我们有用 TestNG 编写的详细场景,是否有适当的位置应该进行断言检查?它是在测试用例中还是在调用方法中是否重要?例如:
此测试用例调用包含断言的验证方法:
@Test
public void test1() {
validateResponse();
}
public void validateResponse() {
Assert.assertEquals(a, "123");
Assert.assertEquals(b, "455");
Assert.assertEquals(c, "5678");
Assert.assertEquals(d, "3333");
}
该测试用例根据验证方法的返回值进行断言:
@Test
public void test1() {
Assert.assertTrue(validateResponse());
}
public boolean void validateResponse() throws Exception {
try {
if (!a.equals("123")) throw new Exception();
if (!b.equals("455")) throw new Exception();
if (!c.equals("5678")) throw new Exception();
if (!d.equals("3333")) throw new Exception();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
最佳答案
你的断言应该尽可能具体和细致,以帮助开发人员快速识别问题。例如
@Test
public void testResponseFields(){
// create response to be tested
// JUnit style
Assert.assertEquals("Response 'alpha' should be '123'", 123, response.getAlpha());
// TestNG style
Assert.assertEquals(response.getAlpha(), 123, "Response 'alpha' should be '123'");
}
一旦您在 Assert.assertXX 调用中设置了失败消息,关于在何处调用 Assert 就变得更有争议了,因为您将收到一条解释问题的消息和一个堆栈跟踪以查看失败的位置和时间.
关于java - 断言应该放在测试用例中还是验证方法中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36362472/