Sunday, July 7, 2024

Assertions in TestNG

 Assertions:

Types of Assertions:

1.Hard Assertions

2.Soft Assertions

1.Hard Assertions:

if given assert statement fails, it stops program execution there it self,  it will not execute any stmts after that and also it fails test method.

Assert is predefined class in TestNg, which has different  methods, can be used to assert the given cond is true / false

 

Methods in "Assert" Class: 

1.Assert.assertEquals(actual, expected):

Compares if the actual value is equal to the expected value.

 

2.Assert.assertNotEquals(actual, expected):

Compares if the actual value is not equal to the expected value.

 

3.Assert.assertTrue(condition):

Checks if the given condition is true.

 

4.Assert.assertFalse(condition):

Checks if the given condition is false.

 

5.Assert.assertNull(object):

Checks if the given object is null.

 

6.Assert.assertNotNull(object):

Checks if the given object is not null.

 

ex:

package TestNGAsserionsBasics;

import org.testng.Assert;

import org.testng.annotations.Test;

public class AssertionsBasics

{

            @Test

            public void testA()

            {

                        System.out.println("test A - stmt-1");

                        // assert "ram" and "ram" are equal or not

                                                                                    //  passed                  

                        Assert.assertEquals("ram", "ram");//Pass

                       

                        //   assert  "Ram"  and "sita" are equal

                        Assert.assertEquals("ram", "sita");

                        //

                        //  false-- Fail  this stmt

                       

                        // assert statement -failed

                        //  if assert statement fails, it stops program execution there itself,

                        //it wont execute any next stmt,

                        /// it will fail the test method also

                        //java.lang.AssertionError: expected [sita] but found [Ram]

                        System.out.println("test A - stmt-5");

            }

}

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

test A - stmt-1

FAILED: testA

java.lang.AssertionError: expected [sita] but found [Ram]

            at org.testng.Assert.fail(Assert.java:97)

            at org.testng.Assert.assertEqualsImpl(Assert.java:136)

            at org.testng.Assert.assertEquals(Assert.java:118)

            at org.testng.Assert.assertEquals(Assert.java:575)

            at org.testng.Assert.assertEquals(Assert.java:585)

            at TestNGBasics.AssertionsBasics.testA(AssertionsBasics.java:21)

            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)

            at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

            at java.base/java.lang.reflect.Method.invoke(Method.java:564)

            at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)

            at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)

            at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)

            at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)

            at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)

            at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)

            at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)

            at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)

            at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

            at org.testng.TestRunner.privateRun(TestRunner.java:770)

            at org.testng.TestRunner.run(TestRunner.java:591)

            at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)

            at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)

            at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)

            at org.testng.SuiteRunner.run(SuiteRunner.java:304)

            at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)

            at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)

            at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)

            at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)

            at org.testng.TestNG.runSuites(TestNG.java:1032)

            at org.testng.TestNG.run(TestNG.java:1000)

            at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)

            at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)

            at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

===============================================

    Default test

    Tests run: 1, Failures: 1, Skips: 0

===============================================

===============================================

Default suite

Total tests run: 1, Passes: 0, Failures: 1, Skips: 0

===============================================

ex2:

Compare boolean values:

assertEquals("ram","ram")  - can be used to compare String values, boolean values, int values...etc

assertEquals(true, false);

assertEquals(10, 10); //  ex of Method over loading

assertTrue(true)

assertFalse(false)

 

package TestNGAsserionsBasics;

import org.testng.Assert;

import org.testng.annotations.Test;

public class AssertionsBooleanValues1 {

           

            @Test

            public void testA()

