Behavior-Driven Testing (BDT) defines user stories in a human-readable given-when-then format. This testing approach is typically used as part of Behavior-Driven Development ( BDD ) or other agile development efforts.
To demonstrate the usage of BDT with Squish we have implemented a JavaScript framework that is based on the de-facto standard input language Gherkin. This article demonstrates the usage of this framework which is available on request.
Input¶
The addressbook.feature file describes the expected behavior of the Addressbook application when addressbooks are being created and filled. It contains two different scenarios with Given, When and Then steps.
addressbook.feature
Feature: Filling of addressbook
Scenario: Initial state of created addressbook
Given no prior existing addressbook
When I create a new addressbook
Then no entries should be present
Scenario: State after adding first entry
Given a newly created addressbok
When entering the first person
Then one entry should be present
Above feature file is placed inside of the tst_addressbook test case of the behavior-driven development test suite suite_bdd. As a preparation of the test case implementation the following squishrunner execution will generate a test script skeleton:
Test Skeleton¶
The test.js skeleton looks like this:
The test.log() statements denote the functions that still need to be filled with calls controlling the application to be tested.
Test Implementation¶
Via the use of an Application convenience class the implementation of the functions is a matter of adding 1-3 lines each:
Helper Object¶
The aforementioned Application class has been created for convenience based on a recording created with Squish. The class wraps around the Application Under Test and can be shared among test cases. The functions Application.launch(), createAddressbook(), addDummyEntry() and countEntries() are defined like this:
Execution¶
Now, the behavior-driven test is ready to be executed. It can either be done from the Squish IDE or the command line:
Here is the log of the run:
START_TEST_CASE Start 'tst_addressbook' Test Case 'tst_addressbook' started
LOG shared/scripts/Feature.js:50: Executing 2 test(s) for 'addressbook.feature'
LOG shared/scripts/Feature.js:11: Executing scenario 'Initial state of created addressbook'
LOG shared/scripts/Feature.js:27: Executing step: Given no prior existing addressbook
LOG shared/scripts/Feature.js:27: Executing step: When I create a new addressbook
LOG shared/scripts/Feature.js:27: Executing step: Then no entries should be present
PASS tst_addressbook/test.js:19: Comparison '0' and '0' are equal
LOG shared/scripts/Feature.js:11: Executing scenario 'State after adding first entry'
LOG shared/scripts/Feature.js:27: Executing step: Given a newly created addressbok
LOG shared/scripts/Feature.js:27: Executing step: When entering the first person
LOG shared/scripts/Feature.js:27: Executing step: Then one entry should be present
PASS tst_addressbook/test.js:36: Comparison '1' and '1' are equal
END_TEST_CASE End 'tst_addressbook' End of test case 'tst_addressbook'
And here is the result summary:
*******************************************************
Summary:
Number of Test Cases: 1
Number of Tests: 2
Number of Errors: 0
Number of Fatals: 0
Number of Fails: 0
Number of Passes: 2
Number of Expected Fails: 0
Number of Unexpected Passes: 0
Number of Warnings: 0
*******************************************************
Outlook¶
Possible extensions of the framework:
Data tables and placeholders for Scenario Outlines
Write out a HTML version of the steps including results
Grab a screenshot for each step
Provide statistics on the coverage of features and scenarios