-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add individual docs pages for AAA02 and AAA04 errors (#234)
* WIP add test example for AAA02, plus tweak docs * Add note about pytest.raises() in Assert blocks, fix missing option ref * Add examples for AAA02 * Build out AAA02 doc * Build out AAA04 doc * Update CHANGELOG * Fix bad expected output after comment added
- Loading branch information
1 parent
aca7194
commit 92db079
Showing
11 changed files
with
150 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 52 additions & 10 deletions
62
docs/error_codes/AAA02-multiple-act-blocks-found-in-test.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,56 @@ | ||
AAA02: multiple Act blocks found in test | ||
---------------------------------------- | ||
======================================== | ||
|
||
There must be one and only one Act block in every test but Flake8-AAA found | ||
more than one potential Act block. This error is usually triggered when a test | ||
contains more than one ``result =`` statement or more than one line marked ``# | ||
act``. Multiple Act blocks create ambiguity and raise this error code. | ||
Flake8-AAA checks that every test has a single, clear Act block. | ||
|
||
Resolution | ||
.......... | ||
When Flake8-AAA raises ``AAA02`` it found more than one Act block in a | ||
particular test. | ||
|
||
Split the failing test into multiple tests. Where there is complicated or | ||
reused set-up code then apply the DRY principle and extract the reused code | ||
into one or more fixtures. | ||
Problematic code | ||
---------------- | ||
|
||
.. code-block:: python | ||
def test() -> None: | ||
x = 1 | ||
y = 2 | ||
result = x + y | ||
assert result == 3 | ||
result = 2 * x + 2 * y | ||
assert result == 6 | ||
Correct code | ||
------------ | ||
|
||
Split the one test with two Act blocks into two distinct tests. | ||
|
||
.. code-block:: python | ||
def test_A() -> None: | ||
x = 1 | ||
y = 2 | ||
result = x + y | ||
assert result == 3 | ||
def test_B() -> None: | ||
x = 1 | ||
y = 2 | ||
result = 2 * x + 2 * y | ||
assert result == 6 | ||
Rationale | ||
--------- | ||
|
||
Each test carries out a single action and tests its result. | ||
|
||
Having multiple actions in a test create ambiguity because it can become less | ||
clear which behaviour is being tested. | ||
|
||
Where there is complicated or reused set-up code, then apply the DRY principle | ||
and extract the reused code into one or more fixtures. |
40 changes: 34 additions & 6 deletions
40
docs/error_codes/AAA04-expected-1-blank-line-before-assert-block.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,43 @@ | ||
AAA04: expected 1 blank line before Assert block, found none | ||
------------------------------------------------------------ | ||
============================================================ | ||
|
||
For tests that have an Assert block, there must be a blank line between the Act | ||
and Assert blocks, but Flake8-AAA could not find one. | ||
|
||
This blank line creates separation between the action and the assertions and | ||
makes the Act block easy to spot. | ||
Prerequisites | ||
------------- | ||
|
||
This rule works best with `pycodestyle | ||
<https://pypi.org/project/pycodestyle/>`_'s ``E303`` rule enabled because it | ||
ensures that there are not multiple blank lines between the blocks. | ||
|
||
Problematic code | ||
---------------- | ||
|
||
As with rule ``AAA03``, this rule works best with ``E303`` enabled. | ||
.. code-block:: python | ||
Resolution | ||
.......... | ||
def test() -> None: | ||
x = 3 | ||
result = x**5 | ||
assert result == 243 | ||
Correct code | ||
------------ | ||
|
||
Add a blank line before the Assert block. | ||
|
||
.. code-block:: python | ||
def test() -> None: | ||
x = 3 | ||
result = x**5 | ||
assert result == 243 | ||
Rationale | ||
--------- | ||
|
||
This blank line creates separation between the action and the assertions - it | ||
makes the Act block easy to spot. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# NOTE: tests _can_ use pytest.raises() in Assert blocks. See discovery docs | ||
# and examples/good/test_with_statement.py | ||
|
||
|
||
def test() -> None: | ||
""" | ||
Two result assignment statements will raise AAA02 | ||
""" | ||
x = 1 | ||
y = 2 | ||
|
||
result = x + y | ||
|
||
assert result == 3 | ||
result = 2 * x + 2 * y | ||
assert result == 6 | ||
|
||
|
||
def test_act() -> None: | ||
""" | ||
One result and act hint will raise | ||
""" | ||
x = 1 | ||
y = 2 | ||
|
||
result = x + y | ||
|
||
assert result == 3 # act |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
def test(): # noqa: AAA02 | ||
def test() -> None: # noqa: AAA02 | ||
result = 1 | ||
|
||
assert result == 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
def test(): # noqa: AAA02 | ||
def test() -> None: # noqa: AAA02 | ||
result = 1 | ||
|
||
assert result == 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters