For our final assignment for the course, we are being tasked to create a sort of activity similar to the activities we have been doing in class this semester for my software testing course. In working on this activity, we chose to write an assignment based on behavior driven development, using the Cucumber tool. As such, before we really get into the weeds of the assignment, I thought it would be a good idea to look into the tool myself and what behavior driven development looks like.
For today’s post, I’m looking at a blog post from Moisés Macero on The Practical Developer blog.
Firstly, behavior driven development is built for fostering communication and discussion around systems and how they should properly be working. Typically, the process is in three stages: discuss, capture, and write tests. We want to talk about what the requirements really should be between the product owner and developers, refine what our requirements and targets should be, then move forward to building the software and testing it.
What Cucumber does for this is introducing syntax, called Gherkin, to enhance the readability of tests even for people who don’t know how to read code. The syntax is styled in a format of ‘given’, ‘when’, ‘then’. You mark these test cases with keywords such as feature, scenario or example to accurately describe what the code should be doing and how it is being tested. One of the examples used in the article is the following:
Scenario: Users solve challenges, they get feedback and their stats.
Given a new user John
When he requests a new challenge
And he sends the correct challenge solution
Then his stats include 1 correct attempt
Here, you can see that the syntax is (probably) easily readable and understandable even if you don’t have any software development experience. These statements are stored in .feature files, and act as sorts of definitions for tests.
For the actual testing, you apply these definitions by writing Cucumber expressions in a step definitions file. These are essentially files that not only test the code in a step-by-step approach, but also features readable headers for each function. Here’s an example of a couple of functions using Cucumber expressions (taken from this post in the same series):
@Given("a new user {word} is created")
public void aNewUser(String user) {
this.challengeActor = new Challenge(user);
}
@When("they request a new challenge")
public void userRequestsANewChallenge() throws Exception {
this.challengeActor.askForChallenge();
}
@Then("they gets a mid-complexity multiplication to solve")
public void getsAMidComplexityMultiplicationToSolve() {
assertThat(this.challengeActor.getCurrentChallenge().getFactorA())
.isBetween(9, 100);
assertThat(this.challengeActor.getCurrentChallenge().getFactorB())
.isBetween(9, 100);
}
Cucumber supports a variety of different languages, and also supports testing frameworks such as JUnit, which means it is also very versatile and can be used alongside a testing driven development environment as well.
From the blog CS@Worcester – V's CompSCi Blog by V and used with permission of the author. All other rights reserved by the author.