Re: [Cucumber] [JVM] Supply feature files to Cucumber-jvm test runner

5,086 views
Skip to first unread message

Aslak Hellesøy

unread,
Dec 11, 2012, 2:54:29 AM12/11/12
to cu...@googlegroups.com

On Monday, 10 December 2012 at 23:07, Tim Mertens wrote:

Hi,

I am using cucumber-jvm and am wondering if/how I can tell Cucumber what feature files to run at runtime,
You can override all command line arguments with the cucumber.options system property. Try this:

-Dcucumber.options="--help"

Aslak 
rather than by specifying the  features at compile time:

@RunWith(Cucumber.class)
@Cucumber.Options(features={"classpath:com/test/my/feature.feature"})
public class RunTest {}



For reasons specific to my environment, I am using the following code to execute specific test cases from an executable jar:

junitRunner.run(com.test.RunTest.class);

I can use custom @RunWith(Cucumber.class) classes like the one above to run specific feature files, but I would really like to be able to supply a list of .feature files (or even an input stream or whatever else it needs) to the test runner to define which test cases to execute, or I could call the test runner for every feature file if needed.  I want to avoid having to statically define the feature files in Java code.

If anyone has any idea how I could do this or can point me in the right direction, please let me know!

Thanks!
Tim

--
-- Rules --
 
1) Please prefix the subject with [Ruby], [JVM] or [JS].
2) Please use interleaved answers http://en.wikipedia.org/wiki/Posting_style#Interleaved_style
3) If you have a question, don't reply to an existing message. Start a new topic instead.
 
You received this message because you are subscribed to the Google Groups Cukes group. To post to this group, send email to cu...@googlegroups.com. To unsubscribe from this group, send email to cukes+un...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/cukes?hl=en
 
 

Tim Mertens

unread,
Dec 12, 2012, 3:49:22 PM12/12/12
to cu...@googlegroups.com
Thanks Aslak,

I am looking at the usage document but it is not clear how I can use the cucumber.options property to accomplish my objective.  I can build an options String[] and pass it to the cli main:

cucumber.api.cli.Main.main(opts);

But there are a couple problems with this approach:
  1. The glue "-g" option allows file paths only and does not allow classpath references, as is allowed in the @Cucumber.Options() notation when using a RunTest.class file.  It returns an error when I try to supply a classpath.
  2. Executing the features this way does not return results unless I manually parse the stdout output manually. On the other hand, using JUnitCore.run() returns a Result object that can be easily used for processing results:
JUnitCore junitRunner = new JUnitCore();
Result run;
run
= junitRunner.run(com.tim.tests.RunTest.class);
for(Failure failure : run.getFailures()) {
    logger
.error(failure.getMessage(), failure.getException());
}

But of course, in the above case this uses only the @Cucumber.Options specified in the class, unless there is some way to override them prior to the junitRunner.run call.

I am still investigating this myself but any help is appreciated!

Thanks,
Tim
Message has been deleted

Tim Mertens

unread,
Dec 12, 2012, 4:22:14 PM12/12/12
to cu...@googlegroups.com
I tried setting the cucumber.options System Property, but when I do I receive an exception. For example:

System.setProperty("cucumber.options", " --monochrome");
run
= junitRunner.run(com.tim.tests.RunTest.class);

Results in the following exception, even though I am not specifying any glue or feature file options in the cucumber.options System Property:

Not a file or directory: C:\Users\tim\Documents\NetBeansProjects\tests
java.lang.IllegalArgumentException: Not a file or directory: C:\Users\tim\Documents\NetBeansProjects\tests
    at cucumber.runtime.io.FileResourceIterator$FileIterator.<init>(FileResourceIterator.java:54)
    at cucumber.runtime.io.FileResourceIterator.<init>(FileResourceIterator.java:20)
    at cucumber.runtime.io.FileResourceIterable.iterator(FileResourceIterable.java:19)
    at cucumber.runtime.model.CucumberFeature.load(CucumberFeature.java:38)
    at cucumber.runtime.RuntimeOptions.cucumberFeatures(RuntimeOptions.java:100)
    at cucumber.api.junit.Cucumber.<init>(Cucumber.java:62)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runner.Computer.getRunner(Computer.java:38)
    at org.junit.runner.Computer$1.runnerForClass(Computer.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:98)
    at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
    at org.junit.runners.Suite.<init>(Suite.java:79)
    at org.junit.runner.Computer.getSuite(Computer.java:26)
    at org.junit.runner.Request.classes(Request.java:69)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
    at com.tim.tests.TestRunner.main(TestRunner.java:126)


