- Annotations.
- Run your tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc...).
- Test that your code is multithread safe.
- Flexible test configuration.
- Support for data-driven testing (with @DataProvider).
- Support for parameters.
- Powerful execution model (no more TestSuite).
- Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc...).
- Default JDK functions for runtime and logging (no dependencies).
- Dependent methods for application server testing.
TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc...
Benefits of using TestNG
TestNG is similar to JUnit but introduces some new features that make it more powerful and easier to use. Below are some of the benefits Appium Java users can enjoy with TestNG:
- More annotations: TestNG has an extensive choice of annotations. See a full list of annotations available in TestNG.
- Test Grouping: The
groups
attribute with the @Test annotation eases the grouping of test cases. - Test Prioritization: Using the
priority
attribute to set different priorities for each test case. - Parameterized Tests: Using parameterized tests makes it easy to run the same test over and over using different values.
- Parallel Testing: It’s possible and easy to parallelize test execution with the
parallel
attribute.
TestNG Annotations?
Annotations, in general, mean a comment or a note on a object, TestNG annotations are the code that is written inside source test code logic to control the flow of the execution of tests. It is essential to annotate your methods in TestNG to run the tests. TestNG will ignore the method which does not contain an annotation since it won’t know when to execute this method.
A TestNG annotation starts from the symbol @ and whatever follows is the annotation name. These names are predefined.
Types of TestNG Annotations:
In TestNG, there are #10 types of annotations:
- @BeforeSuite – The @BeforeSuite method in TestNG runs before the execution of all other test methods.
- @AfterSuite – The @AfterSuite method in TestNG runs after the execution of all other test methods.
- @BeforeTest – The @BeforeTest method in TestNG runs before the execution of all the test methods that are inside that folder.
- @AfterTest – The @AfterTest method in TestNG executes after the execution of all the test methods that are inside that folder.
- @BeforeClass – The @BeforeClass method in TestNG will run before the first method invokes of the current class.
- @AfterClass – The @AfterClass method in TestNG will execute after all the test methods of the current class execute.
- @BeforeMethod – The @BeforeMethod method in TestNG will execute before each test method.
- @AfterMethod – The @AfterMethod method in TestNG will run after each test method is executed.
- @BeforeGroups – The @BeforeGroups method in TestNG run before the test cases of that group execute. It executes just once.
- @AfterGroups – The @AfterGroups method in TestNG run after the test cases of that group execute. It executes only once.
Order of Annotations
Example of code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class TestNG { @Test public void testCase() { System.out.println("This is the Test Case"); } @BeforeMethod public void beforeMethod() { System.out.println("This will execute before every Method"); } @AfterMethod public void afterMethod() { System.out.println("This will execute after every Method"); } @BeforeClass public void beforeClass() { System.out.println("This will execute before the Class"); } @AfterClass public void afterClass() { System.out.println("This will execute after the Class"); } @BeforeTest public void beforeTest() { System.out.println("This will execute before the Test"); } @AfterTest public void afterTest() { System.out.println("This will execute after the Test"); } @BeforeSuite public void beforeSuite() { System.out.println("This will execute before the Test Suite"); } @AfterSuite public void afterSuite() { System.out.println("This will execute after the Test Suite"); } } |
Test Priority in TestNG
Although TestNG annotations decide in which order the tests will run, priorities do more or less the same job.
The priorities are an additional option that we can put to use with the test annotations. This attribute decides the priority of the annotation. But remember that priority check happens after the annotation check by TestNG. So the TestNG annotation hierarchy is followed first and then priority-based execution. The larger the priority number, the lower is its priority. So a method with priority 1 will run after the test with priority 0.
@Test(priority=1) public void b_method(){ System.out.println("B Method"); } @Test(priority=1) public void a_method(){ System.out.println("A method"); } |
TestNG Test suite:
TestNG does not let us define the test suites inside the test code or the main testing source code. Hence, we need to create a TestNG XML file for the same and execute this file.
How To Create A TestNG XML?
To create TestNG XML file for running the TestNG test suites, follow the given steps:
1.Right-click on the Project folder, go to New and select File as shown in the below image.
3. Finally, it will add a testng.xml file under your project folder, and we are all set to write our first TestNG XML to run TestNG test suites.
After that, add the below-given code in your testng.xml file.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="Test-Suite" > <test name="HelpingTesters" > <classes> <class name="testAndroid.FirstTestNG" /> </classes> </test> </suite> |
This TestNG XML code does not need any explanation. It is effortless to read and write.
- <suite> – The suite tag can be given any name and denotes the test suite name.
- <test> – The test tag can be given any name and indicates your test sets.
- <classes> – This is the combination of your package name and test case name and cannot write anything else.
How to Run the TestNG Suite?
Example:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="Test-Suite" > <test name="HelpingTesters" > <classes> <class name="testAndroid.FirstTestNG" /> <class name="testAndroid.AnotherTestNG" /> </classes> </test> </suite> Include/ exclude methods in testng.xmlHere we can ignore/ run out test cases by using Exclude/Include tags in xml file. <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="FirtSuite">
<test thread-count="5" name="TestSuite">
<classes>
<class name="testAndroid.Testng2">
<methods>
<include name="test4"></include>
<exclude name="test5"></exclude>
<include name="test6"></include>
</methods>
</class> </classes> </test> <!-- TestSuite -->
</suite> <!-- FirtSuite -->Output:
This is test case 6
===============================================
FirtSuite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
=============================================== |
No comments:
Post a Comment