Cucumber-jvm:在执行任何功能之前设置环境

时间:2017-11-30 05:29:57

标签: java bdd cucumber-jvm

我想在运行任何功能之前设置环境(例如local,dev,prod)。它是设置基本URL并加载特定的测试数据文件。下面是我想在我的测试中调用一次和第一件事的示例方法。请建议最好的方法。

public void  baseSetUp(String environment){
        loadTestData = loadPropertiesFile(enviroment);
        setBaseUrl(enviroment);
        restUtil = new RestUtil(pilotBaseUrl);
        initialSetUp();
    }

我是黄瓜和java的新手。

1 个答案:

答案 0 :(得分:0)

我通过使用带有TestNG的黄瓜实现了它。通过这种方式,我能够使用TestNG的注释以及黄瓜钩。 TestNG执行您的功能文件。以下是Runner类:

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import cucumber.api.testng.CucumberFeatureWrapper;
import cucumber.api.testng.TestNGCucumberRunner;  
import org.junit.runner.RunWith;
import org.testng.annotations.*;


@RunWith(Cucumber.class)
@CucumberOptions(
        monochrome = true,
        features = "src/test/java/features",    
        glue = {"utils","steps"},
        tags = {"@test"},
        format = {
                "pretty",
                "html:target/cucumber-reports/cucumber-pretty",
                "json:target/cucumber-reports/CucumberTestReport.json",
                "rerun:target/cucumber-reports/rerun.txt"}
)
public class CucumberRunnerUtil  {

    private TestNGCucumberRunner testNGCucumberRunner;


    @BeforeSuite
    public void setUpEnvironment(){

    // your setup code e.g. environment set up etc.

    }

    @BeforeClass(alwaysRun = true)
    public void setUpClass() throws Exception {
        testNGCucumberRunner = new 
        TestNGCucumberRunner(this.getClass());
    }


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


    @DataProvider
    public Object[][] features() {
        return testNGCucumberRunner.provideFeatures();
    }



    @AfterClass(alwaysRun = true)
    public void tearDownClass() throws Exception {
        testNGCucumberRunner.finish();
    }


    @AfterSuite
    public void cleanUp(){
        // code to clean resources.
    }
}