1

我正在使用 Maven + Surefire + TestNG + Guice(最新的稳定版)

我有需要运行 Guice 的“大型”测试。基本上我是这样做的:

@Test(groups = "large")
@Guice(modules = FooLargeTest.Module.class)
public class FooLargeTest {
  public static class Module extends AbstractModule {
    public void configure() {
      bindConstant().annotatedWith(FooPort.class).to(5000);
      // ... some other test bindings
    }
  }
  @Inject Provider<Foo> fooProvider;
  @Test
  public void testFoo() {
    Foo foo = fooProvider.get() // here injection of port is done
                                // it could not be passed to constructor
    // ... actual test of foo
  }
}

问题是它FooPort被硬编码为5000. 这是一个 Maven 属性,所以第一次尝试是使用下一个 Surefire 配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <suiteXmlFiles>
        <suiteXmlFile>testng.xml</suiteXmlFile>
      </suiteXmlFiles>
      <systemPropertyVariables>
        <fooPort>${foo.port}</fooPort>
      </systemPropertyVariables>
    </configuration>
 </plugin>

然后请求它就像System.getProperty("fooPort"). 不幸的是,文档说,这仅适用于 JUnit 测试。至少在调试测试期间我看不到这个系统变量。我尝试forkMode了默认的一个和never,它没有改变任何东西。对于 TestNG 测试,建议这样做:

<properties>
  <property>
    <name>fooPort</name>
    <value>${foo.port}</value>
  </property>
</properties>

但是现在我应该使用 Guice 的这个属性,所以应该以某种方式将它提供给 GuiceModule,我尝试了下一个方法:

@Test(groups = "large")
@Guice(moduleFactory = FooLargeTest.ModuleFactory.class)
public class FooLargeTest {
  public static class ModuleFactory extends AbstractModule {
    private final String fooPort = fooPort;
    @Parameters("fooPort")
    public ModuleFactory(String fooPort) {
      this.fooPort = fooPort;
    }
    public Module createModule(final ITestContext context, Class<?> testClass) { 
      return new AbstractModule {
        public void configure() {
          bindConstant().annotatedWith(FooPort.class).to(fooPort);
          // ... some other test bindings
        }
      }
    }
  }
  @Inject Provider<Foo> fooProvider;
  @Test
  public void testFoo() {
    Foo foo = fooProvider.get() // here injection of port is done
    // actual test of foo
  }
}

但是这种方式也是失败的,因为创建者modulefactories没有考虑@Parameters到,因此无法创建工厂的实例。

看起来我应该尝试从中获取一些数据ITestContext context,但我不知道数据如何以及是否存在,或者是否有一些更简单的方法可以做我想做的事情。

感谢您的回复。

4

2 回答 2

2

我刚刚进行了快速测试,属性似乎正确传递给了 TestNG:

    <configuration>
      <suiteXmlFiles>
        <suiteXmlFile>src/test/resources/testng-single.yaml</suiteXmlFile>
      </suiteXmlFiles>
      <systemPropertyVariables>
        <foo>bar</foo>
      </systemPropertyVariables>

我的套件文件调用测试类 B,其中包含:

@Test
public void f() {
  System.out.println(" property:" + System.getProperty("foo"));
}

并用 Maven 运行它显示:

property:bar
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.358 sec

在这个简单的示例中,我没有使用 Guice,但这是您的代码和我的代码之间的唯一区别。

随意创建一个小型 Maven 项目来重现您的问题,确保我可以编译它,然后通过电子邮件发送给我。

于 2011-06-08T18:31:49.430 回答
-1

我尝试了以下测试并且工作正常

我的 Pom 文件

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <!--<groups>Regression</groups> -->
                    <systemPropertyVariables>
                        <environment>UAT</environment>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>

我的 TestNG 测试

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;


public class TestAuthentication {

    @Test (groups = { "Sniff", "Regression" })
    public void validAuthenticationTest(){
        System.out.println(" property:" + System.getProperty("environment"));
    }

    @Test (groups = { "Regression" },parameters = {"environment"})
    public void failedAuthenticationTest(String environment){
        System.out.println("Regression-"+environment);
    }

    @Parameters("environment")
    @Test (groups = { "Sniff"})
    public void newUserAuthenticationTest(String environment){
        System.out.println("Sniff-"+environment);
    }
}
于 2012-11-29T17:06:15.710 回答