Search code examples
testngtestng-eclipse

How do I retrieve (x) = Arguments from TestNG debug configuration?


I am trying to debug a TESTNG test using Debug Configurations option in Eclipse and I need to get the Program arguments inside my TestNG test. Could you please help me in accomplishing it?

enter image description here


Solution

  • You can pass values to TestNG via two means in the Run Configurations screen

    1. JVM arguments. Below screenshot shows how to pass them

    JVM arguments

    1. As environment arguments. Below screenshot shows how to pass them

    Environment arguments

    The below sample shows how to read those values from the Run configuration

    import org.testng.annotations.Test;
    
    public class HelloWorldTestClass {
    
        @Test
        public void testMethod() {
            //This is how we read values provided via the Environment tab section of the run configuration.
            String environment = System.getenv("env");
            //This is how we read values provided via the VM arguments section 
            String browser = System.getProperty("browser");
            System.err.println("Running on [" + browser + "] in the environment [" + environment + "]");
        }
    }
    

    Below is the output of the execution:

    [RemoteTestNG] detected TestNG version 6.14.3
    Running on [Opera] in the environment [Production]
    PASSED: testMethod
    
    ===============================================
        Default test
        Tests run: 1, Failures: 0, Skips: 0
    ===============================================
    
    
    ===============================================
    Default suite
    Total tests run: 1, Failures: 0, Skips: 0
    ===============================================