Is there a way to inject ITestContext from TestNg to guice module?

血红的双手。 提交于 2019-12-23 04:47:24

问题


Let's say I have some guice module/provider which supposed to create bindings based on parameters received from TestNg suite file. e.g.

<test name="Test">
    <parameter name="profile" value="chrome"></parameter>
    <classes>
        <class name="com.apc.ui.tests.TestClass">
        </class>
    </classes>
</test>

What I wanted to achieve is a possibility to access parameter value from withing above mentioned module. e.g.

public class MyModule extends AbstractModule {
    @Inject
    ITestContext context;

    @Override
    protected void configure() {
        ...
    }
}

So, I'm wondering whether it's possible. Any alternatives are also really welcomed. Thanks.


回答1:


Eventually, did manage to find a solution in testNg sources. There is a possibility to set a so called parent module in a suite file.

<suite name="Suite1" verbose="1" parallel="false"
    parent-module="org.my.tests.ParentModule">
...

the module can receive ITestContext as a constructor parameter which means it can be then injected to other classes:

public class ParentModule extends AbstractModule {

  private ITestContext context;

  public GuiceParentModule(ITestContext context) {
    this.context = context;
  }

  @Override
  protected void configure() {
    bind(ITestContext.class).toInstance(context);
  }
...


来源:https://stackoverflow.com/questions/50351001/is-there-a-way-to-inject-itestcontext-from-testng-to-guice-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!