如何通过Surefire Maven插件将参数传递给经过测试的TestNG测试? [英] How to pass parameters to guicified TestNG test from Surefire Maven plugin?

查看:77
本文介绍了如何通过Surefire Maven插件将参数传递给经过测试的TestNG测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Maven + Surefire + TestNG + Guice (latest stable ones)

我有一个大型"测试,需要Guice才能运行. 基本上我是这样的:

I have "large" test that requires Guice to run. Basically I'm doing it this way:

@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配置:

The problem is that FooPort is hardcoded to 5000. It is a Maven property, so the first try was to use next Surefire configuration:

<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测试,建议采用这种方式:

And after that request it like System.getProperty("fooPort"). Unfortunately, documentation says, that this is only for JUnit test. At least I could not see this system variable during debugging of a test. I tried forkMode both default one and never, it does not change anything. For TestNG tests it's recommended to make it this way:

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

但是现在我应该使用Guice的此属性,因此应该以某种方式将其赋予GuiceModule,我已经尝试过以下方法:

But now I should use this property from Guice, so it should be given somehow to GuiceModule, I've tried it next way:

@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,因此无法创建工厂实例.

But this way was also a failure as creator for modulefactories does not take @Parameters into account and thus could not create instance of a factory.

看起来我应该尝试从ITestContext context获取一些数据,但是我不知道数据的方式和是否存在,或者是否有一些更简单的方法来做我想要的事情.

Looks like I should try to get some data from ITestContext context, but I do not know how and if the data is there or if there is some simpler way to do what I want.

感谢您的回复.

推荐答案

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

I just ran a quick test and properties seem to be passed correctly to TestNG:

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

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

My suite file calls the test class B, which contains:

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

并在Maven中运行它显示:

and running it with Maven shows:

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

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

I'm not using Guice in this simple example but that's the only difference between your code and mine.

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

Feel free to create a small Maven project reproducing your problem, make sure that I can compile it and then email it to me.

这篇关于如何通过Surefire Maven插件将参数传递给经过测试的TestNG测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