有没有一种方法可以将ITestContext从TestNg注入guice模块? [英] Is there a way to inject ITestContext from TestNg to guice module?

查看:120
本文介绍了有没有一种方法可以将ITestContext从TestNg注入guice模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一些guice模块/提供程序,它们应该根据从TestNg套件文件接收的参数来创建绑定.例如

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.

推荐答案

最终,确实设法在testNg源代码中找到了解决方案.可以在套件文件中设置所谓的父模块.

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">
...

该模块可以接收ITestContext作为构造函数参数,这意味着它可以随后注入到其他类中:

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);
  }
...

这篇关于有没有一种方法可以将ITestContext从TestNg注入guice模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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