2015-06-02 7 views
14

Ich führe ein Maven-Projekt in Eclipse für meine Gurken-Tests. Mein Test-Runner-Klasse sieht wie folgt aus:Get @ CucumberOptions-Tag-Eigenschaft mit 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 { 
} 

Statt die Tags in den Testläufer zu hart Code, ich bin daran interessiert, sie passieren in der .command-Datei. (Dh System.getProperty mit („cucumber.tag“)

aber ich erhalte eine Fehlermeldung, wenn ich die Codezeile zu dem obigen Testläufer hinzufügen:

@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 { 
} 

Der Fehler ich erhalte, ist: „Der Wert für Annotation CucumberOptions.tags Attribut muss ein konstanter Ausdruck sein“.

So scheint es nur konstanten eher als ein parametrierten Wert will. Wer eine kluge Art und Weise runden das wissen?

Antwort

13

Sie verwenden, um die cucumber.options Umgebung al-Variable zur Festlegung der Variablen zur Laufzeit

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

Diese ersetzt die bereits im Testcode enthaltenen Variablen.

0

ich so tue: -

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

Methode Properties-Datei zu laden: -

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

Methode CucumberOptions

zu erhalten und zu setzen
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); 
    } 

Und Haupt-Methode, dies auszuführen: -

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

Und RunGwMLCompareTests.class ist meine Gurke Klasse

@

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

    public RunGwMLCompareTests(){ 

    } 
} 

Also im Grunde jetzt setzen Sie den Ausgang Bericht erhalten und Feature-Ordner durch Eigenschaften Dateien und andere Optionen wie Kleber Definitionen Java-Klasse. Und zum Ausführen der Testfälle führen Sie einfach Ihre Hauptklasse aus.

Grüße,

Vikram Pathania