15

Traditionally, structured test, especially long scenarios are commonly written in the step, expected, actual format. Behaviour Driven Design replaces this with the style of "Given", "When" "Then", for example

Given a working ATM

And invalid credit card

When the card is inserted

And a cash withdrawl is requested

Then no cash should be provided

And the card should be retained

I want to start use BDD on complex scenarios on a project, and the question that I have is: Does this new style of test case work for larger, real world, end-to-end scenarios or is it more appropriate for simpler "atomic" or unit test style test cases?

4 Answers 4

6

I don't see why it wouldn't scale. It's conceptually simple (which naturally makes it self open to scalability.) I would be weary though - the more complex a test is, the less effective it is. If you have tests you're worried about being too complex "Given, When, Then" (I almost abbreviated that to GWT but realized that would confuse people haha. Ahem...) then you may want to consider splitting that test into smaller components that will give you more feed back.

In other words, what makes it complex? Does it have input chaining which is the complexity? If it is, then you have a definite rationale for making multiple tests out of it. Another common introduction of complexity would be if you something like "when the result of ___ is ___" That really should be a given, not a when. Personally I would classify a move from when to given to be a complexity reduction.

3
  • Can you please clarify your comment, "the more complex a test is, the less effective it is?" Unit tests and functional tests should not be overly complex; however, customer (or end-to-end) scenarios/stories are typically more complex and involve testing multiiple parts of the system. Commented Jun 12, 2012 at 13:57
  • The effectiveness of a test only ever comes into play when it fails. The test is supposed to tell you what fails in your system. The more complex a test is, the less information you have about what is actually broken when it fails.
    – corsiKa
    Commented Jun 12, 2012 at 14:31
  • The effectiveness of a test is determined by the value of the information that test provides (e.g. a unit test that passes after refactoring gives me valuable information, as does a unit test that fails after refactoring). As a test becomes more complex the greater the information it should report out. If you design a complex test and rely on a simple pass/fail oracle then the design is likely not optimal Commented Jun 12, 2012 at 20:43
5

Although I don't have any experience using SpecFlow, I've recently starting using StoryQ for this type of testing. Although I was very leary on it at first, I've found it to be very useful, including in complex scenario's. Not only is the code more organized now, but also, I've found it easier to write more complex scenario's as it forces me to break it up more.

3

Firstly, I think you used the word "anger" when you meant "earnest" - but this is purely a guess on my part.

If my guess is correct (a "big" IF), then to answer your question, I would say "yes" - in other words, I would use "given" "when" "then" for more complex scenarios.

2

You can use Given/When/Then for scenarios with any complicity level, for instance,

Very specific scenario:

Scenario: Error when the password and confirm password do not match
Given I am on User Registration From 
When I fill the Form fields as follow:
| Field   | Value           |
| Name | myusrname|
| Password| password123| 
|Confirm Password | passnotmatch234|
And I click on the button “Save”
Then the error message should be “Password and Confirm password do not match”

End-to-end scenario:

Scenario: New user with unique name can be registered in the system 
Given user with name “Uncle Bob” does not exists in the system
When I create a new user with name “Uncle Bob”
Then the registration should be successful 

In this case, there are the following checks behind the phrase “Then the registration should be successful”:

1. Registration form was saved without errors
2. The User has received a confirmation email
3. The user had clicked on the confirm link and the email confirmation was successful 
4. User logged in to the system

All those steps are implicit and hidden in the phrase “Then the registration should be successful”, because the goal of the scenario was to check that user with unique name can be registered in the system.

1
  • If the Then step fails, how can I get clear feedback on which of the underlying assertions (i.e. 1,2,3,4) is the cause of failure?
    – urig
    Commented Sep 15, 2018 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.