|
1 | 1 | # flake8-test-docs
|
2 |
| -Linter that checks test docstrings for the arrange/act/assert or given/when/then structure |
| 2 | + |
| 3 | +Have you ever needed to understand a new project and started reading the tests |
| 4 | +only to find that you have no idea what the tests are doing? Good test |
| 5 | +documentation is critical during test definition and when reviewing tests |
| 6 | +written in the past or by someone else. This linter checks that the test |
| 7 | +function docstring includes a description of the test setup, execution and |
| 8 | +checks. |
| 9 | + |
| 10 | +## Getting Started |
| 11 | + |
| 12 | +```shell |
| 13 | +python -m venv venv |
| 14 | +source ./venv/bin/activate |
| 15 | +pip install flake8 flake8-test-docs |
| 16 | +flake8 test_source.py |
| 17 | +``` |
| 18 | + |
| 19 | +On the following code: |
| 20 | + |
| 21 | +```Python |
| 22 | +# test_source.py |
| 23 | +def test_foo(): |
| 24 | + value = foo() |
| 25 | + assert value == "bar" |
| 26 | +``` |
| 27 | + |
| 28 | +This will produce warnings such as: |
| 29 | + |
| 30 | +```shell |
| 31 | +flake8 test_source.py |
| 32 | +test_source.py:2:1: TDO001 Docstring not defined on test function, more information: https://github.com/jdkandersson/flake8-test-docs#fix-tdo001 |
| 33 | +``` |
| 34 | + |
| 35 | +This can be resolved by changing the code to: |
| 36 | + |
| 37 | +```Python |
| 38 | +# test_source.py |
| 39 | +def test_foo(): |
| 40 | + """ |
| 41 | + arrange: given foo that returns bar |
| 42 | + act: when foo is called |
| 43 | + assert: then bar is returned |
| 44 | + """ |
| 45 | + value = foo() |
| 46 | + assert value == "bar" |
| 47 | +``` |
| 48 | + |
| 49 | +## Configuration |
| 50 | + |
| 51 | +The plugin adds the following configurations to `flake8`: |
| 52 | + |
| 53 | +* `--test-docs-patter`: The pattern the test documentation should follow, |
| 54 | + e.g., `given/when/then`. Defaults to `arrange/act/assert`. |
| 55 | +* `--test-docs-filename-pattern`: The filename pattern for test files. Defaults |
| 56 | + to `test_.*.py`. |
| 57 | +* `--test-docs-function-pattern`: The function pattern for test functions. |
| 58 | + Defaults to `test_.*`. |
| 59 | + |
| 60 | + |
| 61 | +## Rules |
| 62 | + |
| 63 | +A few rules have been defined to allow for selective suppression: |
| 64 | + |
| 65 | +* `TDO001`: checks that test functions have a docstring. |
| 66 | +* `TDO002`: checks that test function docstrings follow the documentation |
| 67 | + pattern. |
| 68 | + |
| 69 | +### Fix TDO001 |
| 70 | + |
| 71 | +This linting rule is triggered by a test function in a test file without a |
| 72 | +docstring. For example: |
| 73 | + |
| 74 | +```Python |
| 75 | +# test_source.py |
| 76 | +def test_foo(): |
| 77 | + result = foo() |
| 78 | + assert result == "bar" |
| 79 | +``` |
| 80 | + |
| 81 | +This example can be fixed by: |
| 82 | + |
| 83 | +```Python |
| 84 | +# test_source.py |
| 85 | +def test_foo(): |
| 86 | + """ |
| 87 | + arrange: given foo that returns bar |
| 88 | + act: when foo is called |
| 89 | + assert: then bar is returned |
| 90 | + """ |
| 91 | + result = foo() |
| 92 | + assert result == "bar" |
| 93 | +``` |
| 94 | + |
| 95 | +### Fix TDO002 |
| 96 | + |
| 97 | +This linting rule is triggered by a test function in a test file with a |
| 98 | +docstring that doesn't follow the documentation pattern. For example: |
| 99 | + |
| 100 | +```Python |
| 101 | +# test_source.py |
| 102 | +def test_foo(): |
| 103 | + """Test foo.""" |
| 104 | + result = foo() |
| 105 | + assert result == "bar" |
| 106 | +``` |
| 107 | + |
| 108 | +This example can be fixed by: |
| 109 | + |
| 110 | +```Python |
| 111 | +# test_source.py |
| 112 | +def test_foo(): |
| 113 | + """ |
| 114 | + arrange: given foo that returns bar |
| 115 | + act: when foo is called |
| 116 | + assert: then bar is returned |
| 117 | + """ |
| 118 | + result = foo() |
| 119 | + assert result == "bar" |
| 120 | +``` |
| 121 | + |
| 122 | +The message of the linting rule should give you the specific problem with the |
| 123 | +documentation. In general, the pattern is: |
| 124 | + |
| 125 | +* start on the second line with the same indentation is the start of the |
| 126 | + docstring |
| 127 | +* the starting line should begin with `arrange:` (or whatever was set using |
| 128 | + `--test-docs-patter`) followed by at least some words describing the test |
| 129 | + setup |
| 130 | +* long test setup descriptions can be broken over multiple lines by indenting |
| 131 | + the lines after the first by one level (e.g., 4 spaces by default) |
| 132 | +* this is followed by similar sections starting with `act:` describing the test |
| 133 | + execution and `assert:` describing the checks |
| 134 | +* the last line should be indented the same as the start of the docstring |
| 135 | + |
| 136 | +Below are some valid examples. Starting with a vanilla example: |
| 137 | + |
| 138 | +```Python |
| 139 | +# test_source.py |
| 140 | +def test_foo(): |
| 141 | + """ |
| 142 | + arrange: given foo that returns bar |
| 143 | + act: when foo is called |
| 144 | + assert: then bar is returned |
| 145 | + """ |
| 146 | + result = foo() |
| 147 | + assert result == "bar" |
| 148 | +``` |
| 149 | + |
| 150 | +Here is an example where the test function is in a nested scope: |
| 151 | + |
| 152 | +```Python |
| 153 | +# test_source.py |
| 154 | +class TestSuite: |
| 155 | + |
| 156 | + def test_foo(): |
| 157 | + """ |
| 158 | + arrange: given foo that returns bar |
| 159 | + act: when foo is called |
| 160 | + assert: then bar is returned |
| 161 | + """ |
| 162 | + result = foo() |
| 163 | + assert result == "bar" |
| 164 | +``` |
| 165 | + |
| 166 | +Here is an example where each of the descriptions go over multiple lines: |
| 167 | + |
| 168 | +```Python |
| 169 | +# test_source.py |
| 170 | +def test_foo(): |
| 171 | + """ |
| 172 | + arrange: given foo |
| 173 | + that returns bar |
| 174 | + act: when foo |
| 175 | + is called |
| 176 | + assert: then bar |
| 177 | + is returned |
| 178 | + """ |
| 179 | + result = foo() |
| 180 | + assert result == "bar" |
| 181 | +``` |
0 commit comments