TestNg assertions do you really know

Foreword

In the implementation of automated test scripts, we need to automatically determine the actual results after the completion of the test script execution is consistent with the expected results, this time on the need to write assertions before the program is run to determine whether it is normal after the current program execution.

About TestNG assert divided into two types:

  • Soft assertion
  • Hard assertion

Hard assertion

In TestNg in, Assert class is hard to assert, there are multiple static method called hard asserted, characterized by, if the script assertion fails, stop running immediately behind the code will not be executed.

TestNG provided in plurality assert * () method, mainly match the different types of data objects and collections, and other operations.

For example as follows:

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestAssert {

    @Test
    public void testAssert() throws Exception {
        Assert.assertEquals(4, 2 * 2);
        Assert.assertEquals(5,1+9);
    }
}

Running the above code, the following results:

From the above it can be seen in assertEquals Expected Actual unequal and, if the actual result and the expected results are not equal, it will throw an exception asserted and display the contents, such errors are thrown, more easily locate the error cause and specific business logic.

  • About assert common assertion methods and described as follows:
  • assertTrue: to determine whether to True.
  • assertFalse: determine whether false.
  • assertSame: to determine whether the address is the same references.
  • assertNotSame: determining whether the referenced address is not the same.
  • assertNull: judgment is null.
  • assertNotNull: determine whether the is not null.
  • assertEquals: determining whether the same, need to achieve the object of type Object and haseCode equals method.
  • assertNotEquals: determining whether or not equal.
  • assertEqualsNoOrder: ignore the order is determined whether the same

Soft assertion

In TestNg in, SoftAssert class soft assertion, is characterized by an assertion failure if run, will not stop running, it will continue to execute other statements or assertions in this assertion, it does not affect the operation of other assertions.

Instructions: assertAll () must be placed in a final test of the assertion class behind the assertion of the soft type, called SoftAssert.java, this class is the need to create an instance of the object before calling an instance method associated soft assertion.

For example as follows:

Import org.testng.annotations.Test;
 Import org.testng.asserts.SoftAssert; 

public  class TestSoftAssert { 

    @Test 
    public  void testSoftAssert () { 
        System.out.println ( "script execution start .......... ........ " );
         // instantiate SoftAssert 
        SoftAssert the assertion = new new SoftAssert (); 
        assertion.assertEquals ( . 5,. 6," we were not as large " ); 
        System.out.println ( " script execution end .................. " ); 
        System.out.println ( " I was watching, that this will not be executed " );
         // this must put the final ,nothing to say
        assertion.assertAll();
    }
}

Running the above code, the following results:

It was found that by running, you can see after the assertion of equal 5 and 6 lines of code ,, there are other statements, if here is hard asserted, then the back of the " script execution ended" and "I'm waiting to see, to this will not be executed " is not output

That is, these two statements are not output. This is the advantage of SoftAssert, Again, be sure to call the last assertAll () method.

Guess you like

Origin www.cnblogs.com/longronglang/p/11276219.html