            {

                        System.out.println("test A - stmt-1");

                        // check true, true are equal

                        // Method Over loading

                        Assert.assertEquals(true, true);

                        System.out.println(" assertEquals(true, true) ");

                       

                        // Pass

                       

                        //- ve assert true, false

//                     Assert.assertEquals(true, false);

                       

                        // not equals --  Fail -- stops program execution

                        //  Fail test method name - i.e testA()           

                       

                        //  if cond is false - it throws AssertionError

                        // java.lang.AssertionError: expected [false] but found [true]

                       

                       

                        // HW Assert given 10 and 10 values are equal or not

                       

//                    HW Assert given 10 and 20 values are equal or not

                       

                        //****************************************************

                        // 2.assertTrue(true) -can be used to  assert given value is true

                        //  condi - pass

                        Assert.assertTrue(true);

                                    //  given value is true --  Passed

                       

                        //

//                     Assert.assertTrue(false);// given value is not true --Fail

                        //                                 Assert.assertTrue(false); //  condition - false

                        // it stops progr execution - it wont execute next stmt

                        //  it throws AssertionError

                        //java.lang.AssertionError: did not expect to find [true] but found [false]

                        //   also it fails test method

                        // 3 assertFalse() can be used to check given value  is false

                        Assert.assertFalse(false);

                        System.out.println("Asserting false");

                                               

                        //  given value is false-Passed

                       

                                    // -ve

//                     Assert.assertFalse(true);

                        // cond - false -- stops progr execution here - it wont execute next stm

                        // java.lang.AssertionError: did not expect to find [false] but found [true]

                        // 4  AssertNull()

                        System.out.println("Asserting null");

                        String s = null;

                        // assert the given value is null

                        Assert.assertNull(s);

                        //                 null

                        //                pass

                        //                null -- cond - is true -- it goes to next statement and it executes

                       

                       

                        s  = "Raju";

                        // assert the given value is null

//                     Assert.assertNull(s);

                       

                        //                Raju

                        //          Fail -

                        ///                 "Raju" --   cond is false -    stops , throws assertion error and it wiil not execute next smt

                        //                                 FAILED: testA

                        //                                 java.lang.AssertionError: expected [null] but found [Raju]/

                        // Note :  it will fail test method  if assertion statement is failed

                       

                        System.out.println("test A - stmt-5");

            }

}

                       

// HW Assert 2 int values

// Hw  Assert 2 decimal  values

// HW Assert 2 char values

// HW Assert 2 arrays

 

Assert statement with 3 args :

Assert.assertEquals(actual, expected, "Expected msg"): --MOL

Compares if the actual value is equal to the expected value and provides a custom message if the assertion fails.

 

package TestNGAsserionsBasics;

import org.testng.Assert;

import org.testng.annotations.Test;

public class AssertMethodsWith3Args {

            @Test

            public void testA()

            {

                        System.out.println("test A - stmt-1");

                        //assertEquals (str1, Str2, "Expected msg")

//             assert ram ram

                        Assert.assertEquals("ram", "ram", "Given Values are not Equals.");

// if given condition is true, it will not display the given expected message in output

 

// assert ram and sita and display some message if condition fails

 Assert.assertEquals("ram", "sita", "Given Values are not Equal.");

 // if given condition fails, it throws the given expected message in the console window // java.lang.AssertionError: Given Values are not Equal expected [sita] but found [ram] }

 

                        // assertTrue with 2 args  - 2nd arg - we can pass some msg

                        //     5>3

                                   

                        Assert.assertTrue(true, "Check given value is not true");

                                                // true

                        //                 true 

                        // 5<3

                        Assert.assertTrue(false, "Check given value is not true");

                        //                false

                        //                     java.lang.AssertionError:  Cond is false  did not expect to find [true] but found [false]

                        // HW AssertFalse with 2 args

                        // HW assertNotEquals   with 2 args

                        // HW assertNotEquals   with 3 args

                        //                     Assert.assertNotEquals(0, 0, 0);

                        // HW AssertNotNull  with 2 args

                        // Note : All the methods in Assert class are 'static' method

                        //                       static methods will be called by class name

                        // ex:   className.staticMethod();

                        //         Assert.assertTrue();

                        System.out.println("test A - stmt-5");

            }

}

 

2.Soft Assertions : 

Hard Assertion: If a hard assertion statement fails, it stops program execution and does not execute the next statement. This will also fail the test method. For example, Assert class in TestNG.

 

Soft Assertion: Even if an assert statement fails, it does not stop program execution and proceeds to the next statement.

 

package TestNGAsserionsBasics;

import org.testng.annotations.Test;

