This project shows how to do BDD
in Python
using behave.
It exhibits the basics of the behave test framework
with simple unit-, service-, and web-level tests.
Tests are meant to highlight behave features,
not to necessarily show testing best practices for scalable solutions.
This project is a companion to the PyCon 2018 talk Behavior-Driven Python and the Automation Panda article Python Testing 101: behave.
This project uses
Python 3.
Dependencies are listed in requirements.txt.
Use venv
to create a virtual environments for dependencies.
The unit tests use the cucumbers.py module from the parent directory.
The features/steps/__init__.py file automatically appends this path
for import lookup using sys.path.append.
The Web tests use Selenium WebDriver to interact with live pages in real browsers. The hard-coded browser choice is Mozilla Firefox. These tests require geckodriver to be installed locally on the test machine and accessible from the system path. Typically, they should run fine on any OS with the latest versions of Firefox and geckodriver. They have been verified on macOS 10.13.4, Firefox 59.0.2, and geckodriver 0.20.1.
There are 3 feature files that showcase how to use behave in various ways:
unit.feature- Contains unit test scenarios for a cucumber basket.
- Tests that cucumbers can be added and removed within limits.
service.feature- Contains service test scenarios for the DuckDuckGo Instant Answer API.
- Uses requests to make REST API calls.
web.feature- Contains Web test scenarios for the DuckDuckGo home page.
- Uses Selenium WebDriver to interact with the site through Firefox.
- Uses
environment.pyhooks for WebDriver setup and cleanup.
Every feature and scenario is tagged according to coverage area.
To run all tests from the root directory, run behave.
Use command line options
for filtering and other controls.
Options may also be put inside the behave.ini
configuration file.
Below are some common options:
# run all tests
behave
# filter tests by feature file
behave features/unit.feature
behave features/service.feature
behave features/web.feature
# filter tests by tags
behave --tags-help
behave --tags @unit
behave --tags @service
behave --tags @web
behave --tags @duckduckgo
behave --tags ~@unit
behave --tags @basket --tags @add
# print JUnit report
behave --junit- Automation Panda blog
- Python Testing 101: behave
- Official Behave Docs
- Behave on GitHub
- Behave Examples on GitHub
- bdp-behave.pdf (Slides from PyCon 2018 Talk)