Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

README.md

behavior-driven-python/pytest-bdd

Purpose

This project shows how to do BDD in Python using pytest-bdd, a plugin for the pytest test automation framework. It exhibits the basics of pytest-bdd with simple unit-, service-, and web-level tests. Tests are meant to highlight pytest-bdd features, not to necessarily show testing best practices for scalable solutions.

This project is a companion to the PyCon Canada 2018 talk "Behavior-Driven Python with pytest-bdd" and the Automation Panda article Python Testing 101: pytest-bdd.

Setup

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 tests/step_defs/__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.

Features

There are 4 feature files that showcase how to use pytest-bdd in various ways:

  1. unit_basic.feature
    • Contains unit test scenarios for a cucumber basket.
    • Tests that cucumbers can be added and removed within limits.
  2. unit_outlines.feature
    • Contains cucumber basket tests written as scenario outlines.
  3. service.feature
    • Contains service test scenarios for the DuckDuckGo Instant Answer API.
    • Uses requests to make REST API calls.
  4. web.feature
    • Contains Web test scenarios for the DuckDuckGo home page.
    • Uses Selenium WebDriver to interact with the site through Firefox.
    • Uses a custom pytest fixture for WebDriver setup and cleanup.

Every feature and scenario is tagged according to coverage area.

Test Execution

To run all tests from the root directory, run pytest. All the standard pytest command line options work. Use command line options for filtering and other controls. Options may also be put inside the pytest.ini configuration file. Below are some common options:

# run all tests
pytest

# filter tests by test module
# note: feature files cannot be run directly
pytest tests/step_defs/test_unit_basic.py
pytest tests/step_defs/test_unit_outlines.py
pytest tests/step_defs/test_unit_service.py
pytest tests/step_defs/test_unit_web.py

# filter tests by tags
# running by tag is typically better than running by path
pytest -k "unit"
pytest -k "service"
pytest -k "web"
pytest -k "add or remove"
pytest -k "unit and not outline"

# print JUnit report
pytest -junitxml=<path>

pytest-bdd tests can be executed and filtered together with regular pytest tests. Tests can all be located within the same directory. Tags work just like pytest.mark. All other pytest plugins should work, too. For example:

Warning: Scenario outlines cause deprecation warnings when executed. pytest.ini includes options to skip deprecation warnings. Alternatively, use the --disable-pytest-warnings command line option.

Helpful Links