Scala Cucumber from IntelliJ and the command line with SBT

In this post we’ll see how to have Cucumber tests running in IntelliJ and command line using SBT.

Adding Cucumber to the project and how to configure SBT multi-module won’t be covered this time as these topics are well covered elsewhere.

Motivation

Executing Cucumber tests from IntelliJ is necessary to help during development as you just want to start your application and run the Cucumber tests right from the IDE.

Executing Cucumber tests on SBT command line is useful when you want to add the Cucumber tests as part of your application deployment pipeline.

From this point “test” means “Cucumber test”

Project architecture

This post is based on a multi-module SBT project. The separation in this architecture allows running functional tests against the same application deliverable that will be eventually running in production. The module discussed in this post is rhAcceptance:

rh-project-structure

Project architecture with modules

Before choosing to go with the fat jar approach, I tried to configure the test to execute from its natural location PROJECT_ROOT/rhAcceptance/src/test. Having Googled it quite extensively it seems only “Scalatests” are found by default in SBT.

rh-acceptance-not-running

Cucumber test was in the scope but it never ran

There are a couple of third-party plugins that enable Cucumber tests to be run as part of sbt test goal. I haven’t used them as I am wary of depending on third-party plugins with few contributors and, in this case, it was cheap to solve the problem in the way described below.

The alternative solution was to move the tests PROJECT_ROOT/rhAcceptance/src/main directory and run it as a fat jar.

rh-acceptance-test-in-main

In the acceptance test module, Cucumber tests and feature files have bee moved to the ‘main’ directory instead of ‘test’

Fat jar in SBT

In order to get the build.sbt creating a fat jar, I used the assembly plugin. Note that assembly.sbt goes into the SBT project directory.

rh-assembly-plugin

Below the SBT configuration in rhAcceptance module that create a fat jar named acceptance.jar

rh-sbt-module

For the set up above, just run sbt assembly and the fat jar will be created inPROJECT_ROOT/rhAcceptance/target/scala-2.12.

Cucumber from IntelliJ

Don’t forget to install the plugins “Scala”, “Cucumber for Java” and “Cucumber for Scala”.

Create the Cucumber runner as main function:

rh-cucumber-as-main

Cucumber runner as main function

Run the above main with the following configuration:

rh-acceptance-intellij-config

IntelliJ config to run Cucumber tests

The message “Test framework quit unexpectedly” may sound like the test run failed, but we can see that they were run successfully.

rh-acceptance-ran-from-intellij

Cucumber tests run directly from IntelliJ

Cucumber from command line

Note that --glue must point to the root package where your step definitions are located.

The last argument classpath:uk.co.myproject is the location of your feature files, classpath is necessary to load the feature files from inside the jar.

rh-run-acceptance-command-line

Cucumber tests run from command line

Here now we have the same tests run successfully from IntelliJ and the command line.

Advertisements