Introduction
This document describes rules for writing BDD instructions. It is used as a guideline for proper stated and formatted instructions. Natural language instructions used for BDD are not absolutely natural language instructions. They're actually "naturalized" form of some specific command with ability to variate some part. So, from the technical side of BDD the strict rules are still necessary to avoid different forms of absolutely the same phrases but with small differences. It's not essential for understanding but it is critical for technical implementation.
This document contains examples of correct and incorrect phrases. If some phrase is described as incorrect it only means that it violates the standards though it can be properly built from the language point of view. These standards do not teach English. They just describe which subset of possible phrases is taken as acceptable.
And again, standards are designed for uniformity only. There's no strict dogmatum in them. If some phrases seem to be more appropriate but they violate the rules it's OK to adjust rules to current needs rather than restrict needs with existing rules. The main thing is that there should be some uniform rule set which is kept within the project. Current document can be a basis for such standards
Terms and Definitions
- Feature file – file containing test definitions written in natural language. Typically it has *.feature extension
- Keyword – fully filled natural language phrase used in feature files
- Given\When\Then statement – the keyword starting with Given, When or Then word correspondingly
- Keyword implementation (implementation) – the actual code written with programming language and containing actions to be done for corresponding keyword
- Keyword binding (binding) – string or regular expression setting correspondence between keyword and it's implementation. Typically it's represented as an attribute (for C# BDD engines) or annotation (for Java BDD engines). The regular expression or string should be unique and match to specific set of keywords.
General rules
Rule 1.1
- Incorrect:
Given I'm logged into the system
Givenall services are up and runningNOTE: second Given statement should start with "And"
- Correct:
Given I'm logged into the system And all services are up and running
Rule 1.2
- "And" word is used for phrases in positive forms.
- "But" word is used for negative forms of keywords.
- When the sequence of phrases contains a mix of positive and negative phrases then series of "And" statements should be used prior to negative statement forms.
- Incorrect:
Given I'm logged into the system
And1 no data is available in the databaseBut2 all services are up and runningAnd3 debug logging is turned onNOTE: - 1 - First "And" statement contains negative form.
- 2 - "But" statement contains positive form of expression.
- 3 - Second "And" statement goes after "But" statement within the same sequence
- Correct:
Given I'm logged into the system And all services are up and running And debug logging is turned on But no data is available in the database
Rule 1.3
- If the varying part is a string it MUST be quoted.
- If varying part is the number it MUST be without quotes.
- Incorrect:
Given I'm logged into the system as
login/password1 And I have"5"2 records in the tableNOTE: - 1 - login and password are varying strings so they must be quoted.
- 2 - Second statement has the numeric value parametrized so no quotes are needed there
- Correct:
Given I'm logged into the system as "login"/"password" And I have 5 records in the table
Rule 1.4
- If table is used for multiple data elements it MUST contain header.
- If data structure is passed via table the field names MUST be placed as table headers.
- Each keyword using array/table MUST end with colon character
- Incorrect:
When I add the following colors 1 2 |Red| |Green| |Blue|
NOTE: - 1 - When statement here doesn't end with colon character
- 2 - Table contain only data without header
- Correct:
When I add the following colors: |Color Name| |Red| |Green| |Blue|
Rule 1.5
- Incorrect:
Given I
should see1 I'm logged into the system WhenI'm logged into2 the system Then Imake sure3 I'm logged into the systemNOTE: - 1 - First statement fits more to the Then statement
- 2 - Second statement fits more to Given statement
- 3 - Third statement is rather applicable for When
- Correct:
Given I'm logged into1 I'm logged into the system When I make sure2 I'm logged into the system Then I should see3 I'm logged into the system
Rule 1.6
- All test scenarios MUST fit the following structure:
<Given block> <When-Then blocks sequence>
- Given statements MUST go first. There MUST be no When or Then statements prior to Given.
- When-Then blocks sequence represents actual test logic and it reflects the sequence of steps built in the form of "action-verification".
- Each When-Then block MUST be started with When statement.
- Incorrect:
When I log into the system Then I should see the home page
Given I have 2 user accounts in the system1Given I'm logged into the system
Then I should see the home page2NOTE: - 1 - the Given statement goes after When-Then. In this case it's more reasonable to place Given statement as pre-condition prior to When-Then
- 2 - the Then statement goes right after Given omitting When statement. In this case Given statement can be replaced with corresponding When statement
- Correct:
Given I have 2 user accounts in the system When I log into the system Then I should see the home page
When I log into the system Then I should see the home page
Given statements
Rule 2.1
- Incorrect:
Given I
loginto the systemNOTE: Given statement here describes action instead of the state, so it must be re-phrased
- Correct:
Given I'm logged into the system
Rule 2.2
- Incorrect:
Given there are colors like : |Color Name| |Red| |White| |Blue|
NOTE: phrase "the following" is missing
- Correct:
Given there are the following colors: |Color Name| |Red| |White| |Blue|
When statements
Rule 3.1
- Incorrect:
When
I'm loggedinto the systemNOTE: this When statement operates with passive voice and actually describe the state rather than action so it should be re-phrased.
- Correct:
When I log into the system
Rule 3.2
- When statements describe actions from the first person. Each When statement MUST start with "I" in case "When" word is used.
- In case "And" or "But" words replace "When" (see Rule 1.1.) the "I" word MUST be omitted.
- Keyword binding expression MUST skip "I" part to fit both form of corresponding keyword.
- Incorrect:
When I log into the system And I open home page
NOTE: repetitive "I" must be removed
- Correct:
When I log into the system And open home page
Rule 3.3
- Incorrect:
Given I'm logged into the system
Then I should see the home page is openWhen I navigate to services pageNOTE: Here we make some verifications prior to any actions. "Then" statement here is quite reasonable as it describes verification. But such structure violates rule 1.6 when we MUST do actions prior to verifications. In this case we can re-phrase "Then" statement to describe action
- Correct:
Given I'm logged into the system When I make sure the home page is open And navigate to services page
Rule 3.4
- Incorrect:
When I add colors: |Color Name| |Red| |White| |Blue|
NOTE: phrase "the following" is missing
- Correct:
When I add the following colors: |Color Name| |Red| |White| |Blue|
Then statements
Rule 4.1
- Incorrect:
When I log into the system
Then I open the home pageNOTE: Then statement here describes action while it should be phrased as expected state description
- Correct:
When I log into the system Then I should see the home page is open
Rule 4.2
- In case Then statement starts with "Then" word in feature file it MUST be followed with "I should see" phrase or "I shouldn't see" phrase in case of negative form.
- If "And" or "But" words are used (see Rule 1.1) the "I should see" phrase should be skipped.
- In case there's a mix of "And" and "But" statements (see Rule 1.2) the first "But" statement in the sequence MUST contain "I shouldn't see" phrase.
- Incorrect:
Then I should see the home page is open And I should see1 the left navigation menu is present
And2 I should see1 no Admin tab is availableNOTE: - 1 - "I should see" phrase is not needed in second and third lines as they're ambiguous.
- 2 - The third line contains negative form of expression so "But" statement looks more appropriate here
- Correct:
Then I should see the home page is open And the left navigation menu is present But no Admin tab is available
Rule 4.3
- Incorrect:
Then I should see the drop down list with
the following options: |Option Name| |Option 1| |Option 2|NOTE: despite "the following" phrase is present it is located at the end of keyword
- Correct:
Then I should see the following options are available in the drop down list: |Option Name| |Option 1| |Option 2|
Conclusion
The list of rules described in this document is not complete and can be updated or detalized according to each specific project needs or some aquired practices. The main aim is to define some baseline which helps identifying whether we write instructions in proper way. Also, strict guidelines help finding existing keywords by using fixed constructions as we know how the phrase we look for should be formulated. Anyway, standards are not a silver bullet but quite helpful guide for effective BDD usage.
No comments:
Post a Comment