易百教程

14、软断言和硬断言有什么区别?

Soft Assertion:在Soft Assertion的情况下,如果TestNG在@Test过程中得到错误,当断言失败时会抛出异常,继续assert语句之后的下一条语句。
Hard Assertion:在 Hard Assertion 的情况下,如果 TestNG 在 @Test 过程中遇到错误,它会在断言失败时立即抛出 AssertException 并在 assert 语句之后停止执行。
让我们通过一个例子来理解。

package com.yiibai;  
import org.testng.Assert;  
import org.testng.annotations.Test;  
import org.testng.asserts.SoftAssert;  
public class Assertion {  
    SoftAssert soft_assert=new SoftAssert();  
    @Test  
    public void Soft_Assert()  
    {  
     soft_assert.assertTrue(false);  
     System.out.println("soft assertion");  
    }  
    @Test  
    public void Hard_Assert()  
    {  
     Assert.assertTrue(false);  
     System.out.println("hard assertion");  
    }  
}