CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-testng--testng

Comprehensive testing framework for Java with annotations, data-driven testing, and parallel execution capabilities

Overview
Eval results
Files

test-execution.mddocs/

Test Execution

TestNG's test execution system centers around the TestNG class, which orchestrates test discovery, dependency resolution, parallel execution, and result reporting. It provides both command-line and programmatic interfaces for running tests.

Capabilities

TestNG Class

The main entry point for running TestNG tests programmatically. Provides comprehensive configuration options for test execution, parallel processing, and result collection.

/**
 * Main class for running TestNG tests programmatically
 */
public class TestNG {
    
    // Constants
    public static final String DEFAULT_COMMAND_LINE_SUITE_NAME = "Command line suite";
    public static final String DEFAULT_COMMAND_LINE_TEST_NAME = "Command line test";
    public static final String DEFAULT_OUTPUTDIR = "test-output";
    
    // Constructors
    public TestNG();
    public TestNG(boolean useDefaultListeners);
    
    // Test Configuration Methods
    public void setTestClasses(Class[] classes);
    public void setTestSuites(List<String> testSuites);
    public void setXmlSuites(List<XmlSuite> xmlSuites);
    public void setMethodInterceptor(IMethodInterceptor interceptor);
    public void setObjectFactory(ITestObjectFactory objectFactory);
    public void setTestRunnerFactory(ITestRunnerFactory testRunnerFactory);
    
    // Group Configuration
    public void setGroups(String groups);
    public void setExcludedGroups(String excludedGroups);
    public void setGroupByInstances(boolean groupByInstances);
    
    // Parallel Execution Configuration
    public void setParallel(XmlSuite.ParallelMode parallel);
    public void setThreadCount(int threadCount);
    public void setDataProviderThreadCount(int dataProviderThreadCount);
    public void setSuiteThreadPoolSize(int suiteThreadPoolSize);
    public void setConfigFailurePolicy(XmlSuite.FailurePolicy configFailurePolicy);
    
    // Output Configuration
    public void setOutputDirectory(String outputDirectory);
    public void setVerbose(int verbose);
    public void setUseDefaultListeners(boolean useDefaultListeners);
    
    // Listener Management
    public void addListener(Object listener);
    public void setListenerClasses(List<Class<? extends ITestNGListener>> listenerClasses);
    public void addClassLoader(ClassLoader loader);
    
    // Execution Methods
    public void run();
    public int getStatus();
    public List<ISuite> getSuites();
    
    // Command Line Interface
    public static void main(String[] argv);
    public void configure(CommandLineArgs cla);
    
    // Utility Methods
    public void initializeEverything();
    public void setPreserveOrder(boolean preserveOrder);
    public void setRandomizeSuites(boolean randomizeSuites);
    public void setSkipFailedInvocationCounts(boolean skipFailedInvocationCounts);
    public void setAnnotations(IAnnotationFinder finder);
}

Usage Examples:

import org.testng.TestNG;
import org.testng.xml.XmlSuite;
import java.util.Arrays;
import java.util.List;

public class TestExecutionExamples {
    
    public void runTestClasses() {
        // Basic test execution with classes
        TestNG testng = new TestNG();
        testng.setTestClasses(new Class[] { 
            MyTestClass1.class, 
            MyTestClass2.class 
        });
        testng.run();
        
        System.out.println("Test execution status: " + testng.getStatus());
    }
    
    public void runWithConfiguration() {
        TestNG testng = new TestNG();
        
        // Configure test classes
        testng.setTestClasses(new Class[] { MyTestClass.class });
        
        // Configure output
        testng.setOutputDirectory("target/test-results");
        testng.setVerbose(2);
        
        // Configure groups
        testng.setGroups("unit,integration");
        testng.setExcludedGroups("slow");
        
        // Configure parallel execution
        testng.setParallel(XmlSuite.ParallelMode.METHODS);
        testng.setThreadCount(4);
        
        // Add listeners
        testng.addListener(new CustomTestListener());
        testng.addListener(new CustomSuiteListener());
        
        // Run tests
        testng.run();
        
        // Check results
        if (testng.getStatus() == 0) {
            System.out.println("All tests passed");
        } else {
            System.out.println("Some tests failed");
        }
    }
    
    public void runWithXmlSuites() {
        TestNG testng = new TestNG();
        
        // Create XML suite programmatically
        XmlSuite suite = new XmlSuite();
        suite.setName("Programmatic Suite");
        suite.setParallel(XmlSuite.ParallelMode.CLASSES);
        suite.setThreadCount(3);
        
        // Add tests to suite
        XmlTest test = new XmlTest(suite);
        test.setName("Unit Tests");
        test.setXmlClasses(Arrays.asList(
            new XmlClass("com.example.UnitTest1"),
            new XmlClass("com.example.UnitTest2")
        ));
        
        // Set XML suites
        testng.setXmlSuites(Arrays.asList(suite));
        testng.run();
    }
    
