-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add error for over-complex Act block #77
Comments
Extracted this test case from with mock.patch(thing):
with pytest.raises(ValueError):
do_thing()
print('hi') Assert that |
My two cents in regards to the example with the context managers: I think extracting the mocking into a fixture would be more elegant: @pytest.fixture()
def patched_thing_thinger():
with mock.patch('thing.thinger') as mock_thinger:
yield mock_thinger
@pytest.fixture()
def patched_other_thing_thinger():
with mock.patch('other_thing.thinger') as mock_thinger:
yield mock_thinger
def test(patched_thing_thinger, patched_other_thing_thinger):
with pytest.raises(ValueError):
do_thing()
do_other_thing2() # < shouldn't be in Act
do_other_thing3() # < shouldn't be in Act
assert patched_thing_thinger.call_count == 1 Applicability may vary though (is |
@ROpdebee Thanks for pointing out that extraction is more elegant 👍 - I completely agree 😊 Sometimes I find it hard to write failing examples that are bad enough and real world enough both in Issues and in the https://github.com/jamescooke/flake8-aaa/tree/master/examples/bad dir... Which is why you see function names like |
Reassess this after #146 is done. 👀 |
Extra example that just came up at work - a false positive, where the def test_assert_ddex_equal_failed(
audit_test_class, input1, input2, err_msg
) -> None:
with pytest.raises(AssertionError) as excinfo:
audit_test_class.assertDDEXEqual(input1, input2)
assert err_msg in str(excinfo.value)
|
An Act block can be overly complex when parent nodes of the Act node contain "extra" lines.
In a simple example, an Act node with a context manager ('pytest_raises' or 'unittest_raises' types) can wrap other nodes "below" it.
In this case
do_other_thing()
can be "smuggled" into the Act block because it's wrapped in thepytest.raises()
context manager.A more complex example, extra statements can be wrapped by context managers which are found that are parents of the Act node:
In this case,
do_other_thing2()
anddo_other_thing3()
are wrapped by themock.patch()
context managers and are then "smuggled" into the Act block.Requirements
Add non-blocking rule for over-complex act block. Point error raised at the additional lines. Resolution is to move the additional lines outside of any act block, maybe to extract them to a fixture or
setUp()
function.Checking of Act block should be wired in to happen after Act block is linted.
Add documentation and tests.
The text was updated successfully, but these errors were encountered: