Feature options
To implement a CornichonFeature it is only required to implement the feature function. However, a number of useful options are available using override.
Before and after hooks
Hooks are available to set up and tear down things as usual but this feature is not integrated into the DSL.
Four functions are available in CornichonFeature with self-explanatory names:
Taking a Unit expression
beforeFeature {
// do side effect here
}
afterFeature {
// do side effect here
}
Taking a Step expression similar to the main DSL. You can either pass a single regular Step or a WrapperStep like Attach.
Here is an example with fictitious steps.
beforeEachScenario {
Attach {
Given I setup_server
Then assert setup_successful
}
}
afterEachScenario {
Then I cleanup_resources
}
Base URL
Instead of repeating at each HTTP statement the full URL, it is possible to set a common URL for the entire feature by overriding:
override lazy val baseUrl = s"http://localhost:8080"
and then only provide the missing part in the HTTP step definition
When I get("/superheroes/Batman")
When I delete("/superheroes/GreenLantern")
You can still override the base URL of a single step by providing the complete URL starting with the HTTP protocol.
Request timeout
The default value for the HTTP request timeout is 2 seconds. As always it can be overridden per scenario.
import scala.concurrent.duration._
override lazy val requestTimeout = 100.millis
Seed
On a failure the initial seed will be provided in the error reporting enabling you to replay the exact same test even if it contains source of randomness such as:
- randomized placeholders (random-uuid, random-string, random-boolean etc.)
- property based testing generators & transitions
- custom steps using
ScenarioContext.randomContext
RandomMapperas extractor
override lazy val seed: Option[Long] = Some(1L)
Register custom extractors
Custom extractors let you define reusable placeholder mappings that automatically extract values from the session. See Custom extractors in the Placeholders page.
Execution model
By default, features are executed sequentially and scenarios within are executed in parallel.
This execution is configurable if you have specific constraints.
To run scenarios sequentially it is necessary to declare in your application.conf file
cornichon {
executeScenariosInParallel = false
}
The actual number of concurrent scenarios is controlled via the configuration field scenarioExecutionParallelismFactor which defaults to 1.
number of concurrent scenarios = `scenarioExecutionParallelismFactor` * number of CPU + 1
This means using more powerful machines will automatically trigger more scenarios.
To run features in parallel it is necessary to manually set a flag in your SBT build file.
Test / parallelExecution := true
Focusing on a scenario
During development, you can focus on a single scenario within a feature. All other scenarios will be ignored.
Scenario("the one I'm debugging").focused {
When I get("http://superhero.io/batman")
Then assert status.is(200)
}
Do not commit focused scenarios to your main branch — they will silently skip all other scenarios in the feature.
Ignoring features or scenarios
Feature or individual scenario can also be marked to be ignored.
import com.github.agourlay.cornichon.CornichonFeature
class CornichonExamplesSpec extends CornichonFeature {
// Ignore a complete feature
def feature = Feature("Checking google").ignoredBecause("Your reasons ..."){
// Ignore a single scenario
Scenario("Google is up and running").ignoredBecause("Your reasons ..."){
When I get("http://google.com")
Then assert status.is(302)
}
}
}
Pending scenario
During development, you may want to remember that a scenario needs to be created, but you’re not ready to write it yet.
import com.github.agourlay.cornichon.CornichonFeature
class CornichonPendingExamplesSpec extends CornichonFeature {
def feature = Feature("Some title"){
Scenario("this important use case").pending
Scenario("that important edge case").pending
}
}