- TestNG Tutorial
- TestNG - Home
- TestNG - Overview
- TestNG - Environment
- TestNG - Writing Tests
- TestNG - Basic Annotations
- TestNG - Execution Procedure
- TestNG - Executing Tests
- TestNG - Suite Test
- TestNG - Ignore a Test
- TestNG - Group Test
- TestNG - Exception Test
- TestNG - Dependency Test
- TestNG - Parameterized Test
- TestNG - Run JUnit Tests
- TestNG - Test Results
- TestNG - Annotation Transformers
- TestNG - Asserts
- TestNG - Parallel Execution
- TestNG - Plug with ANT
- TestNG - Plug with Eclipse
- TestNG - TestNG - vs JUnit
- TestNG Useful Resources
- TestNG - Quick Guide
- TestNG - Useful Resources
- TestNG - Discussion
TestNG - Basic Annotations - Factory
@Factory annotated method allows tests to be created at runtime depending on certain data-sets or conditions. The method must return Object[].
Create Test Case Class
Create a java test class, say, SimpleTestFactory.java.
Add a test method testMethod() to your test class.
Add an Annotation @Test to method testMethod().
Create a java class file called SimpleTestFactory.java in /work/testng/src.
import org.testng.annotations.Test; public class SimpleTestFactory { @Test public void testMethod(){ System.out.println("Simple Test Method."); } }
Create Factory Test Class
Create a another java class, say, TestAnnotationFactory.java in /work/testng/src.
Add a factory method factoryMethod() to your test class. It's mandatory that a factory method should return an array of Object class ( Object [] )
Add an Annotation @Factory to method factoryMethod().
Following are the TestAnnotationFactory.java contents:
import org.testng.annotations.Test; import org.testng.annotations.Factory; public class TestAnnotationFactory { @Factory public Object[] factoryMethod() { return new Object[]{ new SimpleTestFactory(), new SimpleTestFactory() }; } }
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 split several runtime configurations (e.g., testngdatabase.xml would run only tests that exercise your database).
<?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="TestAnnotationFactory"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
Compile the test case using javac.
/work/testng/src$ javac SimpleTestFactory.java TestAnnotationFactory.java
Now, run the testng.xml, which will run the test case defined in <test> tag. As you can see the the test method from the SimpleTestFactory class was executed two times.
/work/testng/src$ java org.testng.TestNG testng.xml
Verify the output.
Simple Test Method. Simple Test Method. =============================================== Suite Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================