    public void runTestSuiteFiles() {
        TestNG testng = new TestNG();
        
        // Run from XML files
        testng.setTestSuites(Arrays.asList(
            "src/test/resources/smoke-tests.xml",
            "src/test/resources/integration-tests.xml"
        ));
        
        testng.run();
    }
    
    public void runWithAdvancedConfiguration() {
        TestNG testng = new TestNG();
        
        // Advanced configuration
        testng.setTestClasses(new Class[] { AdvancedTestClass.class });
        testng.setPreserveOrder(true);
        testng.setRandomizeSuites(false);
        testng.setSkipFailedInvocationCounts(true);
        testng.setConfigFailurePolicy(XmlSuite.FailurePolicy.CONTINUE);
        testng.setDataProviderThreadCount(2);
        testng.setSuiteThreadPoolSize(1);
        
        // Custom object factory for dependency injection
        testng.setObjectFactory(new GuiceObjectFactory());
        
        // Method interceptor for custom test selection
        testng.setMethodInterceptor(new CustomMethodInterceptor());
        
        testng.run();
    }
    
    public static void main(String[] args) {
        // Command line execution
        TestNG.main(new String[] {
            "-testclass", "com.example.MyTestClass",
            "-groups", "unit",
            "-parallel", "methods",
            "-threadcount", "4",
            "-outputdir", "test-results"
        });
    }
}

Command Line Interface

TestNG provides a comprehensive command-line interface for test execution with extensive configuration options.

# Basic usage
java org.testng.TestNG testng.xml

# Run specific test classes
java org.testng.TestNG -testclass com.example.TestClass1,com.example.TestClass2

# Run with groups
java org.testng.TestNG -groups unit,integration -excludegroups slow testng.xml

# Parallel execution
java org.testng.TestNG -parallel methods -threadcount 4 testng.xml

# Output configuration
java org.testng.TestNG -d test-results -verbose 2 testng.xml

# Custom listeners
java org.testng.TestNG -listener com.example.CustomListener testng.xml

Command Line Options:

/**
 * Command line argument configuration
 */
public class CommandLineArgs {
    
    // Test selection
    public String testClass;           // -testclass: Comma-separated test classes
    public List<String> suiteFiles;   // Suite XML files
    public String groups;              // -groups: Included groups
    public String excludedGroups;     // -excludegroups: Excluded groups
    public String methods;             // -methods: Specific methods to run
    
    // Parallel execution
    public String parallel;            // -parallel: none|methods|tests|classes|instances
    public Integer threadCount;        // -threadcount: Number of threads
    public Integer dataProviderThreadCount; // -dataproviderthreadcount
    public Integer suiteThreadPoolSize;     // -suitethreadpoolsize
    
    // Output configuration
    public String outputDirectory;     // -d: Output directory
    public Integer verbose;            // -verbose: Verbosity level
    public Boolean useDefaultListeners; // -usedefaultlisteners
    
    // Listeners and factories
    public String listener;            // -listener: Comma-separated listener classes
    public String objectFactory;      // -objectfactory: Object factory class
    public String testRunnerFactory;   // -testrunner: Test runner factory
    
    // Execution behavior
    public String configFailurePolicy; // -configfailurepolicy: skip|continue
    public Boolean skipFailedInvocationCounts; // -skipfailedinvocationcounts
    public Boolean preserveOrder;      // -preserveorder
    public Boolean randomizeSuites;    // -randomizesuites
    public Boolean groupByInstances;   // -groupbyinstances
    
    // Miscellaneous
    public String xmlPathInJar;        // -xmlpathinjar: XML path in JAR
    public List<String> commandLineMethods; // Individual methods
    public String junit;               // -junit: Run in JUnit mode
    public Boolean mixed;              // -mixed: Mixed mode
}

Test Result Collection

TestNG provides comprehensive result collection through the ISuite and ITestResult interfaces.

/**
 * Suite execution results
 */
public interface ISuite {
    String getName();
    Map<String, ISuiteResult> getResults();
    String getOutputDirectory();
    String getParameter(String parameterName);
    Map<String, Collection<ITestNGMethod>> getMethodsByGroups();
    Collection<ITestNGMethod> getAllInvokedMethods();
    Collection<ITestNGMethod> getExcludedMethods();
    void run();
    String getXmlSuite();
    IObjectFactory getObjectFactory();
    IObjectFactory2 getObjectFactory2();
    List<IInvokedMethod> getAllInvokedMethods();
}

/**
 * Individual test execution results
 */
public interface ISuiteResult {
    ITestContext getTestContext();
    String getName();
}

/**
 * Test context providing execution information
 */
