Search code examples
mavenjenkinstestngtestng-eclipse

How to set up testsuite files for execution across environments


I have a test suite file that needs ability to be executed from both mvn command line (from Jenkins) as well as on-demand from Eclipse.

The test suite file must have ability to support parameters, ie:

<suite name="test run1">
   <parameter name="testEnv" value="dev"></parameter>
   <parameter name="proxyServer" value="x"></parameter>
   <parameter name="proxyPort" value="y"></parameter>

If I leave as is, then mvn command line parameters don't work, as the values in the test suite file will override the parameters. i.e. this will not work:

mvn test ... -dtestEnv=E1QA -dproxyServer= -dproxyPort=

How can I write the test suite file so it supports both ad-hoc execution from Eclipse and mvn command line execution?


Solution

  • based on a combination of above answers, I've figured it out.

    First remove the hard-coded parameters in the test suite file.

    Next, ensure the parameters are supported in the pom file.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.4.2</version>
                <configuration>
                    <systemPropertyVariables>
                        <environment>${testEnv}</environment>
                        <environment>${proxyServer}</environment>
                        <environment>${proxyPort}</environment>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    Create constants file to contain default values for the parameters

    public class Constants {
        public static final String DEFAULT_TEST_ENV = "dev";
        public static final String DEFAULT_PROXY_SERVER = "a-dev.phx.com";
        public static final String DEFAULT_PROXY_PORT = "8585";
    }
    

    In the testNG setup() method, use System.getproperty():

    @BeforeClass(alwaysRun = true)
    protected static void setUp() throws Exception {
        String testEnv = System.getProperty("testEnv", Constants.DEFAULT_TEST_ENV);
        String proxyServer = System.getProperty("proxyServer", Constants.DEFAULT_PROXY_SERVER);
        String proxyPort = System.getProperty("proxyPort", Constants.DEFAULT_PROXY_PORT);
    
        System.out.println("testEnv: " + testEnv);
        System.out.println("proxyServer: " + proxyServer);
        System.out.println("proxyPort: " + proxyPort);
    }
    

    I could put the default values in a config file, but for now, constants file seems easiest in its own class.