-
Notifications
You must be signed in to change notification settings - Fork 4
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 fixture usage validator #8
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,48 @@ | ||
import ast | ||
import typing | ||
|
||
from flake8_fine_pytest.watchers.base import BaseWatcher | ||
|
||
|
||
class UsefixturesWatcher(BaseWatcher): | ||
error_template = ( | ||
'FP009 {test_name} should use fixtures as follows: ' | ||
'@pytest.mark.usefixtures({fixtures_list_as_str})' | ||
) | ||
|
||
def run(self) -> None: | ||
if self._should_run(): | ||
self._validate_usefixtures_used_where_possible(self.tree) | ||
|
||
def _validate_usefixtures_used_where_possible(self, tree: ast.AST) -> None: | ||
for node in ast.walk(tree): | ||
if not self._should_check_node(node): | ||
continue | ||
|
||
fixture_names = self._get_unreferenced_fixture_names(node) # type: ignore | ||
if fixture_names: | ||
self._add_usefixtures_error(node, fixture_names) # type: ignore | ||
|
||
def _get_unreferenced_fixture_names( | ||
self, | ||
function_node: ast.FunctionDef, | ||
) -> typing.List[str]: | ||
referenced_variable_names = { | ||
node.id | ||
for node in ast.walk(function_node) | ||
if isinstance(node, ast.Name) | ||
} | ||
test_fixture_names = {arg.arg for arg in function_node.args.args} | ||
|
||
return sorted(test_fixture_names - referenced_variable_names) | ||
|
||
def _add_usefixtures_error( | ||
self, | ||
function_node: ast.FunctionDef, | ||
fixture_names: typing.List[str], | ||
) -> None: | ||
error_message = self.error_template.format( | ||
test_name=function_node.name, | ||
fixtures_list_as_str=', '.join(repr(name) for name in fixture_names), | ||
) | ||
self.add_error((function_node.lineno, 0, error_message)) |
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 |
---|---|---|
|
@@ -16,7 +16,6 @@ def three(): | |
pass | ||
|
||
|
||
|
||
def test_with_too_complex_signature(one, two, three): | ||
assert (2 + 2) == 4 | ||
|
||
|
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,10 @@ | ||
import pytest | ||
|
||
|
||
def test_with_no_usefixtures_where_needed(caplog, capsys, tmp_path): | ||
assert capsys | ||
|
||
|
||
@pytest.mark.usefixtures('caplog', 'tmp_path') | ||
def test_with_usefixtures_where_needed(capsys): | ||
assert capsys |
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,8 @@ | ||
def test_signature_complexity(run_validator_for_test_files): | ||
errors = run_validator_for_test_files('test_complex_signature_tests.py', allowed_test_arguments_count=2) | ||
errors = run_validator_for_test_files( | ||
'test_complex_signature_tests.py', | ||
allowed_test_arguments_count=2, | ||
) | ||
FP004s = [error for error in errors if 'FP004' in error[2]] | ||
|
||
assert len(errors) == 1 | ||
assert len(FP004s) == 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def test_fixtures_should_be_in_usefixtures(run_validator_for_test_files): | ||
expected_error_message = ( | ||
'FP009 test_with_no_usefixtures_where_needed should use ' | ||
"fixtures as follows: @pytest.mark.usefixtures('caplog', 'tmp_path')" | ||
) | ||
|
||
errors = run_validator_for_test_files('test_with_fixtures.py') | ||
|
||
assert len(errors) == 1 | ||
assert errors[0][2] == expected_error_message |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the better solution is to move this method to
BaseWatcher
and override it if we need. I already suggested it in another PR