Skip to content

Commit

Permalink
Merge pull request #2 from best-doctor/master
Browse files Browse the repository at this point in the history
Adds unique tests names validator (best-doctor#7)
  • Loading branch information
micheller authored Dec 1, 2020
2 parents c81fa1a + 707ce5e commit ef11d35
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
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:
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)'

0 comments on commit ef11d35

Please sign in to comment.