Get @CucumberOptions tag property using System.getProperty()

后端 未结 2 974
没有蜡笔的小新
没有蜡笔的小新 2020-12-17 16:06

I am running a maven project in Eclipse for my Cucumber tests. My test runner class looks like this:

@RunWith(Cucumber.class)
@CucumberOptions(
        tags         


        
相关标签:
2条回答
  • 2020-12-17 16:21

    You can use the cucumber.options environmental variable to specify the tags at runtime

    mvn -D"cucumber.options=--tags @Other,@Now" test
    

    This supercedes the tags already contained in the test code.

    0 讨论(0)
  • 2020-12-17 16:23

    I am doing like this:-

    cucmberOption.properties

    #cucumber.options=--plugin html:output/cucumber-html-report 
    #src/test/resources
    cucumber.options.feature =src/test/resources
    cucumber.options.report.html=--plugin html:output/cucumber-html-report
    

    Java Class: CreateCucumberOptions.java

    Method to load properties file:-

    private static void loadPropertiesFile(){
        InputStream input = null;
        try{
            String filename = "cucumberOptions.properties";
            input = CreateCucumberOptions.class.getClassLoader().getResourceAsStream(filename);
            if(input==null){
                LOGGER.error("Sorry, unable to find " + filename);
                return;
            }
            prop.load(input);
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            if(input!=null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    method to get and set CucumberOptions

    private String createAndGetCucumberOption(){       
     StringBuilder sb = new StringBuilder();
     String featureFilesPath = 
     prop.getProperty("cucumber.options.feature");
     LOGGER.info(" featureFilesPath: " +featureFilesPath);
     String htmlOutputReport = 
      prop.getProperty("cucumber.options.report.html");
     LOGGER.info(" htmlOutputReport: " +htmlOutputReport);
     sb.append(htmlOutputReport);
     sb.append(" ");
     sb.append(featureFilesPath);
     return sb.toString();
    }
    
    private void setOptions(){
       String value = createAndGetCucumberOption();
       LOGGER.info(" Value: " +value);
       System.setProperty(KEY, value);
       }
    

    And main method to run this:-

    public static void main(String[] args) {
        CreateCucumberOptions cucumberOptions = new CreateCucumberOptions();
        JUnitCore junitRunner = new JUnitCore();
        loadPropertiesFile();
        cucumberOptions.setOptions();
        junitRunner.run(cucumberTest.runners.RunGwMLCompareTests.class);
     }
    

    And RunGwMLCompareTests.class is my Cucumber class

    @

    RunWith(Cucumber.class)
    @CucumberOptions(
            monochrome = true,
            tags = {"@passed"},
            glue =  "cucumberTest.steps")
    public class RunGwMLCompareTests {
    
        public RunGwMLCompareTests(){
    
        }
    }
    

    So basically now you get set output report and feature folders through properties files and others options like glue definations java class. And to run the test cases just run your main class.

    Regards,

    Vikram Pathania

    0 讨论(0)
提交回复
热议问题