Monday, July 1, 2024

@Test with priority

 @Test with priority:

By using testNG, We can run test methods based on priority numbers

            // Define Test method with priority = 0

            // Define priority:    first it executes test method  priority no = 0 , 1 , 2

 

package TestNgBasics1;

import org.testng.annotations.Test;

public class TestMethodWithPrioriy {

            // Define priority:    first it executes test method  priority no = 0 , 1 , 2

                                    // Define Test method with priority = 0                    

                                    @Test(priority = 0)

                                    public void login()

                                    {

                                                System.out.println("Login  - priority= 0");

                                    }

                                    //// Define Test method with priority = 1

                                    @Test(priority = 1)

                                    public void createOrder()

                                    {

                                                System.out.println("createOrder  priority =1");

                                    }

                                   

                                    // Define Test method with priority = 2

                                    @Test(priority = 3)

                                    public void deleteteOrder()

                                    {

                                                System.out.println("deleteteOrder priority =2 ");

                                    }

                                   

}

 

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

Login  - priority= 0

createOrder  priority =1

deleteteOrder priority =2

PASSED: login

PASSED: createOrder

PASSED: deleteteOrder

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

    Default test

    Tests run: 3, Failures: 0, Skips: 0

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

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

Default suite

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

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

Note :

Even if we change the order of test methods in the class, TestNG always executes test methods based on priority numbers only (0, 1, 2, etc.).

 

ex:

package TestNGBasics1;

import org.testng.annotations.Test;

public class TestMethodWithPrioriy

{

            // Define priority:    first it executes test method  priority no = 0 , 1 , 2

            // Define Test method with priority = 2

            @Test(priority = 2)

            public void deleteteOrder()

            {

                        System.out.println("deleteteOrder priority =2 ");

            }

            // Define Test method with priority = 0                    

            @Test(priority = 0)

            public void login()

            {

                        System.out.println("Login  - priority= 0");

            }

            //// Define Test method with priority = 1

            @Test(priority = 1)

            public void createOrder()

            {

                        System.out.println("createOrder  priority =1");

            }

}

o/p:

Same as previous program

 

Default Priority for @Test:

 

package TestNGBasics1;

import org.testng.annotations.Test;

public class TestMethodWithPrioriy2

{

            // Define priority:    first it executes test method  priority =0 , 1 , 2

//         @Test(priority =0)

            @Test  //   default priority  = 0  even if we don't define any priority -

            public void login()

            {

                        System.out.println("Login  - priority= 0");

            }

            @Test(priority =1)

            public void createOrder()

            {

                        System.out.println("createOrder  priority =1");

            }

           

            @Test(priority =2)

            public void deleteteOrder()

            {

                        System.out.println("deleteteOrder priority =2 ");

            }

           

           

}

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

Login  - priority= 0

createOrder  priority =1

deleteteOrder priority =2

PASSED: login

PASSED: createOrder

PASSED: deleteteOrder

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

    Default test

    Tests run: 3, Failures: 0, Skips: 0

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

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

Default suite

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

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

FAQ What is the default priority no for Test methods?

   priority = 0

 

FAQ Can we give -ve numbers in priority ? -2,-1 , 0 ,1,2

We can assign -ve numbers also  priority number 

Testng executes Test methods with priority number   -2,-1,0,1,2..etc

 

ex:      

package TestNgBasics1;

import org.testng.annotations.Test;

public class TestMethodWithPriority3 {

            // Define Test method with priority = -2

                        @Test(priority = -2)

                        public void login()

                        {

                                    System.out.println("Login  - priority= -2");

                        }

                        // Define Test method with priority = -1

                        @Test(priority = -1)

                        public void createOrder()

                        {

                                    System.out.println("createOrder  priority = -1");

                        }

                        // Define Test method with priority = 0

                        @Test(priority =  0)

                        public void deleteteOrder()

                        {

                                    System.out.println("deleteteOrder priority = 0 ");

                        }

                        // Define Test method with priority = 1

                        @Test(priority = 1)

                        public void deleteteOrder2()

                        {

                                    System.out.println("deleteteOrder priority = 1 ");

                        }

}

           

           

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

Login  - priority= -2

createOrder  priority = -1

deleteteOrder priority = 0

deleteteOrder priority = 1

PASSED: login

PASSED: createOrder

PASSED: deleteteOrder

PASSED: deleteteOrder2

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

    Default test

    Tests run: 4, Failures: 0, Skips: 0

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

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

Default suite

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

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

Note:

When we have 2 (or) 3 Test methods have same priority, then it executes Test methods names in "alphabetical" order.

 

package TestNgBasics1;

import org.testng.annotations.Test;

public class TestMethodWithPriority4 {

            @Test(priority =0) //

            public void login()

            {

                        System.out.println("Login  - priority= 0");

            }

            @Test(priority =0)  //

            public void createOrder()

            {

                        System.out.println("createOrder  priority = 0");

            }

           

            @Test(priority =0) //

            public void deleteteOrder()

            {

                        System.out.println("deleteteOrder priority = 0 ");

            }

           

            @Test(priority =1)  // 4

            public void deleteteOrder2()

            {

                        System.out.println("deleteteOrder priority = 1 ");

            }

}

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

createOrder  priority = 0

deleteteOrder priority = 0

Login  - priority= 0

deleteteOrder priority = 1

PASSED: createOrder

PASSED: deleteteOrder

PASSED: login

