- TestNG - TestNG - vs JUnit
- TestNG - Plug with Eclipse
- TestNG - Plug with ANT
- TestNG - Parallel Execution
- TestNG - Asserts
- TestNG - Annotation Transformers
- TestNG - Test Results
- TestNG - Run JUnit Tests
- TestNG - Parameterized Test
- TestNG - Dependency Test
- TestNG - Exception Test
- TestNG - Group Test
- TestNG - Ignore a Test
- TestNG - Suite Test
- TestNG - Executing Tests
- TestNG - Execution Procedure
- TestNG - Basic Annotations
- TestNG - Writing Tests
- TestNG - Environment
- TestNG - Overview
- TestNG - Home
TestNG Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
TestNG - Executing Tests
The test cases are executed using TestNG class. This class is the main entry point for running tests in the TestNG framework. Users can create their own TestNG object and invoke it in many different ways such as −
On an existing testng.xml.
On a synthetic testng.xml, created entirely from Java.
By directly setting the test classes.
You can also define which groups to include or exclude, assign parameters, etc. The command pne parameters are −
-d outputdir: specify the output directory.
-testclass class_name: specifies one or several class names.
-testjar jar_name: specifies the jar containing the tests.
-sourcedir src1;src2: ; separated pst of source directories (used only when javadoc annotations are used).
-target
-groups
-testrunfactory
-pstener
We will create the TestNG object an existing testng.xml in our example below.
Create a Class
Create a java class to be tested, say, MessageUtil.java in /work/testng/src.
/* * This class prints the given message on console. */ pubpc class MessageUtil { private String message; //Constructor //@param message to be printed pubpc MessageUtil(String message) { this.message = message; } // prints the message pubpc String printMessage() { System.out.println(message); return message; } }
Create Test Case Class
Create a java test class, say, SampleTest.java.
Add a test method testPrintMessage() to your test class.
Add an Annotation @Test to method testPrintMessage().
Implement the test condition and check the condition using assertEquals API of TestNG.
Create a java class file called SampleTest.java in /work/testng/src.
import org.testng.Assert; import org.testng.annotations.Test; pubpc class SampleTest { String message = "Hello World"; MessageUtil messageUtil = new MessageUtil(message); @Test pubpc void testPrintMessage() { Assert.assertEquals(message, messageUtil.printMessage()); } }
Create testng.xml
Next, let s create testng.xml file in /work/testng/src, to execute test case(s). This file captures your entire testing in XML. This file makes it easy to describe all your test suites and their parameters in one file, which you can check in your code repository or e-mail to coworkers. It also makes it easy to extract subsets of your tests or sppt several runtime configurations (e.g., testngdatabase.xml would run only tests that exercise your database).
<?xml version = "1.0" encoding = "UTF-8"?> <suite name = "Sample test Suite"> <test name = "Sample test"> <classes> <class name = "SampleTest" /> </classes> </test> </suite>
Compile the test case using javac.
/work/testng/src$ javac MessageUtil.java SampleTest.java
Now, run the testng.xml, which will run the test case defined in <test> tag.
/work/testng/src$ java org.testng.TestNG testng.xml
Verify the output.
Hello World =============================================== Sample test Suite Total tests run: 1, Failures: 0, Skips: 0 ===============================================Advertisements