In the following post I will show how to configure a new project in order to write integration tests with Cucumber, Scala and SBT build tool.
Intellij IDEA 2016.2 CE is used as IDE
Target audience: QA Automation engineers / developers
DISCLAIMER: Any Selenium WebDriver usage will not be found in post as it is out of scope.
1. Cucumber and BDD approach
It is assumed that reader has some experience with Behavior Driven Development approach and a Cucumber test tool.
For information please refer to official Cucumber website.
Also I can highly recommend you two great books about Cucumber:
2. Install Scala and SBT
As we are trying to write BDD tests on Scala, Scala and SBT tool should be installed. For installation please refer to: Scala and SBT installation guides.
3. Install plugins for Intellij IDEA 2016.2 Community Edition
For our needs we should install:
4. Create a Scala SBT project in Intellij IDEA
Just follow the File -> New -> Project -> Scala -> SBT - based project. Wait for some minutes while project is created. In result project will be with the following structure:
5. Handle project dependencies
Add to build.sbt the following dependencies:
NOTE: If IDEA does not pop-up with suggestion to refresh dependencies - open SBT tab on the right panel and click refresh button.
Now just wait for dependencies resolving and download.
6. Add some code to be tested
I have written a simplest MyCalculator scala class and put it into src/main/scala/calc package
7. Write scenarios for the Calculator feature
For our simple caluclator example we will use two simple positive scenarios: one for checking addition and the other is for checking subtraction.
Here it is the full feature written in Gherkin language.
Put feature file in the src/test/resources/features folder.
8. Implement step definitions
Now it is time to implement our step definitions. For that you should create a StepDefinitions.scala class in src/test/scala/steps package.
If it is hard to implement steps from scratch, just go to feature file, right click on the feature name and choose “Run Feature: My Calculator” option.
It will run all scenarios from your feature.
As none of the steps are implemented - the test result output will contain boilerplate code for steps, generated by Cucumber.
For curreny example it will be:
Just Copy/Paste it in StepDefinitions class. Delete all PendingException() code and redundant comments.
For the testing purposes we will share one instance of Calculator between the steps.
In order to achive that just add calculator as class member in StepDefinitions class.
All implementation of the steps will be:
9. Create a test runner
In order to run test scenarios a separate test runner class with custom cucumber options should be created.
Place it into src/test/steps TestRunner scala class.
NOTE: plugin option allows you to set runner to produce test output in the various formats, such as html, json and xml. Description of other Cucumber runner options can be found here
10. Run your tests with test runner
Right click on TestRunner class name and choose Run TestRunner.
See test results output after a while.