layout | title |
---|---|
bootstrap |
Databases |
Data stored in one scenario shouldn't be available to the next scenario. This just makes your scenarios brittle and impossible to run in isolation.
This can be done either by deleting all data in a Before Hook (running before each Scenario) or to wrap a transaction (if your database supports it) around each Scenario.
If your database supports transactions, you can tell Cucumber to start a transaction in a Before Hook and roll it back
in an After Hook. This is such a common thing to do that several Cucumber extensions provide ready-to-use
Tagged Hooks using a tag named @txn
. To enable it you have to tag every Feature or Scenario that needs transactions with @txn
:
{% highlight gherkin %} @txn Feature: Let's write a lot of stuff to the DB Scenario: I clean up after myself Given I write to the DB Scenario: And so do I! Given I write to the DB {% endhighlight %}
cucumber-spring
module contains @txn
hooks in the cucumber.runtime.java.spring.hooks
package.
This package isn't on your Glue Path by default, so you have to add it yourself in your Configuration Options.
{% highlight java %} @RunWith(Cucumber.class) @Cucumber.Options(glue = {"your.own.glue.code", "cucumber.runtime.java.spring.hooks"}) public class RunCukesTest { } {% endhighlight %}
{% highlight text %} --glue your.own.glue.code --glue cucumber.runtime.java.spring.hooks {% endhighlight %}
See the spring-txn example in Cucumber-JVM for a minimal setup.
If you're using a Browser Automation tool that talks to your application over HTTP the transactional approach will not work if your Step Definitions and the web application serving HTTP request each have their own database connection.
TODO: Write some more here.