Skip to content

Commit 5e55869

Browse files
authored
Merge pull request #27 from cucumber/documentation
Add more documentation on Scala specificities
2 parents 9fe55d3 + 562262e commit 5e55869

File tree

7 files changed

+282
-48
lines changed

7 files changed

+282
-48
lines changed

README.md

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
1-
# cucumber-jvm-scala
2-
[![Maven Central](https://img.shields.io/maven-central/v/io.cucumber/cucumber-scala_2.12.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.cucumber%22%20AND%20a:%22cucumber-scala_2.12%22)
3-
[![Build Status](https://travis-ci.org/cucumber/cucumber-jvm-scala.svg?branch=master)](https://travis-ci.org/cucumber/cucumber-jvm-scala)
4-
5-
6-
## Dependency
1+
# Cucumber Scala
72

8-
### SBT
9-
To use cucumber-jvm-scala in your project, add the following line to your `build.sbt`
10-
11-
```scala
12-
libraryDependencies += "io.cucumber" %% "cucumber-scala" % "4.7.1" % Test
13-
```
3+
[![Maven Central](https://img.shields.io/maven-central/v/io.cucumber/cucumber-scala_2.13.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22io.cucumber%22%20AND%20a:%22cucumber-scala_2.13%22)
4+
[![Build Status](https://travis-ci.org/cucumber/cucumber-jvm-scala.svg?branch=master)](https://travis-ci.org/cucumber/cucumber-jvm-scala)
145

15-
### Maven
16-
To use cucumber-jvm-scala in your project, add the following dependency to your `pom.xml`:
6+
Cucumber Scala is the Scala implementation of [Cucumber](https://cucumber.io/).
177

8+
## Help & Support
189

19-
```xml
20-
<dependency>
21-
<groupId>io.cucumber</groupId>
22-
<artifactId>cucumber-scala_2.12</artifactId>
23-
<version>4.7.1</version>
24-
<scope>test</scope>
25-
</dependency>
26-
```
10+
See: https://cucumber.io/support
2711

2812
## Compatibility matrix
2913

@@ -35,33 +19,25 @@ which can be different.
3519
| 5.6.0 | 5.6.0 | 2.11, 2.12, 2.13 |
3620
| 4.7.1 | 4.7.1 | 2.11, 2.12, 2.13 |
3721

38-
## Migrating from 4.x to 5.x
39-
40-
If you are using Cucumber Scala 4.7.x and want to upgrade to 5.x, please note there are some major changes in addition to the Cucumber upgrade itself.
41-
42-
### Packages
43-
44-
All Cucumber Scala classes are now under `io.cucumber.scala` package instead of `cucumber.api.scala`.
45-
46-
### Before/BeforeStep/After/AfterStep definitions
47-
48-
The `Before`, `BeforeStep`, `After` and `AfterStep` definitions have slightly changed:
49-
- to apply only with some tags, the variable list of tags as `String*` is replaced by a single tag expression of type `String`.
50-
- if providing both an order and a tag expression, the order is now the second parameter instead of the first.
51-
This is more consistent with the Java implementation.
22+
## Getting started
5223

53-
For instance, the following code:
24+
- [Installation](./docs/install.md)
25+
- Upgrade notes
26+
- [Version 5.x](docs/upgrade_v5.md)
27+
- Documentation
28+
- [Basic usage](docs/usage.md)
29+
- [Step Definitions](docs/step_definitions.md)
30+
- [Hooks](docs/hooks.md)
31+
- [Example project](examples/README.md)
32+
- [Reference documentation for Java](https://docs.cucumber.io/docs/cucumber/)
33+
- [Changelog](https://github.com/cucumber/cucumber-jvm-scala/releases)
5434

55-
```scala
56-
Before(1, "@tag1", "@tag2") { _ =>
57-
// Do Something
58-
}
59-
```
35+
## Contributing
6036

61-
Is replaced by:
37+
Any contribution is welcome:
38+
- completing or fixing documentation
39+
- reporting or fixing issue
40+
- reporting missing feature (compared to other implementations)
41+
- developing a new feature
6242

63-
```scala
64-
Before("@tag1 or @tag2", 1) { _ =>
65-
// Do Something
66-
}
67-
```
43+
Please use this Github project for contributing, either through an issue or a Pull Request.

docs/hooks.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Hooks
2+
3+
Hooks are blocks of code that can run at various points in the Cucumber execution cycle.
4+
They are typically used for setup and teardown of the environment before and after each scenario.
5+
6+
See the [reference documentation](https://docs.cucumber.io/docs/cucumber/api/#hooks).
7+
8+
## Scenario hooks
9+
10+
Scenario hooks run for every scenario.
11+
12+
### Before
13+
14+
`Before` hooks run before the first step of each scenario.
15+
16+
```scala
17+
Before { scenario : Scenario =>
18+
// Do something before each scenario
19+
}
20+
```
21+
22+
### After
23+
24+
`After` hooks run after the last step of each scenario.
25+
26+
```scala
27+
After { scenario : Scenario =>
28+
// Do something after each scenario
29+
}
30+
```
31+
32+
## Step hooks
33+
34+
Step hooks invoked before and after a step.
35+
36+
### BeforeStep
37+
38+
```scala
39+
BeforeStep { scenario : Scenario =>
40+
// Do something before step
41+
}
42+
```
43+
44+
### AfterStep
45+
46+
```scala
47+
AfterStep { scenario : Scenario =>
48+
// Do something after step
49+
}
50+
```
51+
52+
## Conditional hooks
53+
54+
Hooks can be conditionally selected for execution based on the tags of the scenario.
55+
56+
```scala
57+
Before("@browser and not @headless") { _ =>
58+
// Do something before each scenario with tag @browser but not @headless
59+
}
60+
```
61+
62+
## Order
63+
64+
You can define an order between multiple hooks.
65+
66+
```scala
67+
Before(10) { _ =>
68+
// Do something before each scenario
69+
}
70+
71+
Before(20) { _ =>
72+
// Do something before each scenario
73+
}
74+
```
75+
76+
The **default order is 1000**.
77+
78+
## Conditional and order
79+
80+
You mix up conditional and order hooks with following syntax:
81+
```scala
82+
Before("@browser and not @headless", 10) { _ =>
83+
// Do something before each scenario
84+
}
85+
```

docs/install.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Installation
2+
3+
## Dependency
4+
5+
### SBT
6+
7+
To use Cucumber Scala in your project, add the following line to your `build.sbt`:
8+
9+
```scala
10+
libraryDependencies += "io.cucumber" %% "cucumber-scala" % "4.7.1" % Test
11+
```
12+
13+
### Maven
14+
15+
To use Cucumber Scala in your project, add the following dependency to your `pom.xml`:
16+
17+
```xml
18+
<dependency>
19+
<groupId>io.cucumber</groupId>
20+
<artifactId>cucumber-scala_2.12</artifactId>
21+
<version>4.7.1</version>
22+
<scope>test</scope>
23+
</dependency>
24+
```

docs/step_definitions.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Step definitions
2+
3+
Step definitions (`Given`, `When`, `Then`) are the glue between features written in Gherkin and the actual tests implementation.
4+
5+
Cucumber supports two types of expressions:
6+
7+
- Cucumber expressions
8+
- Regular expressions
9+
10+
See also the [reference documentation](https://docs.cucumber.io/docs/cucumber/step-definitions/#expressions).
11+
12+
## Cucumber expressions
13+
14+
[Cucumber expressions](https://docs.cucumber.io/docs/cucumber/cucumber-expressions/)
15+
16+
The following Gherkin step:
17+
```gherkin
18+
Given I have 42 cucumbers in my belly
19+
```
20+
21+
Can be implemented with following Cucumber Expression in Scala:
22+
```scala
23+
Given("""I have {int} cucumbers in my belly"""){ (cucumberCount: Int) =>
24+
// Do something
25+
}
26+
```
27+
28+
## Regular expressions
29+
30+
The following Gherkin step:
31+
```gherkin
32+
Given I have 42 cucumbers in my belly
33+
```
34+
35+
Can be implemented with following Regular Expression in Scala:
36+
```scala
37+
Given("""^I have (\d+) cucumbers in my belly$"""){ (cucumberCount: Int) =>
38+
// Do something
39+
}
40+
```

docs/upgrade_v5.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Upgrading from 4.x to 5.x
2+
3+
If you are using Cucumber Scala 4.7.x and want to upgrade to 5.x, please note there are some major changes in addition to the Cucumber upgrade itself.
4+
5+
Starting from version 5.x, Cucumber Scala will try to be as close as possible to the Cucumber Java implementation while remaining Scala oriented.
6+
This means that package names, parameters order, internal code... will be more consistent with the Java implementation.
7+
8+
## Packages
9+
10+
All Cucumber Scala classes are now under `io.cucumber.scala` package instead of `cucumber.api.scala`.
11+
12+
## Hooks
13+
14+
The `Before`, `BeforeStep`, `After` and `AfterStep` definitions have slightly changed:
15+
- to apply only to scenarios with some tags, the `String*` parameter is replaced by a single tag expression of type `String`.
16+
This changes comes from Cucumber itself.
17+
- if providing both an order and a tag expression, the order is now the second parameter instead of the first.
18+
This is more consistent with the Java implementation.
19+
20+
For instance, the following code:
21+
22+
```scala
23+
Before(1, "@tag1", "@tag2") { _ =>
24+
// Do Something
25+
}
26+
```
27+
28+
Is replaced by:
29+
30+
```scala
31+
Before("@tag1 or @tag2", 1) { _ =>
32+
// Do Something
33+
}
34+
```
35+
36+
See also the [Hooks documentation](hooks.md).

docs/usage.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Basic Usage
2+
3+
## Glue code
4+
5+
To use Cucumber Scala, all your glue code (steps or hooks) has to be defined in **classes** extending both the `ScalaDsl` trait and a language trait.
6+
7+
For instance, to use the English flavour:
8+
```scala
9+
import io.cucumber.scala.{EN, ScalaDsl}
10+
11+
class MyGlueClass extends ScalaDsl with EN {
12+
13+
// Here some steps or hooks definitions
14+
15+
Given("""I have {int} cucumbers in my belly"""){ (cucumberCount: Int) =>
16+
// Do something
17+
}
18+
19+
}
20+
```
21+
22+
Cucumber will automatically load all the glue code defined in classes available in the "glue path" (more details in the Run documentation) inheriting `ScalaDsl`.
23+
24+
### Using traits
25+
26+
You can define glue code in **traits** as well and have a **class** extending the traits you need.
27+
28+
For instance, you can do things like this:
29+
```scala
30+
import io.cucumber.scala.{EN, ScalaDsl}
31+
32+
trait StepsForThis extends ScalaDsl with EN {
33+
// Glue code
34+
}
35+
36+
trait StepsForThat extends ScalaDsl with EN {
37+
// Glue code
38+
}
39+
40+
class MyStepDefinitions extends StepsForThis with StepsForThat {
41+
}
42+
```
43+
44+
**Note:** using traits can help you split your tests in different groups and provide some steps only to some tests.
45+
46+
### Using objects
47+
48+
You can also define glue code in **objects**.
49+
50+
It's **not recommended** though, because by definition objects are singleton and if your glue code is stateful you will probably have "state conflicts" between your scenarios.
51+
52+
## Running Cucumber tests
53+
54+
See also the Running Cucumber for Java [documentation](https://docs.cucumber.io/docs/cucumber/api/#running-cucumber).
55+
56+
Add the `cucumber-junit` dependency to your project.
57+
58+
Then create a runner class like this:
59+
```scala
60+
import io.cucumber.junit.{Cucumber, CucumberOptions}
61+
import org.junit.runner.RunWith
62+
63+
@RunWith(classOf[Cucumber])
64+
@CucumberOptions()
65+
class RunCucumberTest
66+
```
67+
68+
You can define several options like:
69+
- the "glue path" (default to current package): packages in which to look for glue code
70+
- the "features path" (default to current folder): folder in which to look for features file

examples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Cucumber Scala Example
2+
3+
This project is an example of Cucumber Scala integration in a Scala Maven project.

0 commit comments

Comments
 (0)