Where the "tests" directory is the base directory of my NetBean project.

I am using the latest cucumber-java and cucumber-junit 1.1.1 in my pom file:
        <dependency>
           
<groupId>info.cukes</groupId>
           
<artifactId>cucumber-java</artifactId>
           
<version>1.1.1</version>
       
</dependency>
       
<dependency>
           
<groupId>info.cukes</groupId>
           
<artifactId>cucumber-junit</artifactId>
           
<version>1.1.1</version>
       
</dependency>

aslak hellesoy

unread,
Dec 12, 2012, 7:02:53 PM12/12/12
to Cucumber Users
On Wed, Dec 12, 2012 at 9:20 PM, Tim Mertens <t...@filmchicago.org> wrote:
I tried setting the cucumber.options System Property, but when I do I receive an exception. For example:

System.setProperty("cucumber.options", " --monochrome");

run
= junitRunner.run(com.lyonscg.qa.taf.tests.magento.RunTest.class);


You shouldn't be using a space in the arguments. Try "--monochrome".
 
Results in the following exception, even though I am not specifying any glue or feature file options in the cucumber.options System Property:

The Cucumber JUnit runner will default the glue and feature paths to the directory of the annotated class, i.e. classpath:com/lyonscg/qa/taf/tests/magento

If you create a ticket with instructions/code to reproduce the exception below we can improve the error message. You can use the java-helloworld example as a starting point.

Hi,

I am using cucumber-jvm and am wondering if/how I can tell Cucumber what feature files to run at runtime, rather than by specifying the  features at compile time:


@RunWith(Cucumber.class)
@Cucumber.Options(features={"classpath:com/test/my/feature.feature"})
public class RunTest {}



For reasons specific to my environment, I am using the following code to execute specific test cases from an executable jar:

junitRunner.run(com.test.RunTest.class);

I can use custom @RunWith(Cucumber.class) classes like the one above to run specific feature files, but I would really like to be able to supply a list of .feature files (or even an input stream or whatever else it needs) to the test runner to define which test cases to execute, or I could call the test runner for every feature file if needed.  I want to avoid having to statically define the feature files in Java code.

If anyone has any idea how I could do this or can point me in the right direction, please let me know!

Thanks!
Tim

aslak hellesoy

unread,
Dec 12, 2012, 7:06:08 PM12/12/12
to Cucumber Users
On Wed, Dec 12, 2012 at 8:49 PM, Tim Mertens <t...@filmchicago.org> wrote:
Thanks Aslak,

I am looking at the usage document but it is not clear how I can use the cucumber.options property to accomplish my objective.  I can build an options String[] and pass it to the cli main:

cucumber.api.cli.Main.main(opts);

But there are a couple problems with this approach:
  1. The glue "-g" option allows file paths only and does not allow classpath references
Yes it does. Try --glue classpath:some/dir/in/your/class/path 

Tim Mertens

unread,
Dec 13, 2012, 11:00:40 AM12/13/12
to cu...@googlegroups.com
On Wednesday, December 12, 2012 6:02:53 PM UTC-6, Aslak Hellesøy wrote:



On Wed, Dec 12, 2012 at 9:20 PM, Tim Mertens <t...@filmchicago.org> wrote:
I tried setting the cucumber.options System Property, but when I do I receive an exception. For example:

System.setProperty("cucumber.options", " --monochrome");
run
= junitRunner.run(com.lyonscg.qa.taf.tests.magento.RunTest.class);


You shouldn't be using a space in the arguments. Try "--monochrome".
 

My mistake, thanks! When I remove the space it works OK and the exception below does not occur.
 

Tim Mertens

unread,
Dec 13, 2012, 11:17:48 AM12/13/12
to cu...@googlegroups.com


On Wednesday, December 12, 2012 6:06:08 PM UTC-6, Aslak Hellesøy wrote:



On Wed, Dec 12, 2012 at 8:49 PM, Tim Mertens <t...@filmchicago.org> wrote:
Thanks Aslak,

