Skip to content

Commit 526d9b0

Browse files
authored
Merge pull request #779 from pre-commit/test-conventions
name-test-tests: add --pytest-test-first convention
2 parents 621f50e + 412564f commit 526d9b0

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

.pre-commit-hooks.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
types: [text]
165165
- id: name-tests-test
166166
name: python tests naming
167-
description: this verifies that test files are named correctly.
167+
description: verifies that test files are named correctly.
168168
entry: name-tests-test
169169
language: python
170170
files: (^|/)tests/.+\.py$

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ Replaces or checks mixed line ending.
142142
- `no` - Checks if there is any mixed line ending without modifying any file.
143143

144144
#### `name-tests-test`
145-
Assert that files in tests/ end in `_test.py`.
146-
- Use `args: ['--django']` to match `test*.py` instead.
145+
verifies that test files are named correctly.
146+
- `--pytest` (the default): ensure tests match `.*_test\.py`
147+
- `--pytest-test-first`: ensure tests match `test_.*\.py`
148+
- `--django` / `--unittest`: ensure tests match `test.*\.py`
147149

148150
#### `no-commit-to-branch`
149151
Protect specific branches from direct checkins.

pre_commit_hooks/tests_should_end_in_test.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,42 @@
99
def main(argv: Sequence[str] | None = None) -> int:
1010
parser = argparse.ArgumentParser()
1111
parser.add_argument('filenames', nargs='*')
12-
parser.add_argument(
13-
'--django', default=False, action='store_true',
14-
help='Use Django-style test naming pattern (test*.py)',
12+
mutex = parser.add_mutually_exclusive_group()
13+
mutex.add_argument(
14+
'--pytest',
15+
dest='pattern',
16+
action='store_const',
17+
const=r'.*_test\.py',
18+
default=r'.*_test\.py',
19+
help='(the default) ensure tests match %(const)s',
20+
)
21+
mutex.add_argument(
22+
'--pytest-test-first',
23+
dest='pattern',
24+
action='store_const',
25+
const=r'test_.*\.py',
26+
help='ensure tests match %(const)s',
27+
)
28+
mutex.add_argument(
29+
'--django', '--unittest',
30+
dest='pattern',
31+
action='store_const',
32+
const=r'test.*\.py',
33+
help='ensure tests match %(const)s',
1534
)
1635
args = parser.parse_args(argv)
1736

1837
retcode = 0
19-
test_name_pattern = r'test.*\.py' if args.django else r'.*_test\.py'
38+
reg = re.compile(args.pattern)
2039
for filename in args.filenames:
2140
base = os.path.basename(filename)
2241
if (
23-
not re.match(test_name_pattern, base) and
42+
not reg.fullmatch(base) and
2443
not base == '__init__.py' and
2544
not base == 'conftest.py'
2645
):
2746
retcode = 1
28-
print(f'{filename} does not match pattern "{test_name_pattern}"')
47+
print(f'{filename} does not match pattern "{args.pattern}"')
2948

3049
return retcode
3150

tests/tests_should_end_in_test_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ def test_main_not_django_fails():
4343
def test_main_django_fails():
4444
ret = main(['--django', 'foo_test.py', 'test_bar.py', 'test_baz.py'])
4545
assert ret == 1
46+
47+
48+
def test_main_pytest_test_first():
49+
assert main(['--pytest-test-first', 'test_foo.py']) == 0
50+
assert main(['--pytest-test-first', 'foo_test.py']) == 1

0 commit comments

Comments
 (0)