Parameterized Steps
given().a_customer_with_name( "John" );
Report
Given a customer with name John
Parameters within the Sentence?
Given there are 5 coffees left
$ to the rescue!
given().there_are_$_coffees_left( 5 );
Parameterized Scenarios
@Test
@DataProvider({
"1, 0",
"3, 2",
"5, 4"})
public void the_stock_is_reduced_when_a_book_is_ordered( int initial,
int left ) {
given().a_customer()
.and().a_book()
.with().$_items_on_stock( initial );
when().the_customer_orders_the_book();
then().there_are_$_items_left_on_stock( left );
}
Uses the DataProviderRunner (github.com/TNG/junit-dataprovider).
Parameterized Runner and Theories of JUnit are also supported
Parameterized Scenarios
Console Output
The stock is reduced when a book is ordered
Given a customer
And a book
With <initial> items on stock
When the customer orders the book
Then there are <left> items left on stock
Cases:
| # | initial | left | Status |
+---+---------+------+---------+
| 1 | 1 | 0 | Success |
| 2 | 3 | 2 | Success |
| 3 | 5 | 4 | Success |
Parameterized Scenarios
HTML Report
Parameter Formatting
- Default: toString()
- @Format( MyCustomFormatter.class )
- @Formatf( " -- %s -- " )
- @MyCustomFormatAnnotation
Example
@OnOff
@Format( value = BooleanFormatter.class, args = { "on", "off" } )
@Retention( RetentionPolicy.RUNTIME )
@interface OnOff {}
Apply to Parameter
public SELF the_machine_is_$( @OnOff boolean onOrOff ) { ... }
Use Step
given().the_machine_is_$( false );
Report
Given the machine is off
Tables as Parameters
SELF the_following_books_are_on_stock( @Table String[][] stockTable ) {
...
}
- All iterable types are supported
Tables as Parameters
@Test
public void ordering_a_book_reduces_the_stock() {
given().the_following_books_on_stock(new String[][]{
{"id", "name", "author", "stock"},
{"1", "The Hitchhiker's Guide to the Galaxy", "Douglas Adams", "5"},
{"2", "Lord of the Rings", "John Tolkien", "3"},
});
when().a_customer_orders_book("1");
then().the_stock_looks_as_follows(new String[][]{
{"id", "name", "author", "stock"},
{"1", "The Hitchhiker's Guide to the Galaxy", "Douglas Adams", "4"},
{"2", "Lord of the Rings", "John Tolkien", "3"},
});
}
Tables as Parameters
Console Output
Ordering a book reduces the stock
Given the following books on stock
| id | name | author | stock |
+----+--------------------------------------+---------------+-------+
| 1 | The Hitchhiker's Guide to the Galaxy | Douglas Adams | 5 |
| 2 | Lord of the Rings | John Tolkien | 3 |
When a customer orders book 1
Then the stock looks as follows
| id | name | author | stock |
+----+--------------------------------------+---------------+-------+
| 1 | The Hitchhiker's Guide to the Galaxy | Douglas Adams | 4 |
| 2 | Lord of the Rings | John Tolkien | 3 |
Tables as Parameters
HTML Report
@BeforeScenario und @AfterScenario
public class GivenSteps extends Stage<GivenSteps> {
@ProvidedScenarioState
File temporaryFolder;
@BeforeScenario
void setupTemporaryFolder() {
temporaryFolder = ...
}
@AfterScenario
void deleteTemporaryFolder() {
temporaryFolder.delete();
}
}
@ScenarioRule
public class TemporaryFolderRule {
File temporaryFolder;
public void before() {
temporaryFolder = ...
}
public void after() {
temporaryFolder.delete();
}
}
public class GivenSteps extends Stage<GivenSteps> {
@ScenarioRule
TemporaryFolderRule rule = new TemporaryFolderRule();
}
@AfterStage, @BeforeStage
public class GivenCustomer extends Stage<GivenSteps> {
CustomerBuilder builder;
@ProvidedScenarioState
Customer customer;
public void a_customer() {
builder = new CustomerBuilder();
}
public void with_age(int age) {
builder.withAge(age);
}
@AfterStage
void buildCustomer() {
customer = builder.build();
}
}
Tags
@Test @FeatureEmail
void the_customer_gets_an_email_when_ordering_a_book() {
...
}
Mit Werten
@Test @Story( "ABC-123" )
void the_customer_gets_an_email_when_ordering_a_book() { ... }
@Pending
- Marks the whole scenario or single steps as not implemented yet
- Steps are skipped, but appear in the report
HTML Report
@Hidden
- Marks methods to not appear in the report
- Useful for technically required methods
@Hidden
public SELF doSomethingTechnical() { ... }
Extended Step Descriptions
@ExtendedDescription("The Hitchhiker's Guide to the Galaxy, "
+ "by default the book is not on stock" )
public SELF a_book() { ... }
HTML Report
Attachments
public class Html5ReportStage {
@ExpectedScenarioState
protected CurrentStep currentStep; // provided by JGiven
protected void takeScreenshot() {
String base64 = ( (TakesScreenshot) webDriver )
.getScreenshotAs( OutputType.BASE64 );
currentStep.addAttachment(
Attachment.fromBase64( base64, MediaType.PNG )
.withTitle( "Screenshot" ) );
}
}
HTML Report