import org.testng.asserts.SoftAssert;

public class SoftAssertBasics1 {

            @Test

            public void testA()

            {

                        System.out.println("test A - stmt-1");

                        //

                        // create obj for SoftAssert -class --> check 2 strings are equal or not  Ram   or Sita

                       

                        //  assert Ram and Ram are equal

                        SoftAssert sa =  new SoftAssert();

                        sa.assertEquals("Ram", "Ram");

                                          // Passed

                        //  assert Ram and Sita are equal

                        sa.assertEquals("Ram", "Sita");

                        //                cond is false --  Fail stmt

                        // even if soft assert statement fail,  it does not stop program execution , 

                        //it does not throw Exception : AssertionError

                        //                     and   it is  passing the test method   (no failing)

                        System.out.println("test A - stmt-5 got executed even if assertion failed");

            }

}

           

o/p:

 [RemoteTestNG] detected TestNG version 7.0.0

test A - stmt-1

test A - stmt-5

PASSED: testA

===============================================

    Default test

    Tests run: 1, Failures: 0, Skips: 0

===============================================

===============================================

Default suite

Total tests run: 1, Passes: 1, Failures: 0, Skips: 0

===============================================

ex2: 

The sa.assertAll(); statement at the end will collect all assertion results and report any failures, causing the test method to fail if any assertions did not pass. It ensures that all errors are displayed even if the test method fails.

 

package TestNGAsserionsBasics;

import org.testng.annotations.Test;

import org.testng.asserts.SoftAssert;

public class SoftAssertBasics1 {

            @Test

            public void testA()

            {

                        System.out.println("test A - stmt-1");

                        // Hard assertion - if cond is fail, it stops program  execution and it wont execute next stmt,

                        //  and also it fails test method

                        //                     ex: Assert - class

                        // Soft assertion : even if condition is fail or false ,  it does not stop program execution and 

                                    // goes to next stmt

                        //  can be used to validate condition

                        // create obj for SoftAssert -class --> check 2 strings are equal or not  Ram   or Sita

                       

                        SoftAssert  sa =  new SoftAssert();

                        //  assert Ram and Ram are equal

                        sa.assertEquals("ram", "ram");// Passed

                        //  assert Ram and Sita are equal

                        sa.assertEquals("ram", "sita");//  False/ Fail

                        //  cond -is false

                        // even if soft assert statement fail,  it does not stop program execution ,  it does not throw Exception : AssertionError

                        //                     and   it is  passing the test method   (no failing)

                       

                       

                        // assert all error

//                     sa.assertAll();

                                                 // dont write here assertAll()

                                                // after assertall, it wont execute any statement

                                               

                                                System.out.println("test A - stmt-5 got executed even if assertion failed");

                                                // If we want  to display all exception in o/p  and fail test method, call sa.assertAll();

                                               

                                                 //   must be the last stmt

                                                // prefer to write at the end

                                                sa.assertAll();

                                                //  to display all errors and fail test method

                                               

                                                System.out.println("ends ");// after assertall() - it wont execute any stmt

            }

}

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

test A - stmt-1

test A - stmt-5 got executed even if assertion failed

FAILED: testA

java.lang.AssertionError: The following asserts failed:

            expected [sita] but found [ram]

            at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:47)

            at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:31)

            at TestNgBasics2.SoftAssertionsBasics1.testA(SoftAssertionsBasics1.java:31)

            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)

            at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

            at java.base/java.lang.reflect.Method.invoke(Method.java:564)

            at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)

            at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)

            at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)

            at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)

            at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)

            at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)

            at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)

            at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)

            at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

            at org.testng.TestRunner.privateRun(TestRunner.java:770)

            at org.testng.TestRunner.run(TestRunner.java:591)

            at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)

            at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)

            at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)

            at org.testng.SuiteRunner.run(SuiteRunner.java:304)

            at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)

            at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)

            at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)

            at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)

            at org.testng.TestNG.runSuites(TestNG.java:1032)

            at org.testng.TestNG.run(TestNG.java:1000)

            at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)

            at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)

            at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

===============================================

    Default test

    Tests run: 1, Failures: 1, Skips: 0

