A linter that checks test docstrings for the arrange/act/assert structure
Project description
flake8-test-docs
Have you ever needed to understand a new project and started reading the tests only to find that you have no idea what the tests are doing? Good test documentation is critical during test definition and when reviewing tests written in the past or by someone else. This linter checks that the test function docstring includes a description of the test setup, execution and checks.
Getting Started
python -m venv venv
source ./venv/bin/activate
pip install flake8 flake8-test-docs
flake8 test_source.py
On the following code:
# test_source.py
def test_foo():
value = foo()
assert value == "bar"
This will produce warnings such as:
flake8 test_source.py
test_source.py:2:1: TDO001 Docstring not defined on test function, more information: https://github.com/jdkandersson/flake8-test-docs#fix-tdo001
This can be resolved by changing the code to:
# test_source.py
def test_foo():
"""
arrange: given foo that returns bar
act: when foo is called
assert: then bar is returned
"""
value = foo()
assert value == "bar"
Configuration
The plugin adds the following configurations to flake8
:
--test-docs-patter
: The pattern the test documentation should follow, e.g.,given/when/then
. Defaults toarrange/act/assert
.--test-docs-filename-pattern
: The filename pattern for test files. Defaults totest_.*\.py
.--test-docs-function-pattern
: The function pattern for test functions. Defaults totest_.*
.
Rules
A few rules have been defined to allow for selective suppression:
TDO001
: checks that test functions have a docstring.TDO002
: checks that test function docstrings follow the documentation pattern.
Fix TDO001
This linting rule is triggered by a test function in a test file without a docstring. For example:
# test_source.py
def test_foo():
result = foo()
assert result == "bar"
This example can be fixed by:
# test_source.py
def test_foo():
"""
arrange: given foo that returns bar
act: when foo is called
assert: then bar is returned
"""
result = foo()
assert result == "bar"
Fix TDO002
This linting rule is triggered by a test function in a test file with a docstring that doesn't follow the documentation pattern. For example:
# test_source.py
def test_foo():
"""Test foo."""
result = foo()
assert result == "bar"
This example can be fixed by:
# test_source.py
def test_foo():
"""
arrange: given foo that returns bar
act: when foo is called
assert: then bar is returned
"""
result = foo()
assert result == "bar"
The message of the linting rule should give you the specific problem with the documentation. In general, the pattern is:
- start on the second line with the same indentation is the start of the docstring
- the starting line should begin with
arrange:
(or whatever was set using--test-docs-patter
) followed by at least some words describing the test setup - long test setup descriptions can be broken over multiple lines by indenting the lines after the first by one level (e.g., 4 spaces by default)
- this is followed by similar sections starting with
act:
describing the test execution andassert:
describing the checks - the last line should be indented the same as the start of the docstring
Below are some valid examples. Starting with a vanilla example:
# test_source.py
def test_foo():
"""
arrange: given foo that returns bar
act: when foo is called
assert: then bar is returned
"""
result = foo()
assert result == "bar"
Here is an example where the test function is in a nested scope:
# test_source.py
class TestSuite:
def test_foo():
"""
arrange: given foo that returns bar
act: when foo is called
assert: then bar is returned
"""
result = foo()
assert result == "bar"
Here is an example where each of the descriptions go over multiple lines:
# test_source.py
def test_foo():
"""
arrange: given foo
that returns bar
act: when foo
is called
assert: then bar
is returned
"""
result = foo()
assert result == "bar"
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flake8_test_docs-1.0.8.tar.gz
.
File metadata
- Download URL: flake8_test_docs-1.0.8.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.11.1 Linux/5.15.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aea112f6dbf52518fd12d7e7959b809c504ebb379d1cde69f56d398900037bd3 |
|
MD5 | 9c31a4d9dd0cca8dd932bcfc5cd4aa8a |
|
BLAKE2b-256 | 1acc69bd58b17228835507aad88f8ebbd13bf3442ff7d6c5330e55aee9d847e1 |
File details
Details for the file flake8_test_docs-1.0.8-py3-none-any.whl
.
File metadata
- Download URL: flake8_test_docs-1.0.8-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.11.1 Linux/5.15.0-1024-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09d1bcdb2472e272adb1130b0f5482973149690ca85ea68fca7f7af901d17f39 |
|
MD5 | dfc33292de84a4e244da3d4740a9e096 |
|
BLAKE2b-256 | 290a6b184345c259581a64169e894ed30a46dc05a7dd2f62e47118c61c26964e |