I am looking at the usage document but it is not clear how I can use the cucumber.options property to accomplish my objective.  I can build an options String[] and pass it to the cli main:

cucumber.api.cli.Main.main(opts);

But there are a couple problems with this approach:
  1. The glue "-g" option allows file paths only and does not allow classpath references
Yes it does. Try --glue classpath:some/dir/in/your/class/path 

This was giving me an error similar to what I received when using the " --monochrome" option in my previous post when I was using the CLI. I probably was doing something wrong.  I tried again today using the JUnit runner and got past the issue I was seeing before and now have a new problem. I will investigate and update with more info if I can't get past it.

Tim Mertens

unread,
Dec 20, 2012, 12:00:10 PM12/20/12
to cu...@googlegroups.com
Aslak,

I got this working.  From my main() class where I execute the tests, here is an example:

System.setProperty("cucumber.options", "--glue com.tests.glue classpath:com/tests/HelloWorld.feature");

run
= junitRunner.run(com.tests.vanillaTest.RunTest.class);

My RunTest class is a vanilla test class with only the format specified so I can pass whatever glue and features to it that I need to:

package com.tests.vanillaTest;

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@Cucumber.Options( format={"pretty","html:target/cucumber", "json-pretty:target/cucumber-report.json"} )
public class RunTest {
}


I did get some weird errors at first from the Cucumber constructor about not being able to call the super() constructor of the RunTest class for some reason. I added an empty constructor and it resolved it but as you can see in my example above, when I removed the constructor later on the tests continued running. I have no idea why it stopped then started working again, but adding the constructor like below seemed to resolve the issue. Unfortunately since it is working now, I do not have a copy of the error to post.

public class RunTest {
   
public RunTest() { }
}

Cheers!
Tim



On Monday, December 10, 2012 5:07:11 PM UTC-6, Tim Mertens wrote:
Hi,

I am using cucumber-jvm and am wondering if/how I can tell Cucumber what feature files to run at runtime, rather than by specifying the  features at compile time:

Aslak Hellesøy

unread,
Dec 20, 2012, 2:40:58 PM12/20/12
to cu...@googlegroups.com

On Thursday, 20 December 2012 at 11:00, Tim Mertens wrote:

Aslak,

I got this working.  From my main() class where I execute the tests, here is an example:

System.setProperty("cucumber.options", "--glue com.tests.glue classpath:com/tests/HelloWorld.feature");

run
= junitRunner.run(com.tests.vanillaTest.RunTest.class);

My RunTest class is a vanilla test class with only the format specified so I can pass whatever glue and features to it that I need to:

package com.tests.vanillaTest;

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@Cucumber.Options( format={"pretty","html:target/cucumber", "json-pretty:target/cucumber-report.json"} )
public class RunTest {
}


I did get some weird errors at first from the Cucumber constructor about not being able to call the super() constructor of the RunTest class for some reason. I added an empty constructor and it resolved it but as you can see in my example above, when I removed the constructor later on the tests continued running. I have no idea why it stopped then started working again, but adding the constructor like below seemed to resolve the issue.
Thanks for sharing. The javac compiler will create an enpty constructor if none are defined, so adding an empty one is not necessary.

Aslak 
Unfortunately since it is working now, I do not have a copy of the error to post.

public class RunTest {
   
public RunTest() { }
}

Cheers!
Tim


On Monday, December 10, 2012 5:07:11 PM UTC-6, Tim Mertens wrote:
Hi,

I am using cucumber-jvm and am wondering if/how I can tell Cucumber what feature files to run at runtime, rather than by specifying the  features at compile time:

@RunWith(Cucumber.class)
@Cucumber.Options(features={"classpath:com/test/my/feature.feature"})
public class RunTest {}



For reasons specific to my environment, I am using the following code to execute specific test cases from an executable jar:

junitRunner.run(com.test.RunTest.class);

I can use custom @RunWith(Cucumber.class) classes like the one above to run specific feature files, but I would really like to be able to supply a list of .feature files (or even an input stream or whatever else it needs) to the test runner to define which test cases to execute, or I could call the test runner for every feature file if needed.  I want to avoid having to statically define the feature files in Java code.

If anyone has any idea how I could do this or can point me in the right direction, please let me know!

Thanks!
Tim

--
Reply all
Reply to author
Forward
0 new messages