2015-06-02 10 views
14

Sto eseguendo un progetto Maven in Eclipse per i miei test Cucumber. La mia classe di test corridore si presenta così:Ottieni la proprietà tag @CucumberOptions utilizzando System.getProperty()

@RunWith(Cucumber.class) 
@CucumberOptions(
     tags = { "@Now" },  
//  tags = { "@Ready" }, 
//  tags = { "@Draft" }, 
     features = { "src/test/java/com/myCompany/FaultReporting/Features" }, 
     glue = { "com.myCompany.myApp.StepDefinitions" } 
     ) 
public class RunnerTest { 
} 

Invece di dover codificare i tag nel test runner, io sono pronto a passarli in utilizzando il file .command. (Vale a dire utilizzando System.getProperty ("cucumber.tag")

Tuttavia, ottengo un errore quando aggiungo la linea di codice al di sopra di test runner:

@RunWith(Cucumber.class) 
@CucumberOptions(
     tags = { System.getProperty("cucumber.tag") } 
//  tags = { "@Now" },  
//  tags = { "@Ready" }, 
//  tags = { "@Draft" }, 
     features = { "src/test/java/com/myCompany/FaultReporting/Features" }, 
     glue = { "com.myCompany.myApp.StepDefinitions" } 
     ) 
public class RunnerTest { 
} 

L'errore che ottengo è: "il valore per l'annotazione attributo CucumberOptions.tags deve essere un'espressione costante".

Così sembra che vuole solo le costanti piuttosto che un valore parametrizzato. Qualcuno sa un modo intelligente intorno a questo?

risposta

13

è possibile utilizzare l'ambiente cucumber.options al variabile per specificare i tag in fase di esecuzione

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

Questo sostituisce i tag già contenute nel codice di test.

0

sto facendo in questo modo: -

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 Classe: CreateCucumberOptions.java

Metodo

per caricare file di proprietà: -

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

metodo ottenere e impostare 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); 
    } 

e il metodo principale per eseguire questo: -

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

E RunGwMLCompareTests.class è la mia classe cetriolo

@

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

    public RunGwMLCompareTests(){ 

    } 
} 

Quindi, in pratica ora si ottiene impostare report di output e funzionalità di cartelle attraverso i file di proprietà e altre opzioni come la definizione jue della glue glue. E per eseguire i test basta eseguire la tua classe principale.

saluti,

Vikram Pathania

Problemi correlati