Table of Contents |
---|
Testing
Unit tests
JUnit tests are the simple and fast tests that use mocks to check simple, single functionality. You should write JUnit to check every function that you use in your code. JUnit must be fast, it should be matters of seconds to run that test.
...
Runner file like all tests should be located in (...)/<moduleName>/src/test/java/AcceptanceTest<TestedClassName>.java
Feature
A feature is a Use Case that describes a specific function of the software being tested. There are three parts to a Feature
The Feature: keyword
The Feature name (on the same line as the keyword)
An optional description on the following lines
Example Feature definition
Code Block |
---|
Feature: Withdraw Money from ATM
A user with an account at a bank would like to withdraw money from an ATM.
Provided he has a valid account and debit or credit card, he should be allowed to make the transaction. The ATM will tend the requested amount of money, return his card, and subtract amount of the withdrawal from the user's account.
Scenario: Scenario 1
Given preconditions
When actions
Then results
Scenario: Scenario 2
... |
Steps Definitions
It is a file which contains steps definitions. It contains source code which should be done within a test step. Parameters defined in feature files are passed to step definitions with regular expressions. To make them work correctly, you should configure methods for each step defined in Feature file. Each step is preceded by an annotation connected to step keywords defined in feature file (e.g. @Then, @When etc.).
Example step definition:
Code Block |
---|
@Given("I have a cucumber step")
public void i_have_a_cucumber_step() throws Throwable {
System.out.println("Step definition exact match"); |
Runner file
In Runner file you have to set all options about tests, like which features are tested or where steps are located. There's few more options to configure but they are optional, the main thing of this file is that he making our test code working.
Required options are:
- format - It describes how cucumber will format test case output and if it create reports.
- glue - It contains path to package containing Step definition files
- features - It contains path to feature file.
Example runner:
Code Block |
---|
package pl.wroc.pwr.qualityspy.vc;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
monochrome = true,
format = { "pretty", "html:target/cucumber" },
glue = "pl.wroc.pwr.qualityspy.vc.steps",
features = "classpath:cucumber/Repository.feature"
)
public class AcceptanceTestRepositoryCucumber {
} |
To run our test all we have to do is just run our test
If everything was configured correct, we should got message about completed tests.
After completed tests Cucumber automatically generate report file, where you can check what was tested, or if something went wrong where was the problem. Report file is generated as file (...)/<moduleName>/target/cucumber/index.html.