Skip to content

Commit

Permalink
Extend discovery to find *_test.py files, matching Pytest better (#204)
Browse files Browse the repository at this point in the history
* Update docs on test discovery

* Stop clean recipe adjusting packages

* Extend test file pattern to include '*_test.py' files

* Add bad example '*_test.py' file

* Add a tiny bit of typing to other bad examples

* Fix tox.ini: meta commands missing label

* Add new test file to test examples run

* Update Changelog
  • Loading branch information
jamescooke committed Mar 1, 2023
1 parent c0e65a1 commit f23db69
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 18 deletions.
15 changes: 11 additions & 4 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ Additional markers for types of change copied from ``README``:
Unreleased_
-----------

See also `latest documentation
<https://flake8-aaa.readthedocs.io/en/latest/#__unreleased_marker__>`_.

Added
.....

* 🎈 Files ending in ``_test.py`` are now linted. `Pull #204
<https://github.com/jamescooke/flake8-aaa/pull/204>`_ fixes `issue #185
<https://github.com/jamescooke/flake8-aaa/issues/185>`_.

Changed
.......

* ⛏️ Release notes updated to use a better method of updating Flake8 version
strings. Also reduce use of ``vx.y.z`` version strings - use just ``x.y.z``
instead.

See also `latest documentation
<https://flake8-aaa.readthedocs.io/en/latest/#__unreleased_marker__>`_.
instead. `Pull #207 <https://github.com/jamescooke/flake8-aaa/pull/207>`_.

0.13.1_ - 2023/02/27
--------------------
Expand Down
27 changes: 19 additions & 8 deletions docs/discovery.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,30 @@ When running as a Flake8 plugin, Flake8-AAA filters the Python code passed to
it by Flake8. It finds code that looks like test code and then checks that code
matches the AAA pattern. When all checks pass, then no error is raised.

Filtering
---------
File filtering
--------------

First, the filename is checked. It must either ``test.py``, ``tests.py`` or
start with ``test_``. For those files that match, every function that has a
name that starts with "test" is checked. This includes class methods.
First, the filename is checked. It must match one of the following patterns:

* Is called ``test.py`` or ``tests.py``.

* Starts with ``test_``, i.e match ``test_*.py``

* Ends with ``_test.py``, i.e. match ``*_test.py``.

For every file that matches the patterns above, Flake8-AAA checks every
function and class method whose name starts with "test".

Test functions and methods that contain only comments, docstrings or ``pass``
are skipped.

The aim of this process is to mirror Pytest's default collection strategy as
closely as possible. It also aims to work with popular testing tutorials such
as Django's `Writing your first Django app
Rationale
.........

The aim of this process is to mirror `Pytest's default collection strategy
<https://docs.pytest.org/en/7.2.x/explanation/goodpractices.html#test-discovery>`_
as closely as possible. It also aims to work with popular testing tutorials
such as Django's `Writing your first Django app
<https://docs.djangoproject.com/en/3.0/intro/tutorial05/#create-a-test-to-expose-the-bug>`_
which states:

Expand Down
1 change: 1 addition & 0 deletions examples/bad/bad_expected.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
examples/bad/file_pattern_test.py:4:1: AAA01 no Act block found in test
examples/bad/test.py:4:1: AAA01 no Act block found in test
examples/bad/test_aaa01.py:8:1: AAA01 no Act block found in test
examples/bad/test_aaa01_cm_import.py:11:1: AAA01 no Act block found in test
Expand Down
5 changes: 5 additions & 0 deletions examples/bad/file_pattern_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ensure that files with '*_test.py' patterns are found


def test() -> None:
assert 1 + 1 == 2
2 changes: 1 addition & 1 deletion examples/bad/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Check that files named "test.py" are checked


def test():
def test() -> None:
action = sorted([3, 2, 1])
assert action == [1, 2, 3]
2 changes: 1 addition & 1 deletion examples/bad/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Check that files named "tests.py" are checked


def test():
def test() -> None:
action = sorted([3, 2, 1])
assert action == [1, 2, 3]
6 changes: 3 additions & 3 deletions src/flake8_aaa/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

from asttokens.util import Token

test_file_pattern = re.compile(r'test(_.*|s)?\.py$')
test_file_pattern = re.compile(r'^(test(_.*|s)?|.*_test)\.py$')


def is_test_file(filename: str) -> bool:
"""
Check that path to file being checked passed by flake8 looks like a pytest
test file.
Returns:
Path to file passed by Flake8 looks like a Pytest test file.
"""
return bool(test_file_pattern.match(os.path.basename(filename)))

Expand Down
8 changes: 7 additions & 1 deletion tests/helpers/test_is_test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
# Non-test files give False.
('./helper.py', False),
('tests/conftest.py', False),
('./greatest.py', False),
('./greatest_dancer.py', False),
# Finds files that start with 'test_' to be test files.
('./test_helpers.py', True),
('test_.py', True),
('tests/helpers/test_is_test_file.py', True),
# Finds files that end with '_test' as test files
('./helpers_test.py', True),
('tests/helpers/is_test_file_test.py', True),
# Finds simple test file names "test.py" and "tests.py".
('./test.py', True),
('project/app/tests.py', True),
# Finds accidental additional underscore test file
('project/app/test_.py', True),
]
)
def test(path, expected_result):
def test(path: str, expected_result: bool) -> None:
result = is_test_file(path)

assert result is expected_result
4 changes: 4 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ commands =
examples/good/test_comments.py \
examples/good/test_with_statement.py \
examples/good/test_with_statement_unittest.py \
examples/bad/file_pattern_test.py \
examples/bad/test_aaa03.py \
examples/bad/test_aaa03_04.py \
examples/bad/test_aaa04.py \
Expand Down Expand Up @@ -149,6 +150,9 @@ allowlist_externals =
# are as expected.
[testenv:py3{7,8,9,10,11}-meta_command]
description = 🎈 Run command "-m flake8_aaa" on all examples
labels =
meta
meta_cmd
commands =
make cmd
make cmdbad
Expand Down

0 comments on commit f23db69

Please sign in to comment.