BDD base project: Python + Behave
Basic example of using Python with Behave (BDD). This Gherkin
example includes:
- Basic Scenario
- Scenario Outline
- Tagged tests
Table of contents
Get started
Installation
pip3 install -r requirements.txt
on Ubuntu 20.04 (pyvirtualdisplay
requirements, we use pyvirtualdisplay
to avoid Cloudflare errors in headless mode):
sudo apt-get install xvfb xserver-xephyr tigervnc-standalone-server xfonts-base
How to run the tests
Running tests
behave
2 features passed, 0 failed, 0 skipped
6 scenarios passed, 0 failed, 0 skipped
30 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m50.056s
Running tests and generate Allure report
behave -f allure -o reports/allure_report/
Open Allure report
Note
: allure is required.
allure serve reports/allure_report/
HTML report:
Running tagged tests
Running tagged smoke tests
behave --tags=smoke
Running using customized tags
behave --tags='
'
more details: running-tagged-tests
Change Browser
edit behave.ini
:
...
[behave.userdata]
browser=firefox # <- CHANGE HERE - Available options: firefox | chrome
...
Change Headless mode
edit behave.ini
:
...
[behave.userdata]
headless = true # <- CHANGE HERE - Available options: true | false
...
Static code analysis tools
Find Problems
Checkers statically analyzes the code to find problems.
bash code_checkers.sh # run pylint, prospector, black and isort
Tools used:
-
pylint: Pylint is a Python static code analysis tool which looks for programming errors, helps enforcing a coding standard, sniffs for code smells and offers simple refactoring suggestions.
-
black: Black is the uncompromising Python code formatter.
-
isort: Python utility / library to sort imports alphabetically, and automatically separated into sections and by type.
-
prospector: Prospector is a tool to analyse Python code and output information about errors, potential problems, convention violations and complexity.
Tools executed by Prospector:
- bandit: Bandit is a tool designed to find common security issues.
- dodgy: It is a series of simple regular expressions designed to detect things such as accidental SCM diff checkins, or passwords or secret keys hard coded into files.
- mccabe: Complexity checker.
- mypy: Mypy is an optional static type checker for Python.
- pep257: pep257 is a static analysis tool for checking compliance with Python PEP 257.
- pep8: pep8 is a tool to check your Python code against some of the style conventions in PEP 8.
- pyflakes: Pyflakes analyzes programs and detects various errors.
- pyroma: Pyroma is a product aimed at giving a rating of how well a Python project complies with the best practices of the Python packaging ecosystem, primarily PyPI, pip, Distribute etc, as well as a list of issues that could be improved.
Testing
We use tox
to validate changes on linux OS (headless mode):
tox
output
:
...
2 features passed, 0 failed, 0 skipped
6 scenarios passed, 0 failed, 0 skipped
30 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m43.177s
_____________________________________________ summary _____________________________________________
code_checkers: commands succeeded
py36: commands succeeded
py37: commands succeeded
py38: commands succeeded
py39: commands succeeded
congratulations :)
to validate changes for a particular Python version (36
, 37
, 38
or 39
), you can do:
tox -e py36
License
Changelog
- 1.0.0 - Initial release.