Merge pull request #7944 from symonk/refactor-test-mark-to-use-pytester

Refactor test_warning_types.py & test_mark.py to use pytester
This commit is contained in:
Ran Benita 2020-10-27 21:06:42 +02:00 committed by GitHub
commit 78c09b9931
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 197 additions and 175 deletions

View File

@ -1,13 +1,16 @@
import os import os
import sys import sys
from typing import List
from typing import Optional
from unittest import mock from unittest import mock
import pytest import pytest
from _pytest.config import ExitCode from _pytest.config import ExitCode
from _pytest.mark import MarkGenerator as Mark from _pytest.mark import MarkGenerator
from _pytest.mark.structures import EMPTY_PARAMETERSET_OPTION from _pytest.mark.structures import EMPTY_PARAMETERSET_OPTION
from _pytest.nodes import Collector from _pytest.nodes import Collector
from _pytest.nodes import Node from _pytest.nodes import Node
from _pytest.pytester import Pytester
class TestMark: class TestMark:
@ -18,7 +21,7 @@ class TestMark:
assert attr in module.__all__ # type: ignore assert attr in module.__all__ # type: ignore
def test_pytest_mark_notcallable(self) -> None: def test_pytest_mark_notcallable(self) -> None:
mark = Mark() mark = MarkGenerator()
with pytest.raises(TypeError): with pytest.raises(TypeError):
mark() # type: ignore[operator] mark() # type: ignore[operator]
@ -36,17 +39,17 @@ class TestMark:
assert pytest.mark.foo(SomeClass) is SomeClass assert pytest.mark.foo(SomeClass) is SomeClass
assert pytest.mark.foo.with_args(SomeClass) is not SomeClass # type: ignore[comparison-overlap] assert pytest.mark.foo.with_args(SomeClass) is not SomeClass # type: ignore[comparison-overlap]
def test_pytest_mark_name_starts_with_underscore(self): def test_pytest_mark_name_starts_with_underscore(self) -> None:
mark = Mark() mark = MarkGenerator()
with pytest.raises(AttributeError): with pytest.raises(AttributeError):
mark._some_name mark._some_name
def test_marked_class_run_twice(testdir): def test_marked_class_run_twice(pytester: Pytester) -> None:
"""Test fails file is run twice that contains marked class. """Test fails file is run twice that contains marked class.
See issue#683. See issue#683.
""" """
py_file = testdir.makepyfile( py_file = pytester.makepyfile(
""" """
import pytest import pytest
@pytest.mark.parametrize('abc', [1, 2, 3]) @pytest.mark.parametrize('abc', [1, 2, 3])
@ -55,13 +58,13 @@ def test_marked_class_run_twice(testdir):
assert abc in [1, 2, 3] assert abc in [1, 2, 3]
""" """
) )
file_name = os.path.basename(py_file.strpath) file_name = os.path.basename(py_file)
rec = testdir.inline_run(file_name, file_name) rec = pytester.inline_run(file_name, file_name)
rec.assertoutcome(passed=6) rec.assertoutcome(passed=6)
def test_ini_markers(testdir): def test_ini_markers(pytester: Pytester) -> None:
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
markers = markers =
@ -69,7 +72,7 @@ def test_ini_markers(testdir):
a2: this is a smoke marker a2: this is a smoke marker
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
def test_markers(pytestconfig): def test_markers(pytestconfig):
markers = pytestconfig.getini("markers") markers = pytestconfig.getini("markers")
@ -79,12 +82,12 @@ def test_ini_markers(testdir):
assert markers[1].startswith("a2:") assert markers[1].startswith("a2:")
""" """
) )
rec = testdir.inline_run() rec = pytester.inline_run()
rec.assertoutcome(passed=1) rec.assertoutcome(passed=1)
def test_markers_option(testdir): def test_markers_option(pytester: Pytester) -> None:
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
markers = markers =
@ -93,21 +96,21 @@ def test_markers_option(testdir):
nodescription nodescription
""" """
) )
result = testdir.runpytest("--markers") result = pytester.runpytest("--markers")
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
["*a1*this is a webtest*", "*a1some*another marker", "*nodescription*"] ["*a1*this is a webtest*", "*a1some*another marker", "*nodescription*"]
) )
def test_ini_markers_whitespace(testdir): def test_ini_markers_whitespace(pytester: Pytester) -> None:
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
markers = markers =
a1 : this is a whitespace marker a1 : this is a whitespace marker
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -116,33 +119,33 @@ def test_ini_markers_whitespace(testdir):
assert True assert True
""" """
) )
rec = testdir.inline_run("--strict-markers", "-m", "a1") rec = pytester.inline_run("--strict-markers", "-m", "a1")
rec.assertoutcome(passed=1) rec.assertoutcome(passed=1)
def test_marker_without_description(testdir): def test_marker_without_description(pytester: Pytester) -> None:
testdir.makefile( pytester.makefile(
".cfg", ".cfg",
setup=""" setup="""
[tool:pytest] [tool:pytest]
markers=slow markers=slow
""", """,
) )
testdir.makeconftest( pytester.makeconftest(
""" """
import pytest import pytest
pytest.mark.xfail('FAIL') pytest.mark.xfail('FAIL')
""" """
) )
ftdir = testdir.mkdir("ft1_dummy") ftdir = pytester.mkdir("ft1_dummy")
testdir.tmpdir.join("conftest.py").move(ftdir.join("conftest.py")) pytester.path.joinpath("conftest.py").replace(ftdir.joinpath("conftest.py"))
rec = testdir.runpytest("--strict-markers") rec = pytester.runpytest("--strict-markers")
rec.assert_outcomes() rec.assert_outcomes()
def test_markers_option_with_plugin_in_current_dir(testdir): def test_markers_option_with_plugin_in_current_dir(pytester: Pytester) -> None:
testdir.makeconftest('pytest_plugins = "flip_flop"') pytester.makeconftest('pytest_plugins = "flip_flop"')
testdir.makepyfile( pytester.makepyfile(
flip_flop="""\ flip_flop="""\
def pytest_configure(config): def pytest_configure(config):
config.addinivalue_line("markers", "flip:flop") config.addinivalue_line("markers", "flip:flop")
@ -154,7 +157,7 @@ def test_markers_option_with_plugin_in_current_dir(testdir):
return return
metafunc.parametrize("x", (10, 20))""" metafunc.parametrize("x", (10, 20))"""
) )
testdir.makepyfile( pytester.makepyfile(
"""\ """\
import pytest import pytest
@pytest.mark.flipper @pytest.mark.flipper
@ -162,12 +165,12 @@ def test_markers_option_with_plugin_in_current_dir(testdir):
assert x""" assert x"""
) )
result = testdir.runpytest("--markers") result = pytester.runpytest("--markers")
result.stdout.fnmatch_lines(["*flip*flop*"]) result.stdout.fnmatch_lines(["*flip*flop*"])
def test_mark_on_pseudo_function(testdir): def test_mark_on_pseudo_function(pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -176,13 +179,15 @@ def test_mark_on_pseudo_function(testdir):
pass pass
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
@pytest.mark.parametrize("option_name", ["--strict-markers", "--strict"]) @pytest.mark.parametrize("option_name", ["--strict-markers", "--strict"])
def test_strict_prohibits_unregistered_markers(testdir, option_name): def test_strict_prohibits_unregistered_markers(
testdir.makepyfile( pytester: Pytester, option_name: str
) -> None:
pytester.makepyfile(
""" """
import pytest import pytest
@pytest.mark.unregisteredmark @pytest.mark.unregisteredmark
@ -190,7 +195,7 @@ def test_strict_prohibits_unregistered_markers(testdir, option_name):
pass pass
""" """
) )
result = testdir.runpytest(option_name) result = pytester.runpytest(option_name)
assert result.ret != 0 assert result.ret != 0
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
["'unregisteredmark' not found in `markers` configuration option"] ["'unregisteredmark' not found in `markers` configuration option"]
@ -208,8 +213,10 @@ def test_strict_prohibits_unregistered_markers(testdir, option_name):
("xyz or xyz2", ["test_one", "test_two"]), ("xyz or xyz2", ["test_one", "test_two"]),
], ],
) )
def test_mark_option(expr: str, expected_passed: str, testdir) -> None: def test_mark_option(
testdir.makepyfile( expr: str, expected_passed: List[Optional[str]], pytester: Pytester
) -> None:
pytester.makepyfile(
""" """
import pytest import pytest
@pytest.mark.xyz @pytest.mark.xyz
@ -220,18 +227,20 @@ def test_mark_option(expr: str, expected_passed: str, testdir) -> None:
pass pass
""" """
) )
rec = testdir.inline_run("-m", expr) rec = pytester.inline_run("-m", expr)
passed, skipped, fail = rec.listoutcomes() passed, skipped, fail = rec.listoutcomes()
passed = [x.nodeid.split("::")[-1] for x in passed] passed_str = [x.nodeid.split("::")[-1] for x in passed]
assert passed == expected_passed assert passed_str == expected_passed
@pytest.mark.parametrize( @pytest.mark.parametrize(
("expr", "expected_passed"), ("expr", "expected_passed"),
[("interface", ["test_interface"]), ("not interface", ["test_nointer"])], [("interface", ["test_interface"]), ("not interface", ["test_nointer"])],
) )
def test_mark_option_custom(expr: str, expected_passed: str, testdir) -> None: def test_mark_option_custom(
testdir.makeconftest( expr: str, expected_passed: List[str], pytester: Pytester
) -> None:
pytester.makeconftest(
""" """
import pytest import pytest
def pytest_collection_modifyitems(items): def pytest_collection_modifyitems(items):
@ -240,7 +249,7 @@ def test_mark_option_custom(expr: str, expected_passed: str, testdir) -> None:
item.add_marker(pytest.mark.interface) item.add_marker(pytest.mark.interface)
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
def test_interface(): def test_interface():
pass pass
@ -248,10 +257,10 @@ def test_mark_option_custom(expr: str, expected_passed: str, testdir) -> None:
pass pass
""" """
) )
rec = testdir.inline_run("-m", expr) rec = pytester.inline_run("-m", expr)
passed, skipped, fail = rec.listoutcomes() passed, skipped, fail = rec.listoutcomes()
passed = [x.nodeid.split("::")[-1] for x in passed] passed_str = [x.nodeid.split("::")[-1] for x in passed]
assert passed == expected_passed assert passed_str == expected_passed
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -266,8 +275,10 @@ def test_mark_option_custom(expr: str, expected_passed: str, testdir) -> None:
("not (1 or 2)", ["test_interface", "test_nointer", "test_pass"]), ("not (1 or 2)", ["test_interface", "test_nointer", "test_pass"]),
], ],
) )
def test_keyword_option_custom(expr: str, expected_passed: str, testdir) -> None: def test_keyword_option_custom(
testdir.makepyfile( expr: str, expected_passed: List[str], pytester: Pytester
) -> None:
pytester.makepyfile(
""" """
def test_interface(): def test_interface():
pass pass
@ -281,15 +292,15 @@ def test_keyword_option_custom(expr: str, expected_passed: str, testdir) -> None
pass pass
""" """
) )
rec = testdir.inline_run("-k", expr) rec = pytester.inline_run("-k", expr)
passed, skipped, fail = rec.listoutcomes() passed, skipped, fail = rec.listoutcomes()
passed = [x.nodeid.split("::")[-1] for x in passed] passed_str = [x.nodeid.split("::")[-1] for x in passed]
assert passed == expected_passed assert passed_str == expected_passed
def test_keyword_option_considers_mark(testdir): def test_keyword_option_considers_mark(pytester: Pytester) -> None:
testdir.copy_example("marks/marks_considered_keywords") pytester.copy_example("marks/marks_considered_keywords")
rec = testdir.inline_run("-k", "foo") rec = pytester.inline_run("-k", "foo")
passed = rec.listoutcomes()[0] passed = rec.listoutcomes()[0]
assert len(passed) == 1 assert len(passed) == 1
@ -302,8 +313,10 @@ def test_keyword_option_considers_mark(testdir):
("2-3", ["test_func[2-3]"]), ("2-3", ["test_func[2-3]"]),
], ],
) )
def test_keyword_option_parametrize(expr: str, expected_passed: str, testdir) -> None: def test_keyword_option_parametrize(
testdir.makepyfile( expr: str, expected_passed: List[str], pytester: Pytester
) -> None:
pytester.makepyfile(
""" """
import pytest import pytest
@pytest.mark.parametrize("arg", [None, 1.3, "2-3"]) @pytest.mark.parametrize("arg", [None, 1.3, "2-3"])
@ -311,14 +324,14 @@ def test_keyword_option_parametrize(expr: str, expected_passed: str, testdir) ->
pass pass
""" """
) )
rec = testdir.inline_run("-k", expr) rec = pytester.inline_run("-k", expr)
passed, skipped, fail = rec.listoutcomes() passed, skipped, fail = rec.listoutcomes()
passed = [x.nodeid.split("::")[-1] for x in passed] passed_str = [x.nodeid.split("::")[-1] for x in passed]
assert passed == expected_passed assert passed_str == expected_passed
def test_parametrize_with_module(testdir): def test_parametrize_with_module(pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@pytest.mark.parametrize("arg", [pytest,]) @pytest.mark.parametrize("arg", [pytest,])
@ -326,7 +339,7 @@ def test_parametrize_with_module(testdir):
pass pass
""" """
) )
rec = testdir.inline_run() rec = pytester.inline_run()
passed, skipped, fail = rec.listoutcomes() passed, skipped, fail = rec.listoutcomes()
expected_id = "test_func[" + pytest.__name__ + "]" expected_id = "test_func[" + pytest.__name__ + "]"
assert passed[0].nodeid.split("::")[-1] == expected_id assert passed[0].nodeid.split("::")[-1] == expected_id
@ -356,23 +369,23 @@ def test_parametrize_with_module(testdir):
], ],
) )
def test_keyword_option_wrong_arguments( def test_keyword_option_wrong_arguments(
expr: str, expected_error: str, testdir, capsys expr: str, expected_error: str, pytester: Pytester, capsys
) -> None: ) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
def test_func(arg): def test_func(arg):
pass pass
""" """
) )
testdir.inline_run("-k", expr) pytester.inline_run("-k", expr)
err = capsys.readouterr().err err = capsys.readouterr().err
assert expected_error in err assert expected_error in err
def test_parametrized_collected_from_command_line(testdir): def test_parametrized_collected_from_command_line(pytester: Pytester) -> None:
"""Parametrized test not collected if test named specified in command """Parametrized test not collected if test named specified in command
line issue#649.""" line issue#649."""
py_file = testdir.makepyfile( py_file = pytester.makepyfile(
""" """
import pytest import pytest
@pytest.mark.parametrize("arg", [None, 1.3, "2-3"]) @pytest.mark.parametrize("arg", [None, 1.3, "2-3"])
@ -380,14 +393,14 @@ def test_parametrized_collected_from_command_line(testdir):
pass pass
""" """
) )
file_name = os.path.basename(py_file.strpath) file_name = os.path.basename(py_file)
rec = testdir.inline_run(file_name + "::" + "test_func") rec = pytester.inline_run(file_name + "::" + "test_func")
rec.assertoutcome(passed=3) rec.assertoutcome(passed=3)
def test_parametrized_collect_with_wrong_args(testdir): def test_parametrized_collect_with_wrong_args(pytester: Pytester) -> None:
"""Test collect parametrized func with wrong number of args.""" """Test collect parametrized func with wrong number of args."""
py_file = testdir.makepyfile( py_file = pytester.makepyfile(
""" """
import pytest import pytest
@ -397,7 +410,7 @@ def test_parametrized_collect_with_wrong_args(testdir):
""" """
) )
result = testdir.runpytest(py_file) result = pytester.runpytest(py_file)
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
'test_parametrized_collect_with_wrong_args.py::test_func: in "parametrize" the number of names (2):', 'test_parametrized_collect_with_wrong_args.py::test_func: in "parametrize" the number of names (2):',
@ -408,9 +421,9 @@ def test_parametrized_collect_with_wrong_args(testdir):
) )
def test_parametrized_with_kwargs(testdir): def test_parametrized_with_kwargs(pytester: Pytester) -> None:
"""Test collect parametrized func with wrong number of args.""" """Test collect parametrized func with wrong number of args."""
py_file = testdir.makepyfile( py_file = pytester.makepyfile(
""" """
import pytest import pytest
@ -424,13 +437,13 @@ def test_parametrized_with_kwargs(testdir):
""" """
) )
result = testdir.runpytest(py_file) result = pytester.runpytest(py_file)
assert result.ret == 0 assert result.ret == 0
def test_parametrize_iterator(testdir): def test_parametrize_iterator(pytester: Pytester) -> None:
"""`parametrize` should work with generators (#5354).""" """`parametrize` should work with generators (#5354)."""
py_file = testdir.makepyfile( py_file = pytester.makepyfile(
"""\ """\
import pytest import pytest
@ -444,16 +457,16 @@ def test_parametrize_iterator(testdir):
assert a >= 1 assert a >= 1
""" """
) )
result = testdir.runpytest(py_file) result = pytester.runpytest(py_file)
assert result.ret == 0 assert result.ret == 0
# should not skip any tests # should not skip any tests
result.stdout.fnmatch_lines(["*3 passed*"]) result.stdout.fnmatch_lines(["*3 passed*"])
class TestFunctional: class TestFunctional:
def test_merging_markers_deep(self, testdir): def test_merging_markers_deep(self, pytester: Pytester) -> None:
# issue 199 - propagate markers into nested classes # issue 199 - propagate markers into nested classes
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
import pytest import pytest
class TestA(object): class TestA(object):
@ -466,13 +479,15 @@ class TestFunctional:
assert True assert True
""" """
) )
items, rec = testdir.inline_genitems(p) items, rec = pytester.inline_genitems(p)
for item in items: for item in items:
print(item, item.keywords) print(item, item.keywords)
assert [x for x in item.iter_markers() if x.name == "a"] assert [x for x in item.iter_markers() if x.name == "a"]
def test_mark_decorator_subclass_does_not_propagate_to_base(self, testdir): def test_mark_decorator_subclass_does_not_propagate_to_base(
p = testdir.makepyfile( self, pytester: Pytester
) -> None:
p = pytester.makepyfile(
""" """
import pytest import pytest
@ -487,12 +502,12 @@ class TestFunctional:
def test_bar(self): pass def test_bar(self): pass
""" """
) )
items, rec = testdir.inline_genitems(p) items, rec = pytester.inline_genitems(p)
self.assert_markers(items, test_foo=("a", "b"), test_bar=("a",)) self.assert_markers(items, test_foo=("a", "b"), test_bar=("a",))
def test_mark_should_not_pass_to_siebling_class(self, testdir): def test_mark_should_not_pass_to_siebling_class(self, pytester: Pytester) -> None:
"""#568""" """#568"""
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
import pytest import pytest
@ -510,7 +525,7 @@ class TestFunctional:
""" """
) )
items, rec = testdir.inline_genitems(p) items, rec = pytester.inline_genitems(p)
base_item, sub_item, sub_item_other = items base_item, sub_item, sub_item_other = items
print(items, [x.nodeid for x in items]) print(items, [x.nodeid for x in items])
# new api segregates # new api segregates
@ -518,8 +533,8 @@ class TestFunctional:
assert not list(sub_item_other.iter_markers(name="b")) assert not list(sub_item_other.iter_markers(name="b"))
assert list(sub_item.iter_markers(name="b")) assert list(sub_item.iter_markers(name="b"))
def test_mark_decorator_baseclasses_merged(self, testdir): def test_mark_decorator_baseclasses_merged(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
import pytest import pytest
@ -538,11 +553,11 @@ class TestFunctional:
def test_bar(self): pass def test_bar(self): pass
""" """
) )
items, rec = testdir.inline_genitems(p) items, rec = pytester.inline_genitems(p)
self.assert_markers(items, test_foo=("a", "b", "c"), test_bar=("a", "b", "d")) self.assert_markers(items, test_foo=("a", "b", "c"), test_bar=("a", "b", "d"))
def test_mark_closest(self, testdir): def test_mark_closest(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
import pytest import pytest
@ -557,14 +572,18 @@ class TestFunctional:
""" """
) )
items, rec = testdir.inline_genitems(p) items, rec = pytester.inline_genitems(p)
has_own, has_inherited = items has_own, has_inherited = items
assert has_own.get_closest_marker("c").kwargs == {"location": "function"} has_own_marker = has_own.get_closest_marker("c")
assert has_inherited.get_closest_marker("c").kwargs == {"location": "class"} has_inherited_marker = has_inherited.get_closest_marker("c")
assert has_own_marker is not None
assert has_inherited_marker is not None
assert has_own_marker.kwargs == {"location": "function"}
assert has_inherited_marker.kwargs == {"location": "class"}
assert has_own.get_closest_marker("missing") is None assert has_own.get_closest_marker("missing") is None
def test_mark_with_wrong_marker(self, testdir): def test_mark_with_wrong_marker(self, pytester: Pytester) -> None:
reprec = testdir.inline_runsource( reprec = pytester.inline_runsource(
""" """
import pytest import pytest
class pytestmark(object): class pytestmark(object):
@ -577,8 +596,8 @@ class TestFunctional:
assert len(values) == 1 assert len(values) == 1
assert "TypeError" in str(values[0].longrepr) assert "TypeError" in str(values[0].longrepr)
def test_mark_dynamically_in_funcarg(self, testdir): def test_mark_dynamically_in_funcarg(self, pytester: Pytester) -> None:
testdir.makeconftest( pytester.makeconftest(
""" """
import pytest import pytest
@pytest.fixture @pytest.fixture
@ -589,17 +608,17 @@ class TestFunctional:
terminalreporter._tw.line("keyword: %s" % values[0].keywords) terminalreporter._tw.line("keyword: %s" % values[0].keywords)
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
def test_func(arg): def test_func(arg):
pass pass
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["keyword: *hello*"]) result.stdout.fnmatch_lines(["keyword: *hello*"])
def test_no_marker_match_on_unmarked_names(self, testdir): def test_no_marker_match_on_unmarked_names(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
import pytest import pytest
@pytest.mark.shouldmatch @pytest.mark.shouldmatch
@ -610,15 +629,15 @@ class TestFunctional:
assert 1 assert 1
""" """
) )
reprec = testdir.inline_run("-m", "test_unmarked", p) reprec = pytester.inline_run("-m", "test_unmarked", p)
passed, skipped, failed = reprec.listoutcomes() passed, skipped, failed = reprec.listoutcomes()
assert len(passed) + len(skipped) + len(failed) == 0 assert len(passed) + len(skipped) + len(failed) == 0
dlist = reprec.getcalls("pytest_deselected") dlist = reprec.getcalls("pytest_deselected")
deselected_tests = dlist[0].items deselected_tests = dlist[0].items
assert len(deselected_tests) == 2 assert len(deselected_tests) == 2
def test_keywords_at_node_level(self, testdir): def test_keywords_at_node_level(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@pytest.fixture(scope="session", autouse=True) @pytest.fixture(scope="session", autouse=True)
@ -636,11 +655,11 @@ class TestFunctional:
pass pass
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def test_keyword_added_for_session(self, testdir): def test_keyword_added_for_session(self, pytester: Pytester) -> None:
testdir.makeconftest( pytester.makeconftest(
""" """
import pytest import pytest
def pytest_collection_modifyitems(session): def pytest_collection_modifyitems(session):
@ -651,7 +670,7 @@ class TestFunctional:
session.add_marker(10)) session.add_marker(10))
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
def test_some(request): def test_some(request):
assert "mark1" in request.keywords assert "mark1" in request.keywords
@ -664,14 +683,14 @@ class TestFunctional:
assert marker.kwargs == {} assert marker.kwargs == {}
""" """
) )
reprec = testdir.inline_run("-m", "mark1") reprec = pytester.inline_run("-m", "mark1")
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def assert_markers(self, items, **expected): def assert_markers(self, items, **expected) -> None:
"""Assert that given items have expected marker names applied to them. """Assert that given items have expected marker names applied to them.
expected should be a dict of (item name -> seq of expected marker names). expected should be a dict of (item name -> seq of expected marker names).
Note: this could be moved to ``testdir`` if proven to be useful Note: this could be moved to ``pytester`` if proven to be useful
to other modules. to other modules.
""" """
items = {x.name: x for x in items} items = {x.name: x for x in items}
@ -680,9 +699,9 @@ class TestFunctional:
assert markers == set(expected_markers) assert markers == set(expected_markers)
@pytest.mark.filterwarnings("ignore") @pytest.mark.filterwarnings("ignore")
def test_mark_from_parameters(self, testdir): def test_mark_from_parameters(self, pytester: Pytester) -> None:
"""#1540""" """#1540"""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -701,12 +720,12 @@ class TestFunctional:
assert True assert True
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(skipped=1) reprec.assertoutcome(skipped=1)
def test_reevaluate_dynamic_expr(self, testdir): def test_reevaluate_dynamic_expr(self, pytester: Pytester) -> None:
"""#7360""" """#7360"""
py_file1 = testdir.makepyfile( py_file1 = pytester.makepyfile(
test_reevaluate_dynamic_expr1=""" test_reevaluate_dynamic_expr1="""
import pytest import pytest
@ -717,7 +736,7 @@ class TestFunctional:
assert True assert True
""" """
) )
py_file2 = testdir.makepyfile( py_file2 = pytester.makepyfile(
test_reevaluate_dynamic_expr2=""" test_reevaluate_dynamic_expr2="""
import pytest import pytest
@ -729,15 +748,15 @@ class TestFunctional:
""" """
) )
file_name1 = os.path.basename(py_file1.strpath) file_name1 = os.path.basename(py_file1)
file_name2 = os.path.basename(py_file2.strpath) file_name2 = os.path.basename(py_file2)
reprec = testdir.inline_run(file_name1, file_name2) reprec = pytester.inline_run(file_name1, file_name2)
reprec.assertoutcome(passed=1, skipped=1) reprec.assertoutcome(passed=1, skipped=1)
class TestKeywordSelection: class TestKeywordSelection:
def test_select_simple(self, testdir): def test_select_simple(self, pytester: Pytester) -> None:
file_test = testdir.makepyfile( file_test = pytester.makepyfile(
""" """
def test_one(): def test_one():
assert 0 assert 0
@ -748,7 +767,7 @@ class TestKeywordSelection:
) )
def check(keyword, name): def check(keyword, name):
reprec = testdir.inline_run("-s", "-k", keyword, file_test) reprec = pytester.inline_run("-s", "-k", keyword, file_test)
passed, skipped, failed = reprec.listoutcomes() passed, skipped, failed = reprec.listoutcomes()
assert len(failed) == 1 assert len(failed) == 1
assert failed[0].nodeid.split("::")[-1] == name assert failed[0].nodeid.split("::")[-1] == name
@ -769,8 +788,8 @@ class TestKeywordSelection:
"xxx and TestClass and test_2", "xxx and TestClass and test_2",
], ],
) )
def test_select_extra_keywords(self, testdir, keyword): def test_select_extra_keywords(self, pytester: Pytester, keyword) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
test_select=""" test_select="""
def test_1(): def test_1():
pass pass
@ -779,7 +798,7 @@ class TestKeywordSelection:
pass pass
""" """
) )
testdir.makepyfile( pytester.makepyfile(
conftest=""" conftest="""
import pytest import pytest
@pytest.hookimpl(hookwrapper=True) @pytest.hookimpl(hookwrapper=True)
@ -790,7 +809,7 @@ class TestKeywordSelection:
item.extra_keyword_matches.add("xxx") item.extra_keyword_matches.add("xxx")
""" """
) )
reprec = testdir.inline_run(p.dirpath(), "-s", "-k", keyword) reprec = pytester.inline_run(p.parent, "-s", "-k", keyword)
print("keyword", repr(keyword)) print("keyword", repr(keyword))
passed, skipped, failed = reprec.listoutcomes() passed, skipped, failed = reprec.listoutcomes()
assert len(passed) == 1 assert len(passed) == 1
@ -799,15 +818,15 @@ class TestKeywordSelection:
assert len(dlist) == 1 assert len(dlist) == 1
assert dlist[0].items[0].name == "test_1" assert dlist[0].items[0].name == "test_1"
def test_select_starton(self, testdir): def test_select_starton(self, pytester: Pytester) -> None:
threepass = testdir.makepyfile( threepass = pytester.makepyfile(
test_threepass=""" test_threepass="""
def test_one(): assert 1 def test_one(): assert 1
def test_two(): assert 1 def test_two(): assert 1
def test_three(): assert 1 def test_three(): assert 1
""" """
) )
reprec = testdir.inline_run("-k", "test_two:", threepass) reprec = pytester.inline_run("-k", "test_two:", threepass)
passed, skipped, failed = reprec.listoutcomes() passed, skipped, failed = reprec.listoutcomes()
assert len(passed) == 2 assert len(passed) == 2
assert not failed assert not failed
@ -816,21 +835,21 @@ class TestKeywordSelection:
item = dlist[0].items[0] item = dlist[0].items[0]
assert item.name == "test_one" assert item.name == "test_one"
def test_keyword_extra(self, testdir): def test_keyword_extra(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
def test_one(): def test_one():
assert 0 assert 0
test_one.mykeyword = True test_one.mykeyword = True
""" """
) )
reprec = testdir.inline_run("-k", "mykeyword", p) reprec = pytester.inline_run("-k", "mykeyword", p)
passed, skipped, failed = reprec.countoutcomes() passed, skipped, failed = reprec.countoutcomes()
assert failed == 1 assert failed == 1
@pytest.mark.xfail @pytest.mark.xfail
def test_keyword_extra_dash(self, testdir): def test_keyword_extra_dash(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
def test_one(): def test_one():
assert 0 assert 0
@ -839,42 +858,42 @@ class TestKeywordSelection:
) )
# with argparse the argument to an option cannot # with argparse the argument to an option cannot
# start with '-' # start with '-'
reprec = testdir.inline_run("-k", "-mykeyword", p) reprec = pytester.inline_run("-k", "-mykeyword", p)
passed, skipped, failed = reprec.countoutcomes() passed, skipped, failed = reprec.countoutcomes()
assert passed + skipped + failed == 0 assert passed + skipped + failed == 0
@pytest.mark.parametrize( @pytest.mark.parametrize(
"keyword", ["__", "+", ".."], "keyword", ["__", "+", ".."],
) )
def test_no_magic_values(self, testdir, keyword: str) -> None: def test_no_magic_values(self, pytester: Pytester, keyword: str) -> None:
"""Make sure the tests do not match on magic values, """Make sure the tests do not match on magic values,
no double underscored values, like '__dict__' and '+'. no double underscored values, like '__dict__' and '+'.
""" """
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
def test_one(): assert 1 def test_one(): assert 1
""" """
) )
reprec = testdir.inline_run("-k", keyword, p) reprec = pytester.inline_run("-k", keyword, p)
passed, skipped, failed = reprec.countoutcomes() passed, skipped, failed = reprec.countoutcomes()
dlist = reprec.getcalls("pytest_deselected") dlist = reprec.getcalls("pytest_deselected")
assert passed + skipped + failed == 0 assert passed + skipped + failed == 0
deselected_tests = dlist[0].items deselected_tests = dlist[0].items
assert len(deselected_tests) == 1 assert len(deselected_tests) == 1
def test_no_match_directories_outside_the_suite(self, testdir): def test_no_match_directories_outside_the_suite(self, pytester: Pytester) -> None:
"""`-k` should not match against directories containing the test suite (#7040).""" """`-k` should not match against directories containing the test suite (#7040)."""
test_contents = """ test_contents = """
def test_aaa(): pass def test_aaa(): pass
def test_ddd(): pass def test_ddd(): pass
""" """
testdir.makepyfile( pytester.makepyfile(
**{"ddd/tests/__init__.py": "", "ddd/tests/test_foo.py": test_contents} **{"ddd/tests/__init__.py": "", "ddd/tests/test_foo.py": test_contents}
) )
def get_collected_names(*args): def get_collected_names(*args):
_, rec = testdir.inline_genitems(*args) _, rec = pytester.inline_genitems(*args)
calls = rec.getcalls("pytest_collection_finish") calls = rec.getcalls("pytest_collection_finish")
assert len(calls) == 1 assert len(calls) == 1
return [x.name for x in calls[0].session.items] return [x.name for x in calls[0].session.items]
@ -883,7 +902,7 @@ class TestKeywordSelection:
assert get_collected_names() == ["test_aaa", "test_ddd"] assert get_collected_names() == ["test_aaa", "test_ddd"]
# do not collect anything based on names outside the collection tree # do not collect anything based on names outside the collection tree
assert get_collected_names("-k", testdir.tmpdir.basename) == [] assert get_collected_names("-k", pytester._name) == []
# "-k ddd" should only collect "test_ddd", but not # "-k ddd" should only collect "test_ddd", but not
# 'test_aaa' just because one of its parent directories is named "ddd"; # 'test_aaa' just because one of its parent directories is named "ddd";
@ -902,7 +921,7 @@ class TestMarkDecorator:
("foo", pytest.mark.bar(), False), ("foo", pytest.mark.bar(), False),
], ],
) )
def test__eq__(self, lhs, rhs, expected): def test__eq__(self, lhs, rhs, expected) -> None:
assert (lhs == rhs) == expected assert (lhs == rhs) == expected
def test_aliases(self) -> None: def test_aliases(self) -> None:
@ -913,9 +932,11 @@ class TestMarkDecorator:
@pytest.mark.parametrize("mark", [None, "", "skip", "xfail"]) @pytest.mark.parametrize("mark", [None, "", "skip", "xfail"])
def test_parameterset_for_parametrize_marks(testdir, mark): def test_parameterset_for_parametrize_marks(
pytester: Pytester, mark: Optional[str]
) -> None:
if mark is not None: if mark is not None:
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
{}={} {}={}
@ -924,7 +945,7 @@ def test_parameterset_for_parametrize_marks(testdir, mark):
) )
) )
config = testdir.parseconfig() config = pytester.parseconfig()
from _pytest.mark import pytest_configure, get_empty_parameterset_mark from _pytest.mark import pytest_configure, get_empty_parameterset_mark
pytest_configure(config) pytest_configure(config)
@ -938,8 +959,8 @@ def test_parameterset_for_parametrize_marks(testdir, mark):
assert result_mark.kwargs.get("run") is False assert result_mark.kwargs.get("run") is False
def test_parameterset_for_fail_at_collect(testdir): def test_parameterset_for_fail_at_collect(pytester: Pytester) -> None:
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
{}=fail_at_collect {}=fail_at_collect
@ -948,7 +969,7 @@ def test_parameterset_for_fail_at_collect(testdir):
) )
) )
config = testdir.parseconfig() config = pytester.parseconfig()
from _pytest.mark import pytest_configure, get_empty_parameterset_mark from _pytest.mark import pytest_configure, get_empty_parameterset_mark
pytest_configure(config) pytest_configure(config)
@ -959,7 +980,7 @@ def test_parameterset_for_fail_at_collect(testdir):
): ):
get_empty_parameterset_mark(config, ["a"], pytest_configure) get_empty_parameterset_mark(config, ["a"], pytest_configure)
p1 = testdir.makepyfile( p1 = pytester.makepyfile(
""" """
import pytest import pytest
@ -968,7 +989,7 @@ def test_parameterset_for_fail_at_collect(testdir):
pass pass
""" """
) )
result = testdir.runpytest(str(p1)) result = pytester.runpytest(str(p1))
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"collected 0 items / 1 error", "collected 0 items / 1 error",
@ -980,13 +1001,13 @@ def test_parameterset_for_fail_at_collect(testdir):
assert result.ret == ExitCode.INTERRUPTED assert result.ret == ExitCode.INTERRUPTED
def test_parameterset_for_parametrize_bad_markname(testdir): def test_parameterset_for_parametrize_bad_markname(pytester: Pytester) -> None:
with pytest.raises(pytest.UsageError): with pytest.raises(pytest.UsageError):
test_parameterset_for_parametrize_marks(testdir, "bad") test_parameterset_for_parametrize_marks(pytester, "bad")
def test_mark_expressions_no_smear(testdir): def test_mark_expressions_no_smear(pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1004,7 +1025,7 @@ def test_mark_expressions_no_smear(testdir):
""" """
) )
reprec = testdir.inline_run("-m", "FOO") reprec = pytester.inline_run("-m", "FOO")
passed, skipped, failed = reprec.countoutcomes() passed, skipped, failed = reprec.countoutcomes()
dlist = reprec.getcalls("pytest_deselected") dlist = reprec.getcalls("pytest_deselected")
assert passed == 1 assert passed == 1
@ -1014,13 +1035,13 @@ def test_mark_expressions_no_smear(testdir):
# todo: fixed # todo: fixed
# keywords smear - expected behaviour # keywords smear - expected behaviour
# reprec_keywords = testdir.inline_run("-k", "FOO") # reprec_keywords = pytester.inline_run("-k", "FOO")
# passed_k, skipped_k, failed_k = reprec_keywords.countoutcomes() # passed_k, skipped_k, failed_k = reprec_keywords.countoutcomes()
# assert passed_k == 2 # assert passed_k == 2
# assert skipped_k == failed_k == 0 # assert skipped_k == failed_k == 0
def test_addmarker_order(): def test_addmarker_order() -> None:
session = mock.Mock() session = mock.Mock()
session.own_markers = [] session.own_markers = []
session.parent = None session.parent = None
@ -1034,9 +1055,9 @@ def test_addmarker_order():
@pytest.mark.filterwarnings("ignore") @pytest.mark.filterwarnings("ignore")
def test_markers_from_parametrize(testdir): def test_markers_from_parametrize(pytester: Pytester) -> None:
"""#3605""" """#3605"""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1067,7 +1088,7 @@ def test_markers_from_parametrize(testdir):
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.assert_outcomes(passed=4) result.assert_outcomes(passed=4)
@ -1079,13 +1100,13 @@ def test_pytest_param_id_requires_string() -> None:
@pytest.mark.parametrize("s", (None, "hello world")) @pytest.mark.parametrize("s", (None, "hello world"))
def test_pytest_param_id_allows_none_or_string(s): def test_pytest_param_id_allows_none_or_string(s) -> None:
assert pytest.param(id=s) assert pytest.param(id=s)
@pytest.mark.parametrize("expr", ("NOT internal_err", "NOT (internal_err)", "bogus/")) @pytest.mark.parametrize("expr", ("NOT internal_err", "NOT (internal_err)", "bogus/"))
def test_marker_expr_eval_failure_handling(testdir, expr): def test_marker_expr_eval_failure_handling(pytester: Pytester, expr) -> None:
foo = testdir.makepyfile( foo = pytester.makepyfile(
""" """
import pytest import pytest
@ -1095,6 +1116,6 @@ def test_marker_expr_eval_failure_handling(testdir, expr):
""" """
) )
expected = f"ERROR: Wrong expression passed to '-m': {expr}: *" expected = f"ERROR: Wrong expression passed to '-m': {expr}: *"
result = testdir.runpytest(foo, "-m", expr) result = pytester.runpytest(foo, "-m", expr)
result.stderr.fnmatch_lines([expected]) result.stderr.fnmatch_lines([expected])
assert result.ret == ExitCode.USAGE_ERROR assert result.ret == ExitCode.USAGE_ERROR