PASSED: deleteteOrder2

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

    Default test

    Tests run: 4, Failures: 0, Skips: 0

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

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

Default suite

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

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


Convert classes to testNG.xml file and Run TestNG.xml

Convert classes to  testNG.xml file :

Steps:

Select some desired 2 or 3 classes > right click > TestNg >   Convert to TestNG > Click next  >  testng.xml got created in Project Level

open testng.xml : --> Click 'Source' tab at the bottom of the page if below code is not avaialble

--------------

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Suite">

  <test thread-count="5" name="Test">

    <classes>

      <class name="TestNgBasics1.TestAnnonation2"/>

      <class name="TestNgBasics1.AfterTestAnnonBasics"/>

    </classes>

  </test> <!-- Test -->

</suite> <!-- Suite -->

 

Run TestNg.xml file :

Right click in xml file  >   run as testNG suite  >   it runs  given classes in sequential order

TestAnnonation2

AfterTestAnnonBasics

 

o/p:

 [RemoteTestNG] detected TestNG version 7.0.0

stmt-1 from testA()

stmt-2 from testA()

stmt-1 from testA()

stmt-1 from testB()

@AfterTest gets executed only once after executing @Test Method

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

Suite

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

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

 

By using TestNG.xml file , we can run single class (or) multiple classes.

 

HW Convert 3 classes to testng.xml  and Run it ?

<class name="TestNgBasics.BeforeTestAnnotations2"/>

name =  Packagename.className1

            Packagename.className2

            Packagename.className3

Run package:

Replace  'class'  --> package in below xml file

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Suite">

  <test thread-count="5" name="Test">

 

  <!--   <classes>

                              <class name="TestNgBasics1.TestAnnonation2"/>

                              <class name="TestNgBasics1.AfterTestAnnonBasics"/>

    </classes> -->

   

     <packages>

                              <package name="TestNgBasics1"/>

                             

    </packages>

   

   

  </test> <!-- Test -->

</suite> <!-- Suite -->

Run as testNG :

2 ways to run Package:

1. through Testng.xml file

            Right click Run as test NG suite

runs all classes inside that package

2. go to Package >Rt click > Run as Test Ng Test

 

Create TestNG.xml file manually:

Steps:

  1. Select Project
  2. Right-click > New > Other
  3. Type xml in the search box
  4. Under XML, select 'XML File' > Next
  5. Give a meaningful name: testNGManual.xml
  6. The testNGManual.xml file is created at the project level

write below tags

<?xml version="1.0" encoding="UTF-8"?>

<suite >

            <test>

                        <classes>

                                    <class name="TestNgBasics1.TestAnnonation2" ></class>

                       

                        </classes>      

           

            </test>

</suite>

 

run as TestNG .xml

o/p:

 [RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

org.testng.TestNGException:

The <suite> tag must define the name attribute

            at org.testng.xml.TestNGContentHandler.xmlSuite(TestNGContentHandler.java:144)

           

-------------------

<?xml version="1.0" encoding="UTF-8"?>

<suite name= "My Suite">

            <test >

                        <classes>

                                    <class name="TestNgBasics1.TestAnnonation2" ></class>

                       

                        </classes>      

           

            </test>

</suite>

run testNg.xml

o/p:

org.testng.TestNGException:

The <test> tag must define the name attribute

 

------------------

<?xml version="1.0" encoding="UTF-8"?>

<suite name= "My Suite">

            <test name="My Test ">

                        <classes>

                                    <class name="TestNgBasics1.TestAnnonation2" ></class>

                       

                        </classes>      

           

            </test>

</suite>

 

run as tetsng.xnl file:

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

stmt-1 from testA()

stmt-2 from testA()

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

My Suite

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

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

Note :

Suite, test tags - we must define name attribute else it throws TestNGException

open xml file:

  click on testng.xml

testNG hierarchy :

Suite > test > Classes > Class

             > packages  > package

 

Run as testNG:

1. if we don't give package name --?

<?xml version="1.0" encoding="UTF-8"?>

<suite name= "My Suite">

            <test name="My Test ">

                        <classes>

                                    <class name="TestAnnonation2" ></class>

                       

                        </classes>      

           

            </test>

</suite>

 

Run :

org.testng.TestNGException:

Cannot find class in classpath: TestAnnotations1

Fix : 

 always give "Package.className"

 

1. Error2: if we give invalid class name :

<?xml version="1.0" encoding="UTF-8"?>

<suite name= "My Suite">

            <test name="My Test ">                                                                                                                                                                                                                                                           

                        <classes>

                                    <class name="TestNgBasics1.TestAnnonation21" ></class>

                       

                        </classes>      

           

            </test>

</suite>

 

run -->

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

org.testng.TestNGException:

Cannot find class in classpath: TestNgBasics1.TestAnnonation21

Fix :  check package name is correct or not

                 class name is correct or not

always Give Packagename.classname

----------------

Run as testNg :

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

stmt-1 from testA()

stmt-2 from testA()

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

My Suite

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

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

 

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

My suite

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

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

 

HW  create Xml file manually   and run any 2 classses?

FAQ : Can you tell me testNG.xml  file hierarchy  (or) structure of testng.xml file ?

            <suite>

                        <test>

                                    <classes>

                                                <class name= "pkg.class1" >   </class>

                                                <class name = "pkg.class2" />  //  this is also ok

                                    </classes>

                        </test>

            </suite>

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...