Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

2 like 0 dislike
38.6k views
in Test Automation by
I am using Cucumber Java to write tests. I have many feature files in Eclipse. How do I execute only one feature file out of many?

4 Answers

1 like 0 dislike
by
Hi,

A quick solution is using the below method.

"@Cucumber.Options(features = { "path.1_feature", "path.2_feature" },"

I tried and work fine for me. You can choose to just call one feature file or many.
0 like 0 dislike
by The go-to Tester (181 points)
By creating one test runner class for each .feature file, it can be
triggered by itself as any other JUnit test class. Example:

package <snip>

import <snip>

@RunWith(Cucumber.class)
@Cucumber.Options(features="src/test/resources/path/feature1.feature",
format={"progress", "junit:target/cucumber-feature1-report.xml"})
public class RunFeature1Test {
}

Triggering this as any other JUnit test class, would execute only the
feature1.feature.
format="junit:target/cucumber-feature1-report.xml" instructs Cucumber-JVM
to create a JUnit result file in the target directory.
This may not be necessary, since RunFeature1Test behaves as any other JUnit
test class. For instance the maven-surefire-plugin will by itself create
test result files for the classes like RunFeature1Test.

It is also possible to use the command line interface to execute a single
.feature file, but many test frameworks handle JUnit tests, so launching a
new JVM for each .feature file seems unnecessary.
0 like 0 dislike
by The go-to Tester (181 points)

Running Feature files directly with Eclipse:

1: Right click on the feature file and select "Debug As" or "Run As"

Create a new "Java Application" execution.

2: Under the "Main" tab....

2.1 It should have your Project name in the "Project:" field.... TODO: test replacing with a system prop for the currently selected files project* 2.2 Enter "cucumber.api.cli.Main" without quotes for the "Main class" field..

3: Under the tab for "Arguments"

3.1 For "Program Arguments", enter...

${selected_resource_loc}
--glue com.your.glue.location
--plugin pretty
--monochrome

This will allow any selected feature file to be run, and ensure useful logging is sent to the IDE's console. Note that Eclipse's console does not support ANSI color (and delete prior line) console colors.

3.2 Under "VM Arguments..."

-ea

if you want to pass in system parameters, do so here:

-DtargetAppType=MobileWebApp

This will turn on java assert() statements, which is need to allow the Then steps to throw assertion exceptions if the code a test is testing is broken.

STEPS 1 - 3 will be saved so you will not have to repeat them again.

4. Click the "Run" or "Debug" button.

0 like 0 dislike
by The go-to Tester (181 points)

Running Feature files directly with IDEA (up to IntelliJ 11, since IntelliJ 12 supports cucumber-jvm natively)

There are two possible ways to run the application in IDEA

  1. One is running the selected feature file via an "external tool", that tool happens to be java... more in a second.
  2. The other way is run the the test as via a main method with run/debug as application functionality.

The trade offs:

  • When running an external tool you can not (easily) debug the test.
  • When running as a java main method you will have to type in the feature file name... very annoying.

Recommendation: run as an external tool when running tests, and debug as a main method application as your about to go through a very annoying process anyway.

Configuring External tool execution in IDEA:

  1. Open "Settings"
  2. Under "External Tools" menu
  3. Click "add"
  4. On the "Edit Tool" enter the following values with out quotes for each field
    • Name: "Run"
    • Group: "Cucumber"
    • Description: "Cucumber"
    • Program: "$JDKPath$/bin/java" (reverse the slashes on windows, maybe leave quotes if spaces in path)
    • Parameters: "-cp $Classpath$:$OutputPath$ cucumber.api.cli.Main $FilePath$ --glue your.glue.path --format gherkin.formatter.PrettyFormatter" (replace : with ; on windows and add "" around $Classpath$:$OutputPath$)
    • Working Directory: "$ModuleFileDir$/target" (reverse the slash on windows)
  5. Press Save

    Now you can right click on any *.feature file and click on the "Cucumber" -> "Run" menu and you will run that test.

Configuring Application run in IDEA:

  1. Under the "Run" menu select "Edit Configuration..."
  2. On the "Run/Debug Window"
  3. Cick the "+" button and select "Application"
  4. For the following field the the values without quotes
    • Name: "Cucumber"
    • Main method: "cucumber.api.cli.Main"
    • VM Options: "-ea"
    • Program Arguments: "$FilePath$ --glue com.your.gluecode.path --format gherkin.formatter.PrettyFormatter"
    • Working Directory: "$MODULE_DIR$/target/" (reverse the slashes on windows)
    • Use classpath of Module: Make sure your module is selected...

You may also pass system properties in the VM Arguements section with -D parameters like -DtargetAppType=MobileWebApp

The problem with this approach:

You will have to replace the $FilePath$ with the actual full path of your feature file.

Then you will be able to debug the test.

If you are using IntelliJ IDEA, consider voting for IDEA-81672.


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

1.4k questions

1.6k answers

866 comments

1.9k users

...