TestNG Prioritizing & Sequencing

TestNG Prioritizing & Sequencing

 轉自http://toolsqa.com/selenium-webdriver/testng-prioritizing-sequencing/ (需翻牆查看)

Multiple Tests

There will be situations when you want to put number of tests under a single test class and like to run all in single shot.  With the help of TestNG ‘@Test‘ annotations we can execute multiple tests in single TestNG file.

Take an example of four different tests under one testng class and print the test sequence on the console.

How to do it…

1) Press Ctrl+N , select “TestNG Class” under TestNG category and click Next.

Or

Right click on Test Case folder, go to TestNG and select “TestNG Class“.

TestNg-Muliple-0

2) If your project is set up and you have selected the Test Case folder before creating TestNG class then the source folder and the package name will be pre-populated on the form. Set class name as ‘TestNG‘.

Leave rest of the settings untouched, do not check for “@BeforeMethod”, “@AfterMethod” for now and click Finish. That’s it.

TestNG Prioritizing & Sequencing

3) By default a new class will have only one @Test method. Add two more methods by yourself and put your code accordingly in methods. Code will look like:

This will enable you to execute all four tests with just one testng class. Take a look on the output.

TestNg-Muliple-3

Attention: By default, methods annotated by @Test are executed alphabetically. Take a look over the next topic to see how to prioritize @Test.

 

Sequencing & Prioritizing

You need to use the ‘priority‘ parameter, if you want the methods to be executed in your order. Parameters are keywords that modify the annotation’s function.

Let’s take the same above example and execute all @Test methods in right order. Simply assign priority to all @Test methods starting from 0(Zero).

Note: TestNG will execute the @Test annotation with the lowest priority value up to the largest.

Output of the above:

TestNg-Muliple-4

Skipping a Test Case

Think of a situation where you are required to skip one or more @Test from your testng class. In testng, you can easily able to handle this situation by setting the ‘enabled’ parameter to ‘false’ for e.g.:

@Test(enabled = false)

To use two or more parameters in a single annotation, separate them with a comma:

@Test(priority = 3, enabled = false)

Again take the same example and set the value false for the third test.

Output of the above example:

TestNg-Muliple-5

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章