JAVA单元测试框架-10-断言Assert
程序员文章站
2022-06-19 21:17:35
...
使用Assert对结果进行断言,这是testng里的断言方式
常用断言
- assertTrue
- assertSame
- assertEquals
@Test
public void TestAssert() {
Assert.assertEquals("2", 2);
}
@Test
public void TestAssertThree() {
int a=3;
Assert.assertNull(a);
}
@Test
public void TestAssertFour() {
String expected="预期结果";
String actual="实际结果";
Assert.assertSame(actual, expected,"实际结果跟预期结果不一样");
}
@Test
public void TestAssertW() {
Assert.assertTrue(1==2);
}
测试执行结果:
通过创建对象来断言:
@Test
public void TestAssertnew(){
System.out.println("Test start");
SoftAssert assertion = new SoftAssert();
assertion.assertEquals(12, 13,"两者不相等");
System.out.println("Test complete");
System.out.println(3+8);
assertion.assertAll();
}