27.07.2019»»суббота

Testng Run Test Class In Order

27.07.2019
    4 - Comments

TestNG test groups and it’s usage are explained in detailed in this post.

  1. Testng Preserve Order
  2. How To Run Selected Test Cases In Testng
TestNG test groups Usage:
  • Used for grouping of test methods
  • These groups allows you to segregate test methods into sets and then runs different set of test methods back to back without recompiling the code.
  • Specify group names in @Test annotation to make the test method as part of specific group.
    • In the above code two types of test methods are available
    • Sanity test methods should be run when doing the sanity test after the build is deployed to make sure that no basic functionality is broken.
    • Regression test methods should be run to make sure all the scenarios are covered.
    • In the above test method “sample1” executes when invoked with group “sanity”.
    • Test methods “sample1” & “sample2” executes when invoked with group “regression”.
    • We can specify any number of group names on test method. That means a method can belong to multiple groups like “sample1” above.
  • Specify groups in testng.xml file <test> tag
    • When invoked the above testng.xml file, it will run method “sample1” only.
Groups with regular expression
  • Groups that are specified in testng.xml file supports regular expressions.
    • To run specific tests on Firefox browser and specific tests in IE browser. Below is the example
  • Following is the xml file to run Firefox test methods
  • Here “firefox.*” means it includes test methods of groups “firefox.sanity” & “firefox.regression”.
Test
Groups Exclusion
  • TestNG allows to exclude the groups.
  • Specify <exclude> tag with group name in <groups> tags as below
  • Sometimes we can exclude the test methods in the execution process using exclude strategy in groups. This might occur when some tests hae to be fixed and some tests are getting failed in the execution process.
    • To do this, create a new group “broken” and then add to the methods to make these failed test methods belongs to it.
    • For example in the above test method “sample2” is broken, so to disable it, we can do like this.
  • As discussed above, test methods “sample1” & “sample2” belongs to “regression” group.
  • But when executed the above xml file with exclude group, it will run only “sample1” test method.

Preserver Order in Testng. In TestNg bydefault the preserve-order attribute will be set to 'true', this means, TestNG will run your tests in the order they are found in the XML file. We will try to execute the below example, by taking three classes. For the first one, We will set the preserve-order attribute to false and check for the the Output. In this tutorial, we will show you how to run multiple TestNG test cases (classes) together, aka suite test. Test Classes. Review following three test classes.

Groups of Groups
  • Groups can include other Groups as well, these groups are called Meta Group
  • Following is the complete class that has test methods with groups, you can use directly by just copying it.
  • You can define a group “all-sanity” that includes “sanity”, “firefox.sanity” and “ie.sanity”. Below is the complete xml file named as “groups.xml“.
  • Below is the result when executed groups.xml file.
    • It executes the test methods “sample1”, “testWithFirefox” & “testWithIE” as those are belonged to “sanity”, “firefox.sanity” & “ie.sanity” respectively.
Summary:

In this post, we learned Test Groups, how to partition tests for execution, how to include and exclude groups & how to use groups with in other groups.

Goal: To Run TestNG using Command Prompt:

Steps to achieve our goal – Executing TestNG using Command Prompt:

  1. Open Eclipse and create a Java class
  2. Write a Java program
  3. Convert the Java Program into TestNG
  4. Open command prompt
  5. Run the TestNG using command prompt

Here is a video tutorial to learn “How To Run TestNG Using Command Prompt”:

Please be patient. The video will load in some time.

Step i: Open Eclipse and create a Java class

Step ii. Keep all the library files in a folder (here I create a folder name “lib”)

Step iii: Write a Java program

Step iv: Convert the Java Program into TestNG

Testng Preserve Order

Step v: Open command prompt

Step vi: Run the TestNG using command prompt

set classpath=C:UsersAdminDesktopSTMSeleniumTutorialworkspaceSoftwareTestingMaterialbin;C:UsersAdminDesktopSTMSeleniumTutorialworkspaceSoftwareTestingMateriallib*
2
4
6
8
set projectLocation=C:UsersAdminDesktopSTMSeleniumTutorialworkspaceSoftwareTestingMaterial
cd%projectLocation%
set classpath=%projectLocation%bin;%projectLocation%lib*
java org.testng.TestNG%projectLocation%testng.xml
pause

You could find the complete TestNG tutorial here.

If you are not a regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.

SUBSCRIBE TO GET FREE EBOOK AND REGULAR UPDATES ON SOFTWARE TESTING