Skip to content

Commit c2ecd69

Browse files
authored
revamp testing infra (#511)
* revamp testing infra * use pytest * fix lower python versions * add instructions to DEVELOPMENT.md, move eval files to tests/eval_files * remove old commented-out code * clarified error message * fix comment (it applied to flake8-async, but this implementation does not allow # error: ...)
1 parent 764c400 commit c2ecd69

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1219
-2232
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ repos:
1919
hooks:
2020
- id: flake8
2121
additional_dependencies: [flake8-bugbear]
22-
exclude: ^tests/b.*
22+
exclude: ^tests/eval_files/.*
2323

2424
- repo: https://github.com/rstcheck/rstcheck
2525
rev: v6.2.4

DEVELOPMENT.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,48 @@ cd flake8-bugbear
4242
/path/to/venv/bin/pip install -e '.[dev]'
4343
```
4444

45+
## Writing Tests
46+
47+
flake8-bugbear has a test runner that will go through all files in `tests/eval_files/`, run them through the linter, and check that they emit the appropriate error messages.
48+
49+
The expected errors are specified by adding comments on the line where the error is expected. The format consists of the error code, followed by a comma-separated list of the `col_offset` as well as `vars` that are used when `str.format`ing the full error message.
50+
```python
51+
x = ++n # B002: 4
52+
try:
53+
...
54+
except* (ValueError,): # B013: 0, "ValueError", "*"
55+
...
56+
```
57+
The error code should be in the `error_codes` dict, and the other values are `eval`'d as if in a `tuple` and should be valid python objects. (I.e. remember to quote strings)
58+
59+
You can also specify options to be passed to `BugBearChecker` with an `# OPTIONS` comments
60+
```python
61+
# OPTIONS: extend_immutable_calls=["fastapi.Depends", "fastapi.Query"]
62+
# OPTIONS: classmethod_decorators=["mylibrary.makeclassmethod", "validator"], select=["B902"]
63+
```
64+
65+
If you specify a python version somewhere in the file name with `_pyXX`, the file will be skipped on smaller versions. Otherwise the name has no impact on the test, and you can test multiple errors in the same file.
66+
67+
The infrastructure is based on the test runner in https://github.com/python-trio/flake8-async which has some additional features that can be pulled into flake8-bugbear when desired.
68+
69+
4570
## Running Tests
4671

4772
flake8-bugbear uses coverage to run standard unittest tests.
4873

4974
```console
50-
/path/to/venv/bin/coverage run tests/test_bugbear.py
75+
/path/to/venv/bin/coverage run -m pytest tests/test_bugbear.py
5176
```
5277

5378
You can also use [tox](https://tox.wiki/en/latest/index.html) to test with multiple different python versions, emulating what the CI does.
5479

5580
```console
5681
/path/to/venv/bin/tox
5782
```
58-
will by default run all tests on python versions 3.8 through 3.12. If you only want to test a specific version you can specify the environment with `-e`
83+
will by default run all tests on python versions 3.9 through 3.13. If you only want to test a specific version you can specify the environment with `-e`
5984

6085
```console
61-
/path/to/venv/bin/tox -e py38
86+
/path/to/venv/bin/tox -e py313
6287
```
6388

6489
## Running linter

0 commit comments

Comments
 (0)