What is Serenity BDD?
Serenity BDD is an open-source library that uses your automated test results to produce illustrated, narrative reports that document and describe what your application does and how it works.
For example, let’s make a mock social media app using a Behaviour-Driven-Development approach:
Scenario: Validate that the user can create and modify their account
Given user creates an account
When user creates a post
Then user comments on their post, create an album and uploads a photo
And user is able to see their posts, comments, and photos
And user updates their post
And user deletes their account
When a tester or a developer automates and executes this scenario, Serenity will generate a report index.html file in the target/site/serenity directory. The report will let you see the high-level view of the test but also lets you step into the details and see a step-by-step execution of the test.
Implementing Step Libraries
In Serenity, tests are broken down into reusable steps. An important principle behind Serenity is the idea that it is easier to maintain a test that uses several layers of abstraction to hide the complexity behind different parts of a test.
Let’s say we are testing if a user can edit their profile and need to illustrate the following business rules:
- Users should start with no account status
- Users that create an account are able to post or edit their information
These can be broken down further into further steps:
- Create a new account
- Make a new post
- Add comments, photos, or an album
- Be able to view and retrieve posted media
- Delete user account
public class newUserCreation { @Steps AccountModification accountModification; @Given("user creates an account") public void createNewUserAccountAndVerify() { accountModification.postNewUser(); } @When("user creates a post") public void createNewPost() { accountModification.postNewPost(); } @Then("user comments on their post, creates an album and uploads a photo") public void commentOnPost() { accountModification.addComment(); accountModification.createAlbum(); accountModification.uploadPhoto(); } @Then("user is able to see their posts, comments and photos") public void checkAccountDetails() { accountModification.getAccount(); accountModification.getComment(); accountModification.getUserPhoto(); } @Then("user updates their post") public void updateUserPost() { accountModification.updatePost(); } @Then("user deletes their account") public void deleteUserAccount() { accountModification.deleteAccount(); } }
The AccountModification class is what we call a step library. As shown above, we use the @Steps annotation to indicate a step library in our test code. This annotation tells Serenity to instantiate and instrument this field so that the methods you call in this library also appear in the test report. The step library class with the method to post new user info will look like this:
public class AccountModification { public static User newUser = User.builder() .name(Utils.randomFullName()) .username(Utils.randomUsername()) .email(Utils.randomEmail()) .phone(Utils.randomPhoneNumber()) .build(); @BeforeStep public void preCondition() { baseURI = Utils.BASE_URL; } @Step("POST new comment") public void addComment() { Response response = SerenityRest.given() .contentType(ContentType.JSON) .pathParams("postId",newUser.getPostId()) .body(newUser.newComment().toJSONString()) .post("/posts/{postId}/comments"); } @Step("POST new album") public void createAlbum() { Response response = SerenityRest.given() .contentType(ContentType.JSON) .body(newUser.newAlbum().toJSONString()) .post("/albums"); } @Step("POST new photo") public void uploadPhoto() { Response response = SerenityRest.given() .contentType(ContentType.JSON) .pathParams("albumId",newUser.getAlbumId()) .body(newUser.newPhoto().toJSONString()) .post("/albums/{albumId}/photos"); } }
And the generated Serenity report looks like this:
Note: One of the keys to writing good test reports is getting the step layers right.
Test suites are more maintainable with organized, clear, and well-defined layers. This helps the report reader concentrate on one step at a time.
“Serenity BDD reporting tool” Tech Bite was brought to you by Alen Vuk, Quality Assurance Engineer at Atlantbh.
Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.