本文介绍了使用Args将JUnit转换为TestNG构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从JUnit迁移到TestNG. TestNG要求提供我提供的零args构造函数,但是我的测试类需要一个构造函数,在该构造函数中,我将一些配置传递给测试:

I'm moving from JUnit to TestNG. TestNG asks for a zero args constructor that I have provided, however my test class requires a constructor where I am passing some config to the test:

@DataProvider(filename = "DataSheet.xlsx", sheet = "DigitalTestsMain")

public class DigitalTests extends AbstractTests{

        @Inject
        public DigitalTests(Logger log, WcConfigManager config,
                                WebSessionFactory sessionFactory) {
            super(log, config, sessionFactory);
        }

        public DigitalTests() { 
        }


        @Test
        public void myAutomationTest() throws Exception { .... }

由于TestNG使用零args构造函数,因此我将这些值传递进去,是否会在@Before中?

As TestNG use a zero args constructor I do I pass these values in, would that be in a @Before?

推荐答案

TestNG支持 @从Guice注入,但我从未在构造函数上尝试过.您应该尝试在类上添加适当的@Guice.

TestNG supports @Inject from Guice, but I've never try it on constructor. You should try by adding the appropriate @Guice on the class.

工厂是将参数传递给测试构造函数的另一种方法.

Factories are antoher way to pass parameters to test constructor.

根据您的AbstractTests,也许@BeforeX/AfterX是最好的解决方案.

Depending on your AbstractTests, maybe @BeforeX/AfterX will be the best solution.

这篇关于使用Args将JUnit转换为TestNG构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:05