To
improve readability, you can group the examples of scenario outline.
BalGame.featureFeature: Bal game Scenario Outline: Remove balls from the box Given A box of <noOfBalls> When I taken <noOfBallsTaken> balls out of the box Then <remainingBalls> balls remaining in the box Examples: Positive Scenario | noOfBalls | noOfBallsTaken | remainingBalls | | 100 | 10 | 90 | | 200 | 20 | 180 | | 100 | 30 | 70 | Examples: Negative Scenario, user tries to take more balls than total number of balls, then it should give all the balls in the box and remainingballs should be 0. | noOfBalls | noOfBallsTaken | remainingBalls | | 100 | 101 | 0 | | 200 | 300 | 0 | | 100 | 500 | 0 |
As
you observe the feature file, all the positive scenarios and negative scenarios
are grouped separately.
BoxUtil.java
package com.sample.util; public class BoxUtil { public static int getRemainingBalls(int noOfBalls, int noOfBallsTaken) { if (noOfBalls < noOfBallsTaken) { return 0; } return noOfBalls - noOfBallsTaken; } }
BallGameTest.java
TestRun.java
When I ran above application, I seen below message in console.
package com.sample.test; import static org.junit.Assert.assertEquals; import com.sample.util.BoxUtil; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; public class BallGameTest { int totalNoOfBalls; int ballsTaken; @Given("^A box of (\\d+)$") public void a_box_of(int arg1) throws Exception { this.totalNoOfBalls = arg1; } @When("^I taken (\\d+) balls out of the box$") public void i_taken_balls_out_of_the_box(int arg1) throws Exception { this.ballsTaken = arg1; } @Then("^(\\d+) balls remaining in the box$") public void balls_remaining_in_the_box(int arg1) throws Exception { assertEquals(arg1, BoxUtil.getRemainingBalls(totalNoOfBalls, ballsTaken)); } }
TestRun.java
package com.sample.main; import org.junit.runner.RunWith; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; @RunWith(Cucumber.class) @CucumberOptions(monochrome = true, features = "src/test/java/com/sample/features", glue = "com.sample.test", plugin = { "pretty" }) public class TestRun { }
When I ran above application, I seen below message in console.
Feature: Bal game Scenario Outline: Remove balls from the box # src/test/java/com/sample/features/BallGame.feature:3 Given A box of <noOfBalls> When I taken <noOfBallsTaken> balls out of the box Then <remainingBalls> balls remaining in the box Examples: Positive Scenario Scenario Outline: Remove balls from the box # src/test/java/com/sample/features/BallGame.feature:11 Given A box of 100 # BallGameTest.a_box_of(int) When I taken 10 balls out of the box # BallGameTest.i_taken_balls_out_of_the_box(int) Then 90 balls remaining in the box # BallGameTest.balls_remaining_in_the_box(int) Scenario Outline: Remove balls from the box # src/test/java/com/sample/features/BallGame.feature:12 Given A box of 200 # BallGameTest.a_box_of(int) When I taken 20 balls out of the box # BallGameTest.i_taken_balls_out_of_the_box(int) Then 180 balls remaining in the box # BallGameTest.balls_remaining_in_the_box(int) Scenario Outline: Remove balls from the box # src/test/java/com/sample/features/BallGame.feature:13 Given A box of 100 # BallGameTest.a_box_of(int) When I taken 30 balls out of the box # BallGameTest.i_taken_balls_out_of_the_box(int) Then 70 balls remaining in the box # BallGameTest.balls_remaining_in_the_box(int) Examples: Negative Scenario, user tries to take more balls than total number of balls, then it should give all the balls in the box and remainingballs should be 0. Scenario Outline: Remove balls from the box # src/test/java/com/sample/features/BallGame.feature:18 Given A box of 100 # BallGameTest.a_box_of(int) When I taken 101 balls out of the box # BallGameTest.i_taken_balls_out_of_the_box(int) Then 0 balls remaining in the box # BallGameTest.balls_remaining_in_the_box(int) Scenario Outline: Remove balls from the box # src/test/java/com/sample/features/BallGame.feature:19 Given A box of 200 # BallGameTest.a_box_of(int) When I taken 300 balls out of the box # BallGameTest.i_taken_balls_out_of_the_box(int) Then 0 balls remaining in the box # BallGameTest.balls_remaining_in_the_box(int) Scenario Outline: Remove balls from the box # src/test/java/com/sample/features/BallGame.feature:20 Given A box of 100 # BallGameTest.a_box_of(int) When I taken 500 balls out of the box # BallGameTest.i_taken_balls_out_of_the_box(int) Then 0 balls remaining in the box # BallGameTest.balls_remaining_in_the_box(int) 6 Scenarios (6 passed) 18 Steps (18 passed) 0m0.295s
No comments:
Post a Comment