Skip to content
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

Adds unique tests names validator #7

Merged
merged 8 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,7 @@ venv.bak/
/site

# mypy
.mypy_cache/
.mypy_cache/

# IDE
.idea
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ python:
- "3.7"
- "3.8"
install:
- gem install chef-utils -v 16.6.14
- gem install mdl
- pip install -r requirements.txt
- pip install -e .
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ tests/test_unit/test_something.py:2:0: FP009 test_something should use fixtures
as follows: @pytest.mark.usefixtures('fixture_one')
```

7) validates that test function uses unique names

## Installation

```terminal
Expand Down
2 changes: 2 additions & 0 deletions flake8_fine_pytest/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from flake8_fine_pytest import __version__ as version
from flake8_fine_pytest.watchers.modules_structure import ModulesStructureWatcher
from flake8_fine_pytest.watchers.unique_test_names import UniqueTestNamesWatcher
from flake8_fine_pytest.watchers.xfail_decorator import XfailWatcher
from flake8_fine_pytest.watchers.xfail_until_argument_watcher import (
XfailUntilArgumentWatcher,
Expand All @@ -26,6 +27,7 @@ class FinePytestChecker:
AssertCountWatcher,
XfailUntilArgumentWatcher,
UsefixturesWatcher,
UniqueTestNamesWatcher,
)

def __init__(self, tree: ast.AST, filename: str):
Expand Down
27 changes: 27 additions & 0 deletions flake8_fine_pytest/watchers/unique_test_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import ast
from typing import Set

from flake8_fine_pytest.watchers.base import BaseWatcher


class UniqueTestNamesWatcher(BaseWatcher):
error_template = 'FP009 Duplicate name test case ({})'

def run(self) -> None:
testcases_names: Set[str] = set()

if not self._is_test_file(self.filename):
return

for node in ast.walk(self.tree):
if not self._should_check_node(node):
continue

node_name = node.name # type: ignore

if node_name not in testcases_names:
mcproger marked this conversation as resolved.
Show resolved Hide resolved
testcases_names.add(node_name)
continue

error_msg = self.error_template.format(node_name)
self.add_error((node.lineno, 0, error_msg))
19 changes: 19 additions & 0 deletions tests/test_files/test_not_unique.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest


def test_not_uniq():
pass


def test_not_uniq():
pass


@pytest.mark.skip
def test_not_uniq_with_decorator():
pass


@pytest.mark.skip
def test_not_uniq_with_decorator():
pass
6 changes: 6 additions & 0 deletions tests/test_integration/test_unique_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def test_unique_names(run_validator_for_test_files):
errors = run_validator_for_test_files('test_not_unique.py')

assert len(errors) == 2
assert errors[0][2] == 'FP009 Duplicate name test case (test_not_uniq)'
assert errors[1][2] == 'FP009 Duplicate name test case (test_not_uniq_with_decorator)'