|
1 | 1 | import os |
2 | 2 | import textwrap |
3 | 3 |
|
4 | | -import py |
| 4 | +import py.path |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 | from _pytest.config import ExitCode |
8 | 8 | from _pytest.config import PytestPluginManager |
9 | 9 | from _pytest.pathlib import Path |
| 10 | +from _pytest.pytester import Testdir |
10 | 11 |
|
11 | 12 |
|
12 | 13 | def ConftestWithSetinitial(path): |
@@ -174,19 +175,25 @@ def test_setinitial_conftest_subdirs(testdir, name): |
174 | 175 | assert len(conftest._conftestpath2mod) == 0 |
175 | 176 |
|
176 | 177 |
|
177 | | -def test_conftest_confcutdir(testdir): |
178 | | - testdir.makeconftest("assert 0") |
179 | | - x = testdir.mkdir("x") |
180 | | - x.join("conftest.py").write( |
181 | | - textwrap.dedent( |
182 | | - """\ |
183 | | - def pytest_addoption(parser): |
184 | | - parser.addoption("--xyz", action="store_true") |
185 | | - """ |
186 | | - ) |
| 178 | +@pytest.fixture |
| 179 | +def conftest_in_tests(testdir: Testdir) -> py.path.local: |
| 180 | + return testdir.makepyfile( |
| 181 | + **{ |
| 182 | + "tests/conftest": """ |
| 183 | + def pytest_addoption(parser): |
| 184 | + parser.addoption("--xyz", action="store_const", const=True) |
| 185 | + """ |
| 186 | + } |
187 | 187 | ) |
| 188 | + |
| 189 | + |
| 190 | +def test_conftest_confcutdir( |
| 191 | + testdir: Testdir, conftest_in_tests: py.path.local |
| 192 | +) -> None: |
| 193 | + testdir.makeconftest("assert 0") |
| 194 | + x = conftest_in_tests.dirpath() |
188 | 195 | result = testdir.runpytest("-h", "--confcutdir=%s" % x, x) |
189 | | - result.stdout.fnmatch_lines(["*--xyz*"]) |
| 196 | + result.stdout.fnmatch_lines(["custom options:", " --xyz"]) |
190 | 197 | result.stdout.no_fnmatch_line("*warning: could not load initial*") |
191 | 198 |
|
192 | 199 |
|
@@ -319,31 +326,17 @@ def test_no_conftest(testdir): |
319 | 326 | assert result.ret == ExitCode.USAGE_ERROR |
320 | 327 |
|
321 | 328 |
|
322 | | -def test_conftest_existing_resultlog(testdir): |
323 | | - x = testdir.mkdir("tests") |
324 | | - x.join("conftest.py").write( |
325 | | - textwrap.dedent( |
326 | | - """\ |
327 | | - def pytest_addoption(parser): |
328 | | - parser.addoption("--xyz", action="store_true") |
329 | | - """ |
330 | | - ) |
331 | | - ) |
| 329 | +def test_conftest_existing_resultlog( |
| 330 | + testdir: Testdir, conftest_in_tests: py.path.local |
| 331 | +) -> None: |
332 | 332 | testdir.makefile(ext=".log", result="") # Writes result.log |
333 | 333 | result = testdir.runpytest("-h", "--resultlog", "result.log") |
334 | 334 | result.stdout.fnmatch_lines(["*--xyz*"]) |
335 | 335 |
|
336 | 336 |
|
337 | | -def test_conftest_existing_junitxml(testdir): |
338 | | - x = testdir.mkdir("tests") |
339 | | - x.join("conftest.py").write( |
340 | | - textwrap.dedent( |
341 | | - """\ |
342 | | - def pytest_addoption(parser): |
343 | | - parser.addoption("--xyz", action="store_true") |
344 | | - """ |
345 | | - ) |
346 | | - ) |
| 337 | +def test_conftest_existing_junitxml( |
| 338 | + testdir: Testdir, conftest_in_tests: py.path.local |
| 339 | +) -> None: |
347 | 340 | testdir.makefile(ext=".xml", junit="") # Writes junit.xml |
348 | 341 | result = testdir.runpytest("-h", "--junitxml", "junit.xml") |
349 | 342 | result.stdout.fnmatch_lines(["*--xyz*"]) |
@@ -409,24 +402,13 @@ def test_event_fixture(bar): |
409 | 402 | result.stdout.fnmatch_lines(["*1 passed*"]) |
410 | 403 |
|
411 | 404 |
|
412 | | -def test_conftest_found_with_double_dash(testdir): |
413 | | - sub = testdir.mkdir("sub") |
414 | | - sub.join("conftest.py").write( |
415 | | - textwrap.dedent( |
416 | | - """\ |
417 | | - def pytest_addoption(parser): |
418 | | - parser.addoption("--hello-world", action="store_true") |
419 | | - """ |
420 | | - ) |
421 | | - ) |
422 | | - p = sub.join("test_hello.py") |
| 405 | +def test_conftest_found_with_double_dash_in_arg( |
| 406 | + testdir: Testdir, conftest_in_tests: py.path.local |
| 407 | +) -> None: |
| 408 | + p = conftest_in_tests.dirpath().join("test_hello.py") |
423 | 409 | p.write("def test_hello(): pass") |
424 | 410 | result = testdir.runpytest(str(p) + "::test_hello", "-h") |
425 | | - result.stdout.fnmatch_lines( |
426 | | - """ |
427 | | - *--hello-world* |
428 | | - """ |
429 | | - ) |
| 411 | + result.stdout.fnmatch_lines(["*--xyz*"]) |
430 | 412 |
|
431 | 413 |
|
432 | 414 | class TestConftestVisibility: |
@@ -643,17 +625,13 @@ def pytest_ignore_collect(path, config): |
643 | 625 | ) |
644 | 626 |
|
645 | 627 |
|
646 | | -def test_required_option_help(testdir): |
647 | | - testdir.makeconftest("assert 0") |
648 | | - x = testdir.mkdir("x") |
649 | | - x.join("conftest.py").write( |
650 | | - textwrap.dedent( |
651 | | - """\ |
652 | | - def pytest_addoption(parser): |
653 | | - parser.addoption("--xyz", action="store_true", required=True) |
654 | | - """ |
655 | | - ) |
| 628 | +def test_required_option_help(testdir: Testdir) -> None: |
| 629 | + testdir.makeconftest( |
| 630 | + """ |
| 631 | + def pytest_addoption(parser): |
| 632 | + parser.addoption("--xyz", action="store_true", required=True) |
| 633 | + """ |
656 | 634 | ) |
657 | | - result = testdir.runpytest("-h", x) |
| 635 | + result = testdir.runpytest("-h") |
658 | 636 | result.stdout.no_fnmatch_line("*argument --xyz is required*") |
659 | | - assert "general:" in result.stdout.str() |
| 637 | + result.stdout.fnmatch_lines(["general:", "custom options:", " --[no-]xyz"]) |
0 commit comments