Maven 如何设置testsuite文件以便跨环境执行

Maven 如何设置testsuite文件以便跨环境执行,maven,jenkins,testng,testng-eclipse,Maven,Jenkins,Testng,Testng Eclipse,我有一个测试套件文件,需要能够从mvn命令行(来自Jenkins)和Eclipse的按需执行 测试套件文件必须能够支持参数,即: <suite name="test run1"> <parameter name="testEnv" value="dev"></parameter> <parameter name="proxyServer" value="x"></parameter> <parameter nam

我有一个测试套件文件,需要能够从mvn命令行(来自Jenkins)和Eclipse的按需执行

测试套件文件必须能够支持参数,即:

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

如何编写测试套件文件,使其支持Eclipse的即席执行和mvn命令行执行?

如果需要可配置的测试属性,请使用@DataProvider而不是硬编码套件xml

提供程序类:

public class EnvProvider {

    @DataProvider(name = "envProvider") 
    public static Object[][] createData() {
        return new Object[][] { new Object[] { 
           System.getProperty("testEnv", "eclipse-default") }
    };
}
试验方法:

@Test(dataProvider = "envProvider", dataProviderClass = EnvProvider.class)
public void myTest(String currentEnv) {
    System.out.println("Current env is : " + currentEnv);
}
pom.xml

<properties>
    <testEnv>default-pom</testEnv>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <systemPropertyVariables>
                    <testEnv>${testEnv}</testEnv>
                </systemPropertyVariables>
                ...

默认pom
org.apache.maven.plugins

如果系统级参数可用,您可以使用这些参数覆盖套件xml参数-这将允许您从xml和mvn运行

所有参数基本上都分配给一些常量属性,以便在测试中使用。属性文件是在套件侦听器的onBeforeSuite方法中初始化的

SuiteList extends SuiteListener{
 public void onStart(ISuite suite) {
    LProperties.loadProperties(suite);
}



//loadProperties implementation
  LProperties{

    public static void  loadProperties(ISuite suite){
        //This can read from a properties file and load properties 
        //or
        // from the suite.getParameter - pick the params you need (reflection or list) and assign to the constants

        //For all the properties, do System.getProperty and override values if found
   }

在测试中使用LProperty常量。

基于以上答案的组合,我已经找到了答案

首先删除测试套件文件中的硬编码参数

接下来,确保pom文件中支持这些参数

<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>
在testNG setup()方法中,使用System.getproperty()


我可以将默认值放在配置文件中,但现在,常量文件在它自己的类中似乎最简单。

我必须管理我不完全理解您的问题,但通常要启用/禁用测试,使用配置属性skipTests。(). 你试过用它吗?您发现任何问题了吗?我如何维护一个测试套件文件,以便它同时支持Eclipse的即席执行和mvn命令行执行(例如,来自Jenkins的命令行执行)。这些参数是否在测试中使用?您用这些参数标记所有测试?这些参数在setup()方法中用于加载特定的配置属性。它们根本没有在@Test注释中使用。虽然这并没有完全解决我的问题,但它确实激发了我的思考,让我朝着正确的方向前进。我已经补充了我的答案。
<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>
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";
}
@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);
}