Back Home
Add to gradle
compile 'info.cukes:cucumber-groovy:1.2.5'
In intelij create a test.feature file in src/test/groovy
In Gherkin support Given/When/Then and table driven
Feature: Hello World Scenario: Say hello Given I have a hello app with the following names Howdy When I ask it to say hi Then it should answer with "Howdy"
public class HelloWorldStepDef { private Hello hello; private String hi; @Given("^I have a hello app with \"([^\"]*)\"$") public void I_have_a_hello_app_with(String greeting) { hello = new Hello(greeting); } @When("^I ask it to say hi$") public void I_ask_it_to_say_hi() { hi = hello.sayHi(); } @Then("^it should answer with \"([^\"]*)\"$") public void it_should_answer_with(String expectedHi) { assertEquals(expectedHi, hi); } }
And run as a normal junit test class
import org.junit.runner.RunWith; import cucumber.api.junit.Cucumber; @SuppressWarnings("deprecation") @RunWith(Cucumber.class) @Cucumber.Options(format = {"pretty", "html:target/cucumber"}, features = "src/test/resources/bskyb" ) public class RunCukesTest { }
Given I have the following foods |name |healthy| |Orange|Yes | |Chips |No | When I count the number of healthy items Then I have 1 healthy item
In code the test data is regular expressioned out:
Given(~'I have the following foods') { table -> basket = new Basket() table.hashes().each { def item = new Food( name: it.name, // it.get('name') healthy: it.healthy == 'Yes') basket.add(item) } } When(~'I count the healthy items') { numberOfHealthy = basket.numberOfHealthy } Then(~'I have (.+) healthy items') { int count -> assert numberOfHealthy == count }
Tables on the given provide a batch of data Scenario Outline can be used to iterate on the test
Scenario Outline: Eating Given there are <start> cucumbers When I eat <eat> cucumbers Then I should have <left> cucumbers Examples: | start | eat | left | | 12 | 5 | 7 | | 20 | 5 | 15 |
Custom tag's can be used to configure the initialization of each test:
Before { // Initialise something } Before('@tagname') { // Initialise only for features/scenarios // tagged with @tagname } Before('~@tagname') {} // not tagged After { // Clean up something }
Clean-up Database Hook example:
After () { def sessionFactory = appCtx.getBean("sessionFactory") sessionFactory.currentSession.flush() def dataSource = appCtx.getBean("dataSource") //clean fixtures println "Deleting the fixture..." def db = new Sql(dataSource) db.execute("DELETE FROM CANDIDATE;") sessionFactory.currentSession.clear() }
Say hello and validate it was output
hello.feature
Feature: Hello World Scenario: Say hello Given I have a hello app with "Howdy" When I ask it to say hi Then it should answer with "Howdy World"
import cucumber.junit.Cucumber; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @Cucumber.Options(format = {"pretty", "html:target/cucumber"}) public class RunCukesTest { }
The Cucumber test can be run as a normal junit test
There are a number of options to manage the test execution:
@Options(features="relative/path/to/features")
@Cucumber.Options(features={"001-OQ_List.feature", "002-OQ_List.feature"})
@ExtendedCucumberOptions(jsonReport = "target/cucumber.json", retryCount = 3, detailedReport = true, detailedAggregatedReport = true, overviewReport = true, toPDF = true, outputFolder = "target")
A good list of the Options list https://mkolisnyk.github.io/cucumber-reports/extended-cucumber-runner
Tests can be tags and then executing accordingly
@Functional Feature: Your feature 1 here
@Integration Feature: Your feature 2 here in another file
Needs gradle dependencies :
compile 'io.cucumber:cucumber-java8:3.0.2' compile 'io.cucumber:cucumber-spring:3.0.2'
The JUnit runner uses the JUnit framework to run the Cucumber Test. All we need is to create a single empty class with an annotation @RunWith(Cucumber.class):
@RunWith(Cucumber.class) @CucumberOptions(features = "src/test/resources") public class CucumberTest { }
import javax.annotation.Resource; import org.apache.activemq.broker.BrokerService; import org.mule.api.MuleContext; import org.mule.module.client.MuleClient; import org.mule.tck.junit4.FunctionalTestCase; import org.springframework.test.context.ContextConfiguration; import com.bskyb.gonzales.TestContext; import cucumber.api.java.After; import cucumber.api.java.Before; @ContextConfiguration("classpath:cucumber.xml") public class Hooks { @Resource public TestContext testContext; BrokerService broker = null; private class MuleTest extends FunctionalTestCase { @Override protected String getConfigResources() { return "rbsa-ingestor.xml"; } public MuleContext getMuleContext() { return muleContext; } } private MuleTest muleTest = null; @Before("@muletest") public void startMule() throws Exception { muleTest = new MuleTest(); muleTest.setUpMuleContext(); MuleClient client = new MuleClient(testContext.muleContext); testContext.muleContext = muleTest.getMuleContext(); } @After("@muletest") public void stopMule() throws Exception { muleTest.disposeContext(); } @Before("@activemqtest") public void startActiveMQ() throws Exception { } @After("@activemqtest") public void stopActiveMQ() throws Exception { } }
Reports can be generated:
The report builder assumes that the tests have been configured to create a json file first. The report builder is the pointed to the the Json output file.
The following code sample generates results overview report based on Cucumber JSON report stored at ./src/test/resources/cucumber.json location. The output directory is target and the file prefix is cucumber-results. The code is:
CucumberResultsOverview results = new CucumberResultsOverview(); results.setOutputDirectory("target"); results.setOutputName("cucumber-results"); results.setSourceFile("./src/test/resources/cucumber.json"); results.executeFeaturesOverviewReport();
As the result of this code there would be target/cucumber-results-feature-overview.html file generated. Cucumber usage report works in the same fashion except it uses report produced as the part of usage Cucumber JVM plugin. The following example generates usage report into the target folder based on JSON usage report generated by Cucumber and located at ./src/test/resources/cucumber-usage.json path:
CucumberUsageReporting report = new CucumberUsageReporting(); report.setOutputDirectory(“target”); report.setJsonUsageFile(“./src/test/resources/cucumber-usage.json”); report.executeReport(); The output would be the HTML file located at target/cucumber-usage-report.html. This is how those reports are generated. Now let's take a look at what exactly is generated.