Cucumber-jvm:无法使用参数注入@Test CucumberFeatureWrapper

时间:2018-04-12 02:23:44

标签: testng cucumber-jvm

我使用BDD Cucumber和TestNG运行项目,但在下面遇到错误。

Cannot inject @Test annotated Method [feature] with [interface cucumber.api.testng.CucumberFeatureWrapper].
For more information on native dependency injection please refer to http://testng.org/doc/documentation-main.html#native-dependency-injection

我知道@Test方法只能有@ITestContext注释。如何运行我的功能文件?

@CucumberOptions(
        features = "src/Feature",
        glue = {"Step_Definitions"},
        plugin = {
                "pretty",
                "html:target/cucumber-reports/cucumber-pretty",
                "json:target/cucumber-reports/CucumberTestReport.json",
                "rerun:target/cucumber-reports/rerun.txt"
})
public class TestRunner {
    private TestNGCucumberRunner testNGCucumberRunner;
    private ExtentReportManager reportMgr;
    private PropertiesManager pm;

    @BeforeTest(alwaysRun = true)
    public void setUp() throws Exception {
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());

        pm = PropertiesManager.createInstance();
        reportMgr = ExtentReportManager.createInstance();

        WebDriverManager.createDriver();
    }

    @Test(groups = "cucumber", description = "Runs Cucumber Feature"/*, dataProvider = "dataProvider"*/)
    public void feature(CucumberFeatureWrapper cucumberFeature) {
        //ITestContext context
        testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
    }



    /*@DataProvider(name = "dataProvider", parallel = true)
    public Object[][] features() {
        return testNGCucumberRunner.provideFeatures();
    }*/

    @AfterTest(alwaysRun = true)
    public void tearDown() throws Exception {
        reportMgr.getExtent().flush();
        testNGCucumberRunner.finish();
    }

我试过这个tutorial但是无法导入RunCukesStrict.class 我使用版本2.4.0 for cucumber-jvm。有具体的例子吗?

1 个答案:

答案 0 :(得分:1)

您要链接的教程不是教程。它是TestNGCucumberRunner的单元测试。试试example section instead.

Cucumber希望你注入个别场景而不是整个测试跑者,如下所示:

@Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "scenarios")
public void scenario(PickleEventWrapper pickleEvent, CucumberFeatureWrapper cucumberFeature) throws Throwable {
    testNGCucumberRunner.runScenario(pickleEvent.getPickleEvent());
}

@DataProvider
public Object[][] scenarios() {
    return testNGCucumberRunner.provideScenarios();
}