Back Home

Purposes

  • Documentation of the system
    • Automated, executable acceptance tests

* Written in a human readable format

  • Business readable DSL
  • Describe software behavior without detailing implementation
  • Feature source files have .feature extension

Getting Started

Add to gradle

    compile 'info.cukes:cucumber-groovy:1.2.5'

In intelij create a test.feature file in src/test/groovy

Scenarios consist of steps

  • Given: Put the system in a known state before the user starts interacting with the system; pre-conditions
  • When: Describe the key action a user performs in this scenario
  • Then: Observe outcomes; observations should relate to business value of the feature

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 {
} 

Table Driven Examples

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
}

Table Driving Tests

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  |

Hooks

  • @Before Executes after the last step of each scenario
  • @Execute regardless if there are failing, undefined, pending or skipped steps
  • @AfterStep Executes after each step in a scenario
  • @database tag to load the database via DBUnit

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()
}   

Cucumber JVM

  • Cucumber-JVM is a pure Java implementation of Cucumber

Hello World

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

Cucumber-jvm Options

There are a number of options to manage the test execution:

  • Options can be configured to run a directory
     @Options(features="relative/path/to/features")

* To run individual features

@Cucumber.Options(features={"001-OQ_List.feature", "002-OQ_List.feature"})

Using Tags

Tests can be tags and then executing accordingly

@Functional Feature: Your feature 1 here

@Integration Feature: Your feature 2 here in another file

Mule Cucumber Hooks

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  {
 
	}
 
 
}
 
jvm_bdd.txt · Last modified: 2017/08/24 08:05 by root
 
RSS - 200 © CrosswireDigitialMedia Ltd