public interface ITestContext {
    String getName();
    Date getStartDate();
    Date getEndDate();
    IResultMap getPassedTests();
    IResultMap getFailedTests();
    IResultMap getSkippedTests();
    IResultMap getFailedButWithinSuccessPercentageTests();
    String[] getIncludedGroups();
    String[] getExcludedGroups();
    String getOutputDirectory();
    ISuite getSuite();
    Object getAttribute(String name);
    void setAttribute(String name, Object value);
    Set<String> getAttributeNames();
    Object removeAttribute(String name);
}

Usage Examples:

public class ResultCollectionExamples {
    
    public void collectTestResults() {
        TestNG testng = new TestNG();
        testng.setTestClasses(new Class[] { MyTestClass.class });
        testng.run();
        
        // Get suite results
        List<ISuite> suites = testng.getSuites();
        for (ISuite suite : suites) {
            System.out.println("Suite: " + suite.getName());
            
            Map<String, ISuiteResult> results = suite.getResults();
            for (ISuiteResult result : results.values()) {
                ITestContext context = result.getTestContext();
                
                System.out.println("Test: " + context.getName());
                System.out.println("Start: " + context.getStartDate());
                System.out.println("End: " + context.getEndDate());
                
                // Passed tests
                System.out.println("Passed: " + context.getPassedTests().size());
                
                // Failed tests
                System.out.println("Failed: " + context.getFailedTests().size());
                for (ITestResult testResult : context.getFailedTests().getAllResults()) {
                    System.out.println("  Failed: " + testResult.getMethod().getMethodName());
                    if (testResult.getThrowable() != null) {
                        System.out.println("  Error: " + testResult.getThrowable().getMessage());
                    }
                }
                
                // Skipped tests
                System.out.println("Skipped: " + context.getSkippedTests().size());
            }
        }
    }
    
    public void analyzeTestExecution() {
        TestNG testng = new TestNG();
        testng.setTestClasses(new Class[] { AnalysisTestClass.class });
        testng.addListener(new ITestListener() {
            @Override
            public void onTestStart(ITestResult result) {
                System.out.println("Starting: " + result.getMethod().getMethodName());
            }
            
            @Override
            public void onTestSuccess(ITestResult result) {
                long duration = result.getEndMillis() - result.getStartMillis();
                System.out.println("Passed: " + result.getMethod().getMethodName() 
                    + " (" + duration + "ms)");
            }
            
            @Override
            public void onTestFailure(ITestResult result) {
                System.out.println("Failed: " + result.getMethod().getMethodName());
                System.out.println("Parameters: " + Arrays.toString(result.getParameters()));
            }
        });
        
        testng.run();
        
        // Final status check
        int status = testng.getStatus();
        System.out.println("Execution completed with status: " + status);
    }
}

Execution Control Interfaces

Interfaces for controlling test execution behavior, method selection, and object creation.

/**
 * Interface for intercepting and modifying test methods
 */
public interface IMethodInterceptor {
    List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context);
}

/**
 * Interface for selecting test methods
 */
public interface IMethodSelector {
    boolean includeMethod(IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod);
    void setTestMethods(List<ITestNGMethod> testMethods);
}

/**
 * Interface for creating test instances
 */
public interface ITestObjectFactory {
    Object newInstance(Constructor constructor, Object... params);
}

/**
 * Enhanced object factory interface
 */
public interface IObjectFactory2 extends ITestObjectFactory {
    Object newInstance(Class<?> cls);
}

/**
 * Interface for creating test runners
 */
public interface ITestRunnerFactory {
    TestRunner newTestRunner(ISuite suite, XmlTest test, 
                           Collection<IInvokedMethodListener> listeners,
                           List<IClassListener> classListeners);
}

Types

// Test execution status constants
public static final int HAS_NO_TEST = 1;
public static final int HAS_FAILURE = 2;  
public static final int HAS_SKIPPED = 4;
public static final int HAS_FSP = 8; // Failed but within success percentage

// Parallel execution modes (from XmlSuite)
public enum ParallelMode {
    TESTS,      // Parallel test execution
    METHODS,    // Parallel method execution  
    CLASSES,    // Parallel class execution
    INSTANCES,  // Parallel instance execution
    NONE        // No parallel execution
}

// Configuration failure policies
public enum FailurePolicy {
    SKIP,       // Skip dependent tests on configuration failure
    CONTINUE    // Continue with dependent tests
}

// Method instance for interceptors
public interface IMethodInstance {
    ITestNGMethod getMethod();
    Object getInstance();
}

// Method selector context
public interface IMethodSelectorContext {
    boolean isStopped();
    void setStopped(boolean stopped);
    Map<Object, Object> getUserData();
}

Install with Tessl CLI

npx tessl i tessl/maven-org-testng--testng

docs

annotations.md

assertions.md

collections-utilities.md

index.md

listeners-hooks.md

test-execution.md

xml-configuration.md

tile.json