Parameterized Scenarios
@Test
@DataProvider({
"Smith, 1",
"Davis, 0",
"Sm, 1"})
public void should_find_owner_by_last_name( String searchTerm,
int numberOfResults ) {
given().an_owner_with_last_name("Smith");
when().searching_for(searchTerm);
then().exactly_$_owner_is_found(numberOfResults);
}
Uses the DataProviderRunner (github.com/TNG/junit-dataprovider).
Parameterized Runner and Theories of JUnit are also supported.
Parameterized Scenarios
Console Output
Should find owner by last name
Given an owner with last name Smith
When searching for <searchTerm>
Then exactly <numberOfResults> owner is found
Cases:
| # | searchTerm | numberOfResults | Status |
+---+------------+-----------------+---------+
| 1 | Smith | 1 | Success |
| 2 | Davis | 0 | Success |
| 3 | Sm | 1 | 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
@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() {
...
}
With Values
@Test @Story( "ABC-123" )
void the_customer_gets_an_email_when_ordering_a_book() { ... }
@Pending
- Marks whole scenarios or single steps as pending
- Steps are skipped and marked accordingly
HTML Report
@Hidden
- Marks methods to not appear in the report
- Useful for technical 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
Inline Attachments
Source: https://github.com/mthuret/xke-jgiven