Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/martinschneider/cucumber2junit-maven-plugin

Maven plugin to generate plain JUnit tests from Gherkin feature files. This is useful when you want to execute Cucumber tests in an environment which does not allow custom JUnit runners, e.g. the AWS Device Farm.
https://github.com/martinschneider/cucumber2junit-maven-plugin

Last synced: 5 days ago
JSON representation

Maven plugin to generate plain JUnit tests from Gherkin feature files. This is useful when you want to execute Cucumber tests in an environment which does not allow custom JUnit runners, e.g. the AWS Device Farm.

Awesome Lists containing this project

README

        

[Build status](https://travis-ci.com/martinschneider/cucumber2junit-maven-plugin)

# cucumber2junit-maven-plugin

## Background
There are various cloud services which allow running automated tests on simulated or real mobile devices. One of them is [AWS Device Farm](https://aws.amazon.com/device-farm/). Their solution requires uploading the whole test code including all dependencies. This reduces the execution time but also limits the flexibility of how to execute tests.

One major drawback is that **when using Appium and JUnit there is no support for custom test runners**. However, a custom runner (namely `cucumber.api.junit.Cucumber`or an extension of it) is the default way to run Cucumber features with JUnit. Until AWS officially supports Cucumber, there is no straightforward way to run them on their public device farm. They offer dedicated private devices as well which give you full access and therefore also allow running Cucumber tests against. This, however, comes at a different price tag.

## Solution
This Maven plugin offers a workaround. It parses the feature files in a Java project and generates plain JUnit test classes which can be run on public devices within the AWS Device Farm.

## Configuration

Add the following configuration in the `pom.xml` of your project:



aws



io.github.martinschneider
cucumber2junit-maven-plugin
1.2


aws
generate-sources

generate


io.github.martinschneider.demo.tests
io.github.martinschneider.demo.steps







`packageName` and `stepsPackageName` are the only mandatory parameters but in almost all cases you want to overwrite the default template too:

/path/to/custom/template.vm

This is the path to a velocity template which will be used for generating the JUnit test code. See [junitTestClass.vm](src/main/resources/junitTestClass.vm) for the default.

Here are the other possible configuration parameter with their respective default values:

${project.basedir}/src/test/resources/features
${project.basedir}/src/test/java
features
"/tmp/features"

Make sure that the `featuresOutputDirectory` points to a directory that you are allowed to write to in your AWS setup.

Also, add the configuration detailed in the [AWS documentation](https://docs.aws.amazon.com/devicefarm/latest/developerguide/test-types-android-appium-java-junit.html#test-types-android-appium-java-junit-prepare) to package your project and all dependencies into a single zip-file.

## Usage
Then simply run `mvn -Paws package -DskipTests=true`. This will result in a zip-file which can be used to run your tests within the AWS Device Farm.

## Additional documentation
### Customising the Velocity template
This plugin uses Apache Velocity to generate Java source code. You can customise the template to the specific needs of your project. For example you might to execute custom code before every test execution.

You can use the following placeholders within the template:

* `className` the name of the test class (derived from the name of the feature)
* `packageName` the package for the JUnit tests
* `featuresSourceDirectory` the absolute path to the feature files
* `javaOutputDirectory` the absolute path to write the JUnit tests to
* `feature` the feature object (see ... for the public methods it exposes)
* `featuresOutputDirectory` the absolute path to write the feature files to
* `featuresClasspath`, the classpath to load the feature files from
* `scenarios` the scenarios (see ... for the Java code)
* `stepsPackage` the package including the glue code (steps)

The (absolute) path to the template file can be set in the configuration of the plugin.

/path/to/custom/template.vm

If it is not specified the [default](src/main/resources/junitTestClass.vm) will be used.

### Additional placeholders
You can also define a map of additional placeholders for the velocity template:


...

value1
value2

...

Maven resolves system properties within the values so the following example works as well:


${someSystemProp}

You can then pass the value as a parameter when packaging your project: `mvn -Paws -DsomeSystemProp=value package -DskipTests=true`.

All occurences of `$key` will then be replaced with `value` when processing the template..

## Contact
Martin Schneider, [email protected]

[![Buy me a coffee](https://www.buymeacoffee.com/assets/img/guidelines/download-assets-1.svg)](https://www.buymeacoffee.com/mschneider)