View File

@ -1,18 +1,19 @@
import inspect import inspect
import _pytest.warning_types
import pytest import pytest
from _pytest import warning_types
from _pytest.pytester import Pytester
@pytest.mark.parametrize( @pytest.mark.parametrize(
"warning_class", "warning_class",
[ [
w w
for n, w in vars(_pytest.warning_types).items() for n, w in vars(warning_types).items()
if inspect.isclass(w) and issubclass(w, Warning) if inspect.isclass(w) and issubclass(w, Warning)
], ],
) )
def test_warning_types(warning_class): def test_warning_types(warning_class: UserWarning) -> None:
"""Make sure all warnings declared in _pytest.warning_types are displayed as coming """Make sure all warnings declared in _pytest.warning_types are displayed as coming
from 'pytest' instead of the internal module (#5452). from 'pytest' instead of the internal module (#5452).
""" """
@ -20,11 +21,11 @@ def test_warning_types(warning_class):
@pytest.mark.filterwarnings("error::pytest.PytestWarning") @pytest.mark.filterwarnings("error::pytest.PytestWarning")
def test_pytest_warnings_repr_integration_test(testdir): def test_pytest_warnings_repr_integration_test(pytester: Pytester) -> None:
"""Small integration test to ensure our small hack of setting the __module__ attribute """Small integration test to ensure our small hack of setting the __module__ attribute
of our warnings actually works (#5452). of our warnings actually works (#5452).
""" """
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
import warnings import warnings
@ -33,5 +34,5 @@ def test_pytest_warnings_repr_integration_test(testdir):
warnings.warn(pytest.PytestWarning("some warning")) warnings.warn(pytest.PytestWarning("some warning"))
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["E pytest.PytestWarning: some warning"]) result.stdout.fnmatch_lines(["E pytest.PytestWarning: some warning"])