===============================================

===============================================

Default suite

Total tests run: 1, Passes: 0, Failures: 1, Skips: 0

===============================================

Note:

  • assertAll() throws all assertion errors in the console window and fails the test method.
  • assertAll() must be the last statement in the test method.
  • After assertAll(), if there are any statements, nothing will be executed.

ex3:

public class SoftAssertionsBasics1

{

            @Test

            public void testA()

            {

                        System.out.println("test A - stmt-1");

           

                        // HW Define soft assertions  -sa.assertTrue

// HW  assertTrue with 2 args

//HW AssertFalse with1 args

            // HW Define soft assertion -assertFalse with 2 args

                       

//HW AssertNull with 1 args

//HW AssertNull with 2 args

//HW AssertNoTNull with 1 args

//HW AssertNotNull with 2 args

//HW assertNotEquals with 1 args

//HW AssertNotEquals with 2 args

           

                        System.out.println("test A - stmt-5 got executed even if assertion failed");

           

                       

            }

           

}

o/p:

FAQ : Tell me some hard assertions?

  Class -  Assert

 Methods:   assertEquals(), AssertNotEqual(), AssertTrue,AssertFalse, null - Assertnull()

                pass 2 / 3 args

 

FAQ: Difference  between Hard Assertion and soft assertion?

1. Hard assertion :

·  If a hard assertion statement fails, it stops program execution immediately and does not execute any subsequent statements.

·  It throws an AssertionError.

·  This failure causes the test method to fail.

·  Class: Assert

·  In the Assert class, all methods are static methods.

 

2. SoftAssert :

·  If a soft assertion statement fails, it does not stop program execution and continues to the next statement.

·  It does not throw an exception in the output and does not fail the test by default.

·  To see exceptions in the output and fail the test method, call assertAll().

·  Class: SoftAssert

·  In the SoftAssert class, all methods are non-static, so an object of the SoftAssert class needs to be created.

 

Certainly! Here is a table summarizing the differences between Hard Assertions and Soft Assertions:

Feature

Hard Assertion

Soft Assertion

Behavior on Failure

Stops program execution immediately

Continues program execution

Exception Thrown

Throws AssertionError

Does not throw an exception immediately

Effect on Test Method

Causes the test method to fail immediately

Does not cause the test method to fail immediately

Execution of Subsequent Statements

Subsequent statements are not executed

Subsequent statements are executed

Class

Assert

SoftAssert

Method Type

All methods are static

All methods are non-static

Object Creation

No need to create an object (static methods)

Need to create an object (non-static methods)

Method to Report All Failures

Not applicable

assertAll() must be called to report all failures

Example Use Case

Assert.assertEquals("expected", "actual");

SoftAssert sa = new SoftAssert(); sa.assertEquals("expected", "actual"); sa.assertAll();

Error Message Display

Displays error message immediately on failure

Displays error message after calling assertAll()

Use Case

Useful for critical checks where subsequent statements should not be executed if the assertion fails

Useful for non-critical checks where you want to perform all checks and report all failures at once

 

 

FAQ: when Should We go For hard assertion and Soft Assertion ?

Use hard assertions (e.g., Assert.assertEquals(), Assert.assertTrue()) when you want to stop the test execution immediately upon encountering a failure.

When a hard assertion fails, TestNG will mark the test method as failed, and any subsequent code in the test method will not be executed.

Hard assertions are useful when the failure of a particular assertion makes it pointless to continue with the remaining test steps, and you want to fail fast.

 

Soft Assertions:

Use soft assertions (e.g., TestNG's SoftAssert class) when you want to collect multiple assertion failures during the execution of a test method and continue executing subsequent test steps.

Soft assertions allow you to accumulate all the assertion failures and report them at the end of the test method rather than failing the test immediately.

Soft assertions are useful when you want to gather information about all the issues in the test before marking the test as failed.

Note:

 use hard assertions when you want to fail fast and stop the test on the first assertion failure, and use soft assertions when you want to continue with the test execution and collect multiple assertion failures to report them all at the end of the test method.

Assertions ==  if cond stmt

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...