Merge pull request #8111 from antonblr/testdir-to-pytester-python

tests: Migrate testing/python to pytester fixture
This commit is contained in:
Ran Benita 2020-12-13 23:19:31 +02:00 committed by GitHub
commit ad65e816e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1250 additions and 1131 deletions

View File

@ -1190,7 +1190,9 @@ class Pytester:
config._do_configure() config._do_configure()
return config return config
def getitem(self, source: str, funcname: str = "test_func") -> Item: def getitem(
self, source: Union[str, "os.PathLike[str]"], funcname: str = "test_func"
) -> Item:
"""Return the test item for a test function. """Return the test item for a test function.
Writes the source to a python file and runs pytest's collection on Writes the source to a python file and runs pytest's collection on
@ -1210,7 +1212,7 @@ class Pytester:
funcname, source, items funcname, source, items
) )
def getitems(self, source: str) -> List[Item]: def getitems(self, source: Union[str, "os.PathLike[str]"]) -> List[Item]:
"""Return all test items collected from the module. """Return all test items collected from the module.
Writes the source to a Python file and runs pytest's collection on Writes the source to a Python file and runs pytest's collection on
@ -1220,7 +1222,11 @@ class Pytester:
return self.genitems([modcol]) return self.genitems([modcol])
def getmodulecol( def getmodulecol(
self, source: Union[str, Path], configargs=(), *, withinit: bool = False self,
source: Union[str, "os.PathLike[str]"],
configargs=(),
*,
withinit: bool = False,
): ):
"""Return the module collection node for ``source``. """Return the module collection node for ``source``.
@ -1238,7 +1244,9 @@ class Pytester:
Whether to also write an ``__init__.py`` file to the same Whether to also write an ``__init__.py`` file to the same
directory to ensure it is a package. directory to ensure it is a package.
""" """
if isinstance(source, Path): # TODO: Remove type ignore in next mypy release (> 0.790).
# https://github.com/python/typeshed/pull/4582
if isinstance(source, os.PathLike): # type: ignore[misc]
path = self.path.joinpath(source) path = self.path.joinpath(source)
assert not withinit, "not supported for paths" assert not withinit, "not supported for paths"
else: else:

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -3,13 +3,14 @@ from typing import Any
import pytest import pytest
from _pytest import runner from _pytest import runner
from _pytest._code import getfslineno from _pytest._code import getfslineno
from _pytest.pytester import Pytester
class TestOEJSKITSpecials: class TestOEJSKITSpecials:
def test_funcarg_non_pycollectobj( def test_funcarg_non_pycollectobj(
self, testdir, recwarn self, pytester: Pytester, recwarn
) -> None: # rough jstests usage ) -> None: # rough jstests usage
testdir.makeconftest( pytester.makeconftest(
""" """
import pytest import pytest
def pytest_pycollect_makeitem(collector, name, obj): def pytest_pycollect_makeitem(collector, name, obj):
@ -20,7 +21,7 @@ class TestOEJSKITSpecials:
return self.fspath, 3, "xyz" return self.fspath, 3, "xyz"
""" """
) )
modcol = testdir.getmodulecol( modcol = pytester.getmodulecol(
""" """
import pytest import pytest
@pytest.fixture @pytest.fixture
@ -39,8 +40,10 @@ class TestOEJSKITSpecials:
pytest._fillfuncargs(clscol) pytest._fillfuncargs(clscol)
assert clscol.funcargs["arg1"] == 42 assert clscol.funcargs["arg1"] == 42
def test_autouse_fixture(self, testdir, recwarn) -> None: # rough jstests usage def test_autouse_fixture(
testdir.makeconftest( self, pytester: Pytester, recwarn
) -> None: # rough jstests usage
pytester.makeconftest(
""" """
import pytest import pytest
def pytest_pycollect_makeitem(collector, name, obj): def pytest_pycollect_makeitem(collector, name, obj):
@ -51,7 +54,7 @@ class TestOEJSKITSpecials:
return self.fspath, 3, "xyz" return self.fspath, 3, "xyz"
""" """
) )
modcol = testdir.getmodulecol( modcol = pytester.getmodulecol(
""" """
import pytest import pytest
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
@ -125,8 +128,8 @@ class TestMockDecoration:
values = getfuncargnames(f) values = getfuncargnames(f)
assert values == ("y", "z") assert values == ("y", "z")
def test_unittest_mock(self, testdir): def test_unittest_mock(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import unittest.mock import unittest.mock
class T(unittest.TestCase): class T(unittest.TestCase):
@ -137,11 +140,11 @@ class TestMockDecoration:
abspath.assert_any_call("hello") abspath.assert_any_call("hello")
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def test_unittest_mock_and_fixture(self, testdir): def test_unittest_mock_and_fixture(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import os.path import os.path
import unittest.mock import unittest.mock
@ -158,12 +161,12 @@ class TestMockDecoration:
os.path.abspath("hello") os.path.abspath("hello")
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def test_unittest_mock_and_pypi_mock(self, testdir): def test_unittest_mock_and_pypi_mock(self, pytester: Pytester) -> None:
pytest.importorskip("mock", "1.0.1") pytest.importorskip("mock", "1.0.1")
testdir.makepyfile( pytester.makepyfile(
""" """
import mock import mock
import unittest.mock import unittest.mock
@ -181,15 +184,15 @@ class TestMockDecoration:
abspath.assert_any_call("hello") abspath.assert_any_call("hello")
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=2) reprec.assertoutcome(passed=2)
def test_mock_sentinel_check_against_numpy_like(self, testdir): def test_mock_sentinel_check_against_numpy_like(self, pytester: Pytester) -> None:
"""Ensure our function that detects mock arguments compares against sentinels using """Ensure our function that detects mock arguments compares against sentinels using
identity to circumvent objects which can't be compared with equality against others identity to circumvent objects which can't be compared with equality against others
in a truth context, like with numpy arrays (#5606). in a truth context, like with numpy arrays (#5606).
""" """
testdir.makepyfile( pytester.makepyfile(
dummy=""" dummy="""
class NumpyLike: class NumpyLike:
def __init__(self, value): def __init__(self, value):
@ -199,7 +202,7 @@ class TestMockDecoration:
FOO = NumpyLike(10) FOO = NumpyLike(10)
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
from unittest.mock import patch from unittest.mock import patch
import dummy import dummy
@ -209,12 +212,12 @@ class TestMockDecoration:
assert dummy.FOO.value == 50 assert dummy.FOO.value == 50
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def test_mock(self, testdir): def test_mock(self, pytester: Pytester) -> None:
pytest.importorskip("mock", "1.0.1") pytest.importorskip("mock", "1.0.1")
testdir.makepyfile( pytester.makepyfile(
""" """
import os import os
import unittest import unittest
@ -237,7 +240,7 @@ class TestMockDecoration:
assert os.path.basename("123") == "mock_basename" assert os.path.basename("123") == "mock_basename"
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=2) reprec.assertoutcome(passed=2)
calls = reprec.getcalls("pytest_runtest_logreport") calls = reprec.getcalls("pytest_runtest_logreport")
funcnames = [ funcnames = [
@ -245,9 +248,9 @@ class TestMockDecoration:
] ]
assert funcnames == ["T.test_hello", "test_someting"] assert funcnames == ["T.test_hello", "test_someting"]
def test_mock_sorting(self, testdir): def test_mock_sorting(self, pytester: Pytester) -> None:
pytest.importorskip("mock", "1.0.1") pytest.importorskip("mock", "1.0.1")
testdir.makepyfile( pytester.makepyfile(
""" """
import os import os
import mock import mock
@ -263,15 +266,15 @@ class TestMockDecoration:
pass pass
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
calls = reprec.getreports("pytest_runtest_logreport") calls = reprec.getreports("pytest_runtest_logreport")
calls = [x for x in calls if x.when == "call"] calls = [x for x in calls if x.when == "call"]
names = [x.nodeid.split("::")[-1] for x in calls] names = [x.nodeid.split("::")[-1] for x in calls]
assert names == ["test_one", "test_two", "test_three"] assert names == ["test_one", "test_two", "test_three"]
def test_mock_double_patch_issue473(self, testdir): def test_mock_double_patch_issue473(self, pytester: Pytester) -> None:
pytest.importorskip("mock", "1.0.1") pytest.importorskip("mock", "1.0.1")
testdir.makepyfile( pytester.makepyfile(
""" """
from mock import patch from mock import patch
from pytest import mark from pytest import mark
@ -284,13 +287,13 @@ class TestMockDecoration:
pass pass
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
class TestReRunTests: class TestReRunTests:
def test_rerun(self, testdir): def test_rerun(self, pytester: Pytester) -> None:
testdir.makeconftest( pytester.makeconftest(
""" """
from _pytest.runner import runtestprotocol from _pytest.runner import runtestprotocol
def pytest_runtest_protocol(item, nextitem): def pytest_runtest_protocol(item, nextitem):
@ -298,7 +301,7 @@ class TestReRunTests:
runtestprotocol(item, log=True, nextitem=nextitem) runtestprotocol(item, log=True, nextitem=nextitem)
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
count = 0 count = 0
@ -314,7 +317,7 @@ class TestReRunTests:
pass pass
""" """
) )
result = testdir.runpytest("-s") result = pytester.runpytest("-s")
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
""" """
*fix count 0* *fix count 0*
@ -336,21 +339,21 @@ def test_pytestconfig_is_session_scoped() -> None:
class TestNoselikeTestAttribute: class TestNoselikeTestAttribute:
def test_module_with_global_test(self, testdir): def test_module_with_global_test(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
__test__ = False __test__ = False
def test_hello(): def test_hello():
pass pass
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
assert not reprec.getfailedcollections() assert not reprec.getfailedcollections()
calls = reprec.getreports("pytest_runtest_logreport") calls = reprec.getreports("pytest_runtest_logreport")
assert not calls assert not calls
def test_class_and_method(self, testdir): def test_class_and_method(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
__test__ = True __test__ = True
def test_func(): def test_func():
@ -363,13 +366,13 @@ class TestNoselikeTestAttribute:
pass pass
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
assert not reprec.getfailedcollections() assert not reprec.getfailedcollections()
calls = reprec.getreports("pytest_runtest_logreport") calls = reprec.getreports("pytest_runtest_logreport")
assert not calls assert not calls
def test_unittest_class(self, testdir): def test_unittest_class(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import unittest import unittest
class TC(unittest.TestCase): class TC(unittest.TestCase):
@ -381,20 +384,20 @@ class TestNoselikeTestAttribute:
pass pass
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
assert not reprec.getfailedcollections() assert not reprec.getfailedcollections()
call = reprec.getcalls("pytest_collection_modifyitems")[0] call = reprec.getcalls("pytest_collection_modifyitems")[0]
assert len(call.items) == 1 assert len(call.items) == 1
assert call.items[0].cls.__name__ == "TC" assert call.items[0].cls.__name__ == "TC"
def test_class_with_nasty_getattr(self, testdir): def test_class_with_nasty_getattr(self, pytester: Pytester) -> None:
"""Make sure we handle classes with a custom nasty __getattr__ right. """Make sure we handle classes with a custom nasty __getattr__ right.
With a custom __getattr__ which e.g. returns a function (like with a With a custom __getattr__ which e.g. returns a function (like with a
RPC wrapper), we shouldn't assume this meant "__test__ = True". RPC wrapper), we shouldn't assume this meant "__test__ = True".
""" """
# https://github.com/pytest-dev/pytest/issues/1204 # https://github.com/pytest-dev/pytest/issues/1204
testdir.makepyfile( pytester.makepyfile(
""" """
class MetaModel(type): class MetaModel(type):
@ -413,7 +416,7 @@ class TestNoselikeTestAttribute:
pass pass
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
assert not reprec.getfailedcollections() assert not reprec.getfailedcollections()
call = reprec.getcalls("pytest_collection_modifyitems")[0] call = reprec.getcalls("pytest_collection_modifyitems")[0]
assert not call.items assert not call.items
@ -422,8 +425,8 @@ class TestNoselikeTestAttribute:
class TestParameterize: class TestParameterize:
"""#351""" """#351"""
def test_idfn_marker(self, testdir): def test_idfn_marker(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -440,11 +443,11 @@ class TestParameterize:
pass pass
""" """
) )
res = testdir.runpytest("--collect-only") res = pytester.runpytest("--collect-only")
res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"]) res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"])
def test_idfn_fixture(self, testdir): def test_idfn_fixture(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -468,5 +471,5 @@ class TestParameterize:
pass pass
""" """
) )
res = testdir.runpytest("--collect-only") res = pytester.runpytest("--collect-only")
res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"]) res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"])

View File

@ -23,7 +23,7 @@ from _pytest.compat import _format_args
from _pytest.compat import getfuncargnames from _pytest.compat import getfuncargnames
from _pytest.compat import NOTSET from _pytest.compat import NOTSET
from _pytest.outcomes import fail from _pytest.outcomes import fail
from _pytest.pytester import Testdir from _pytest.pytester import Pytester
from _pytest.python import _idval from _pytest.python import _idval
from _pytest.python import idmaker from _pytest.python import idmaker
@ -123,7 +123,7 @@ class TestMetafunc:
): ):
metafunc.parametrize("x", [1], scope="doggy") # type: ignore[arg-type] metafunc.parametrize("x", [1], scope="doggy") # type: ignore[arg-type]
def test_parametrize_request_name(self, testdir: Testdir) -> None: def test_parametrize_request_name(self, pytester: Pytester) -> None:
"""Show proper error when 'request' is used as a parameter name in parametrize (#6183)""" """Show proper error when 'request' is used as a parameter name in parametrize (#6183)"""
def func(request): def func(request):
@ -550,12 +550,12 @@ class TestMetafunc:
) )
assert result == [expected] assert result == [expected]
def test_parametrize_ids_exception(self, testdir: Testdir) -> None: def test_parametrize_ids_exception(self, pytester: Pytester) -> None:
""" """
:param testdir: the instance of Testdir class, a temporary :param pytester: the instance of Pytester class, a temporary
test directory. test directory.
""" """
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -567,7 +567,7 @@ class TestMetafunc:
pass pass
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*Exception: bad ids", "*Exception: bad ids",
@ -575,8 +575,8 @@ class TestMetafunc:
] ]
) )
def test_parametrize_ids_returns_non_string(self, testdir: Testdir) -> None: def test_parametrize_ids_returns_non_string(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
"""\ """\
import pytest import pytest
@ -592,7 +592,7 @@ class TestMetafunc:
assert arg assert arg
""" """
) )
result = testdir.runpytest("-vv", "-s") result = pytester.runpytest("-vv", "-s")
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"test_parametrize_ids_returns_non_string.py::test[arg0] PASSED", "test_parametrize_ids_returns_non_string.py::test[arg0] PASSED",
@ -682,7 +682,7 @@ class TestMetafunc:
): ):
metafunc.parametrize("x, y", [("a", "b")], indirect={}) # type: ignore[arg-type] metafunc.parametrize("x, y", [("a", "b")], indirect={}) # type: ignore[arg-type]
def test_parametrize_indirect_list_functional(self, testdir: Testdir) -> None: def test_parametrize_indirect_list_functional(self, pytester: Pytester) -> None:
""" """
#714 #714
Test parametrization with 'indirect' parameter applied on Test parametrization with 'indirect' parameter applied on
@ -690,10 +690,10 @@ class TestMetafunc:
be used directly rather than being passed to the fixture be used directly rather than being passed to the fixture
y. y.
:param testdir: the instance of Testdir class, a temporary :param pytester: the instance of Pytester class, a temporary
test directory. test directory.
""" """
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
@ -708,7 +708,7 @@ class TestMetafunc:
assert len(y) == 1 assert len(y) == 1
""" """
) )
result = testdir.runpytest("-v") result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(["*test_simple*a-b*", "*1 passed*"]) result.stdout.fnmatch_lines(["*test_simple*a-b*", "*1 passed*"])
def test_parametrize_indirect_list_error(self) -> None: def test_parametrize_indirect_list_error(self) -> None:
@ -722,7 +722,7 @@ class TestMetafunc:
metafunc.parametrize("x, y", [("a", "b")], indirect=["x", "z"]) metafunc.parametrize("x, y", [("a", "b")], indirect=["x", "z"])
def test_parametrize_uses_no_fixture_error_indirect_false( def test_parametrize_uses_no_fixture_error_indirect_false(
self, testdir: Testdir self, pytester: Pytester
) -> None: ) -> None:
"""The 'uses no fixture' error tells the user at collection time """The 'uses no fixture' error tells the user at collection time
that the parametrize data they've set up doesn't correspond to the that the parametrize data they've set up doesn't correspond to the
@ -731,7 +731,7 @@ class TestMetafunc:
#714 #714
""" """
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -740,14 +740,14 @@ class TestMetafunc:
assert len(x) == 3 assert len(x) == 3
""" """
) )
result = testdir.runpytest("--collect-only") result = pytester.runpytest("--collect-only")
result.stdout.fnmatch_lines(["*uses no argument 'y'*"]) result.stdout.fnmatch_lines(["*uses no argument 'y'*"])
def test_parametrize_uses_no_fixture_error_indirect_true( def test_parametrize_uses_no_fixture_error_indirect_true(
self, testdir: Testdir self, pytester: Pytester
) -> None: ) -> None:
"""#714""" """#714"""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
@ -762,14 +762,14 @@ class TestMetafunc:
assert len(x) == 3 assert len(x) == 3
""" """
) )
result = testdir.runpytest("--collect-only") result = pytester.runpytest("--collect-only")
result.stdout.fnmatch_lines(["*uses no fixture 'y'*"]) result.stdout.fnmatch_lines(["*uses no fixture 'y'*"])
def test_parametrize_indirect_uses_no_fixture_error_indirect_string( def test_parametrize_indirect_uses_no_fixture_error_indirect_string(
self, testdir: Testdir self, pytester: Pytester
) -> None: ) -> None:
"""#714""" """#714"""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
@ -781,14 +781,14 @@ class TestMetafunc:
assert len(x) == 3 assert len(x) == 3
""" """
) )
result = testdir.runpytest("--collect-only") result = pytester.runpytest("--collect-only")
result.stdout.fnmatch_lines(["*uses no fixture 'y'*"]) result.stdout.fnmatch_lines(["*uses no fixture 'y'*"])
def test_parametrize_indirect_uses_no_fixture_error_indirect_list( def test_parametrize_indirect_uses_no_fixture_error_indirect_list(
self, testdir: Testdir self, pytester: Pytester
) -> None: ) -> None:
"""#714""" """#714"""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
@ -800,12 +800,14 @@ class TestMetafunc:
assert len(x) == 3 assert len(x) == 3
""" """
) )
result = testdir.runpytest("--collect-only") result = pytester.runpytest("--collect-only")
result.stdout.fnmatch_lines(["*uses no fixture 'y'*"]) result.stdout.fnmatch_lines(["*uses no fixture 'y'*"])
def test_parametrize_argument_not_in_indirect_list(self, testdir: Testdir) -> None: def test_parametrize_argument_not_in_indirect_list(
self, pytester: Pytester
) -> None:
"""#714""" """#714"""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
@ -817,13 +819,13 @@ class TestMetafunc:
assert len(x) == 3 assert len(x) == 3
""" """
) )
result = testdir.runpytest("--collect-only") result = pytester.runpytest("--collect-only")
result.stdout.fnmatch_lines(["*uses no argument 'y'*"]) result.stdout.fnmatch_lines(["*uses no argument 'y'*"])
def test_parametrize_gives_indicative_error_on_function_with_default_argument( def test_parametrize_gives_indicative_error_on_function_with_default_argument(
self, testdir self, pytester: Pytester
) -> None: ) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -832,13 +834,13 @@ class TestMetafunc:
assert len(x) == 1 assert len(x) == 1
""" """
) )
result = testdir.runpytest("--collect-only") result = pytester.runpytest("--collect-only")
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
["*already takes an argument 'y' with a default value"] ["*already takes an argument 'y' with a default value"]
) )
def test_parametrize_functional(self, testdir: Testdir) -> None: def test_parametrize_functional(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
@ -853,7 +855,7 @@ class TestMetafunc:
assert y == 2 assert y == 2
""" """
) )
result = testdir.runpytest("-v") result = pytester.runpytest("-v")
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
["*test_simple*1-2*", "*test_simple*2-2*", "*2 passed*"] ["*test_simple*1-2*", "*test_simple*2-2*", "*2 passed*"]
) )
@ -884,8 +886,8 @@ class TestMetafunc:
assert metafunc._calls[1].funcargs == dict(x=3, y=4) assert metafunc._calls[1].funcargs == dict(x=3, y=4)
assert metafunc._calls[1].id == "3-4" assert metafunc._calls[1].id == "3-4"
def test_parametrize_multiple_times(self, testdir: Testdir) -> None: def test_parametrize_multiple_times(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
pytestmark = pytest.mark.parametrize("x", [1,2]) pytestmark = pytest.mark.parametrize("x", [1,2])
@ -897,12 +899,12 @@ class TestMetafunc:
assert 0, x assert 0, x
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
assert result.ret == 1 assert result.ret == 1
result.assert_outcomes(failed=6) result.assert_outcomes(failed=6)
def test_parametrize_CSV(self, testdir: Testdir) -> None: def test_parametrize_CSV(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@pytest.mark.parametrize("x, y,", [(1,2), (2,3)]) @pytest.mark.parametrize("x, y,", [(1,2), (2,3)])
@ -910,11 +912,11 @@ class TestMetafunc:
assert x+1 == y assert x+1 == y
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=2) reprec.assertoutcome(passed=2)
def test_parametrize_class_scenarios(self, testdir: Testdir) -> None: def test_parametrize_class_scenarios(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
# same as doc/en/example/parametrize scenario example # same as doc/en/example/parametrize scenario example
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
@ -941,7 +943,7 @@ class TestMetafunc:
pass pass
""" """
) )
result = testdir.runpytest("-v") result = pytester.runpytest("-v")
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
""" """
@ -978,8 +980,8 @@ class TestMetafunc:
class TestMetafuncFunctional: class TestMetafuncFunctional:
def test_attributes(self, testdir: Testdir) -> None: def test_attributes(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
# assumes that generate/provide runs in the same process # assumes that generate/provide runs in the same process
import sys, pytest import sys, pytest
@ -1005,11 +1007,11 @@ class TestMetafuncFunctional:
assert metafunc.cls == TestClass assert metafunc.cls == TestClass
""" """
) )
result = testdir.runpytest(p, "-v") result = pytester.runpytest(p, "-v")
result.assert_outcomes(passed=2) result.assert_outcomes(passed=2)
def test_two_functions(self, testdir: Testdir) -> None: def test_two_functions(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
metafunc.parametrize('arg1', [10, 20], ids=['0', '1']) metafunc.parametrize('arg1', [10, 20], ids=['0', '1'])
@ -1021,7 +1023,7 @@ class TestMetafuncFunctional:
assert arg1 in (10, 20) assert arg1 in (10, 20)
""" """
) )
result = testdir.runpytest("-v", p) result = pytester.runpytest("-v", p)
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"*test_func1*0*PASS*", "*test_func1*0*PASS*",
@ -1032,8 +1034,8 @@ class TestMetafuncFunctional:
] ]
) )
def test_noself_in_method(self, testdir: Testdir) -> None: def test_noself_in_method(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
assert 'xyz' not in metafunc.fixturenames assert 'xyz' not in metafunc.fixturenames
@ -1043,11 +1045,11 @@ class TestMetafuncFunctional:
pass pass
""" """
) )
result = testdir.runpytest(p) result = pytester.runpytest(p)
result.assert_outcomes(passed=1) result.assert_outcomes(passed=1)
def test_generate_tests_in_class(self, testdir: Testdir) -> None: def test_generate_tests_in_class(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
class TestClass(object): class TestClass(object):
def pytest_generate_tests(self, metafunc): def pytest_generate_tests(self, metafunc):
@ -1057,11 +1059,11 @@ class TestMetafuncFunctional:
assert hello == "world" assert hello == "world"
""" """
) )
result = testdir.runpytest("-v", p) result = pytester.runpytest("-v", p)
result.stdout.fnmatch_lines(["*test_myfunc*hello*PASS*", "*1 passed*"]) result.stdout.fnmatch_lines(["*test_myfunc*hello*PASS*", "*1 passed*"])
def test_two_functions_not_same_instance(self, testdir: Testdir) -> None: def test_two_functions_not_same_instance(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
metafunc.parametrize('arg1', [10, 20], ids=["0", "1"]) metafunc.parametrize('arg1', [10, 20], ids=["0", "1"])
@ -1072,13 +1074,13 @@ class TestMetafuncFunctional:
self.x = 1 self.x = 1
""" """
) )
result = testdir.runpytest("-v", p) result = pytester.runpytest("-v", p)
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
["*test_func*0*PASS*", "*test_func*1*PASS*", "*2 pass*"] ["*test_func*0*PASS*", "*test_func*1*PASS*", "*2 pass*"]
) )
def test_issue28_setup_method_in_generate_tests(self, testdir: Testdir) -> None: def test_issue28_setup_method_in_generate_tests(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
metafunc.parametrize('arg1', [1]) metafunc.parametrize('arg1', [1])
@ -1090,11 +1092,11 @@ class TestMetafuncFunctional:
self.val = 1 self.val = 1
""" """
) )
result = testdir.runpytest(p) result = pytester.runpytest(p)
result.assert_outcomes(passed=1) result.assert_outcomes(passed=1)
def test_parametrize_functional2(self, testdir: Testdir) -> None: def test_parametrize_functional2(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
metafunc.parametrize("arg1", [1,2]) metafunc.parametrize("arg1", [1,2])
@ -1103,13 +1105,13 @@ class TestMetafuncFunctional:
assert 0, (arg1, arg2) assert 0, (arg1, arg2)
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
["*(1, 4)*", "*(1, 5)*", "*(2, 4)*", "*(2, 5)*", "*4 failed*"] ["*(1, 4)*", "*(1, 5)*", "*(2, 4)*", "*(2, 5)*", "*4 failed*"]
) )
def test_parametrize_and_inner_getfixturevalue(self, testdir: Testdir) -> None: def test_parametrize_and_inner_getfixturevalue(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
metafunc.parametrize("arg1", [1], indirect=True) metafunc.parametrize("arg1", [1], indirect=True)
@ -1129,11 +1131,11 @@ class TestMetafuncFunctional:
assert arg1 == 11 assert arg1 == 11
""" """
) )
result = testdir.runpytest("-v", p) result = pytester.runpytest("-v", p)
result.stdout.fnmatch_lines(["*test_func1*1*PASS*", "*1 passed*"]) result.stdout.fnmatch_lines(["*test_func1*1*PASS*", "*1 passed*"])
def test_parametrize_on_setup_arg(self, testdir: Testdir) -> None: def test_parametrize_on_setup_arg(self, pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
assert "arg1" in metafunc.fixturenames assert "arg1" in metafunc.fixturenames
@ -1152,17 +1154,17 @@ class TestMetafuncFunctional:
assert arg2 == 10 assert arg2 == 10
""" """
) )
result = testdir.runpytest("-v", p) result = pytester.runpytest("-v", p)
result.stdout.fnmatch_lines(["*test_func*1*PASS*", "*1 passed*"]) result.stdout.fnmatch_lines(["*test_func*1*PASS*", "*1 passed*"])
def test_parametrize_with_ids(self, testdir: Testdir) -> None: def test_parametrize_with_ids(self, pytester: Pytester) -> None:
testdir.makeini( pytester.makeini(
""" """
[pytest] [pytest]
console_output_style=classic console_output_style=classic
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
@ -1173,14 +1175,14 @@ class TestMetafuncFunctional:
assert a == b assert a == b
""" """
) )
result = testdir.runpytest("-v") result = pytester.runpytest("-v")
assert result.ret == 1 assert result.ret == 1
result.stdout.fnmatch_lines_random( result.stdout.fnmatch_lines_random(
["*test_function*basic*PASSED", "*test_function*advanced*FAILED"] ["*test_function*basic*PASSED", "*test_function*advanced*FAILED"]
) )
def test_parametrize_without_ids(self, testdir: Testdir) -> None: def test_parametrize_without_ids(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
@ -1191,7 +1193,7 @@ class TestMetafuncFunctional:
assert 1 assert 1
""" """
) )
result = testdir.runpytest("-v") result = pytester.runpytest("-v")
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
""" """
*test_function*1-b0* *test_function*1-b0*
@ -1199,8 +1201,8 @@ class TestMetafuncFunctional:
""" """
) )
def test_parametrize_with_None_in_ids(self, testdir: Testdir) -> None: def test_parametrize_with_None_in_ids(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
@ -1211,7 +1213,7 @@ class TestMetafuncFunctional:
assert a == b assert a == b
""" """
) )
result = testdir.runpytest("-v") result = pytester.runpytest("-v")
assert result.ret == 1 assert result.ret == 1
result.stdout.fnmatch_lines_random( result.stdout.fnmatch_lines_random(
[ [
@ -1221,9 +1223,9 @@ class TestMetafuncFunctional:
] ]
) )
def test_fixture_parametrized_empty_ids(self, testdir: Testdir) -> None: def test_fixture_parametrized_empty_ids(self, pytester: Pytester) -> None:
"""Fixtures parametrized with empty ids cause an internal error (#1849).""" """Fixtures parametrized with empty ids cause an internal error (#1849)."""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1235,12 +1237,12 @@ class TestMetafuncFunctional:
pass pass
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 1 skipped *"]) result.stdout.fnmatch_lines(["* 1 skipped *"])
def test_parametrized_empty_ids(self, testdir: Testdir) -> None: def test_parametrized_empty_ids(self, pytester: Pytester) -> None:
"""Tests parametrized with empty ids cause an internal error (#1849).""" """Tests parametrized with empty ids cause an internal error (#1849)."""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1249,12 +1251,12 @@ class TestMetafuncFunctional:
pass pass
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 1 skipped *"]) result.stdout.fnmatch_lines(["* 1 skipped *"])
def test_parametrized_ids_invalid_type(self, testdir: Testdir) -> None: def test_parametrized_ids_invalid_type(self, pytester: Pytester) -> None:
"""Test error with non-strings/non-ints, without generator (#1857).""" """Test error with non-strings/non-ints, without generator (#1857)."""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1263,7 +1265,7 @@ class TestMetafuncFunctional:
assert x * 2 == expected assert x * 2 == expected
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"In test_ids_numbers: ids must be list of string/float/int/bool," "In test_ids_numbers: ids must be list of string/float/int/bool,"
@ -1272,9 +1274,9 @@ class TestMetafuncFunctional:
) )
def test_parametrize_with_identical_ids_get_unique_names( def test_parametrize_with_identical_ids_get_unique_names(
self, testdir: Testdir self, pytester: Pytester
) -> None: ) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
@ -1285,7 +1287,7 @@ class TestMetafuncFunctional:
assert a == b assert a == b
""" """
) )
result = testdir.runpytest("-v") result = pytester.runpytest("-v")
assert result.ret == 1 assert result.ret == 1
result.stdout.fnmatch_lines_random( result.stdout.fnmatch_lines_random(
["*test_function*a0*PASSED*", "*test_function*a1*FAILED*"] ["*test_function*a0*PASSED*", "*test_function*a1*FAILED*"]
@ -1293,9 +1295,9 @@ class TestMetafuncFunctional:
@pytest.mark.parametrize(("scope", "length"), [("module", 2), ("function", 4)]) @pytest.mark.parametrize(("scope", "length"), [("module", 2), ("function", 4)])
def test_parametrize_scope_overrides( def test_parametrize_scope_overrides(
self, testdir: Testdir, scope: str, length: int self, pytester: Pytester, scope: str, length: int
) -> None: ) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
values = [] values = []
@ -1316,11 +1318,11 @@ class TestMetafuncFunctional:
""" """
% (scope, length) % (scope, length)
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=5) reprec.assertoutcome(passed=5)
def test_parametrize_issue323(self, testdir: Testdir) -> None: def test_parametrize_issue323(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1334,11 +1336,11 @@ class TestMetafuncFunctional:
pass pass
""" """
) )
reprec = testdir.inline_run("--collect-only") reprec = pytester.inline_run("--collect-only")
assert not reprec.getcalls("pytest_internalerror") assert not reprec.getcalls("pytest_internalerror")
def test_usefixtures_seen_in_generate_tests(self, testdir: Testdir) -> None: def test_usefixtures_seen_in_generate_tests(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
@ -1350,13 +1352,13 @@ class TestMetafuncFunctional:
pass pass
""" """
) )
reprec = testdir.runpytest() reprec = pytester.runpytest()
reprec.assert_outcomes(passed=1) reprec.assert_outcomes(passed=1)
def test_generate_tests_only_done_in_subdir(self, testdir: Testdir) -> None: def test_generate_tests_only_done_in_subdir(self, pytester: Pytester) -> None:
sub1 = testdir.mkpydir("sub1") sub1 = pytester.mkpydir("sub1")
sub2 = testdir.mkpydir("sub2") sub2 = pytester.mkpydir("sub2")
sub1.join("conftest.py").write( sub1.joinpath("conftest.py").write_text(
textwrap.dedent( textwrap.dedent(
"""\ """\
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
@ -1364,7 +1366,7 @@ class TestMetafuncFunctional:
""" """
) )
) )
sub2.join("conftest.py").write( sub2.joinpath("conftest.py").write_text(
textwrap.dedent( textwrap.dedent(
"""\ """\
def pytest_generate_tests(metafunc): def pytest_generate_tests(metafunc):
@ -1372,13 +1374,13 @@ class TestMetafuncFunctional:
""" """
) )
) )
sub1.join("test_in_sub1.py").write("def test_1(): pass") sub1.joinpath("test_in_sub1.py").write_text("def test_1(): pass")
sub2.join("test_in_sub2.py").write("def test_2(): pass") sub2.joinpath("test_in_sub2.py").write_text("def test_2(): pass")
result = testdir.runpytest("--keep-duplicates", "-v", "-s", sub1, sub2, sub1) result = pytester.runpytest("--keep-duplicates", "-v", "-s", sub1, sub2, sub1)
result.assert_outcomes(passed=3) result.assert_outcomes(passed=3)
def test_generate_same_function_names_issue403(self, testdir: Testdir) -> None: def test_generate_same_function_names_issue403(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1392,12 +1394,12 @@ class TestMetafuncFunctional:
test_y = make_tests() test_y = make_tests()
""" """
) )
reprec = testdir.runpytest() reprec = pytester.runpytest()
reprec.assert_outcomes(passed=4) reprec.assert_outcomes(passed=4)
def test_parametrize_misspelling(self, testdir: Testdir) -> None: def test_parametrize_misspelling(self, pytester: Pytester) -> None:
"""#463""" """#463"""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1406,7 +1408,7 @@ class TestMetafuncFunctional:
pass pass
""" """
) )
result = testdir.runpytest("--collectonly") result = pytester.runpytest("--collectonly")
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"collected 0 items / 1 error", "collected 0 items / 1 error",
@ -1426,8 +1428,8 @@ class TestMetafuncFunctionalAuto:
"""Tests related to automatically find out the correct scope for """Tests related to automatically find out the correct scope for
parametrized tests (#1832).""" parametrized tests (#1832)."""
def test_parametrize_auto_scope(self, testdir: Testdir) -> None: def test_parametrize_auto_scope(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1445,11 +1447,11 @@ class TestMetafuncFunctionalAuto:
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 3 passed *"]) result.stdout.fnmatch_lines(["* 3 passed *"])
def test_parametrize_auto_scope_indirect(self, testdir: Testdir) -> None: def test_parametrize_auto_scope_indirect(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1468,11 +1470,11 @@ class TestMetafuncFunctionalAuto:
assert echo in (1, 2, 3) assert echo in (1, 2, 3)
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 3 passed *"]) result.stdout.fnmatch_lines(["* 3 passed *"])
def test_parametrize_auto_scope_override_fixture(self, testdir: Testdir) -> None: def test_parametrize_auto_scope_override_fixture(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1485,11 +1487,11 @@ class TestMetafuncFunctionalAuto:
assert animal in ('dog', 'cat') assert animal in ('dog', 'cat')
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 2 passed *"]) result.stdout.fnmatch_lines(["* 2 passed *"])
def test_parametrize_all_indirects(self, testdir: Testdir) -> None: def test_parametrize_all_indirects(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1512,11 +1514,11 @@ class TestMetafuncFunctionalAuto:
assert echo in (1, 2, 3) assert echo in (1, 2, 3)
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["* 3 passed *"]) result.stdout.fnmatch_lines(["* 3 passed *"])
def test_parametrize_some_arguments_auto_scope( def test_parametrize_some_arguments_auto_scope(
self, testdir: Testdir, monkeypatch self, pytester: Pytester, monkeypatch
) -> None: ) -> None:
"""Integration test for (#3941)""" """Integration test for (#3941)"""
class_fix_setup: List[object] = [] class_fix_setup: List[object] = []
@ -1524,7 +1526,7 @@ class TestMetafuncFunctionalAuto:
func_fix_setup: List[object] = [] func_fix_setup: List[object] = []
monkeypatch.setattr(sys, "func_fix_setup", func_fix_setup, raising=False) monkeypatch.setattr(sys, "func_fix_setup", func_fix_setup, raising=False)
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
import sys import sys
@ -1545,13 +1547,13 @@ class TestMetafuncFunctionalAuto:
pass pass
""" """
) )
result = testdir.runpytest_inprocess() result = pytester.runpytest_inprocess()
result.stdout.fnmatch_lines(["* 4 passed in *"]) result.stdout.fnmatch_lines(["* 4 passed in *"])
assert func_fix_setup == [True] * 4 assert func_fix_setup == [True] * 4
assert class_fix_setup == [10, 20] assert class_fix_setup == [10, 20]
def test_parametrize_issue634(self, testdir: Testdir) -> None: def test_parametrize_issue634(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1579,7 +1581,7 @@ class TestMetafuncFunctionalAuto:
metafunc.parametrize('foo', params, indirect=True) metafunc.parametrize('foo', params, indirect=True)
""" """
) )
result = testdir.runpytest("-s") result = pytester.runpytest("-s")
output = result.stdout.str() output = result.stdout.str()
assert output.count("preparing foo-2") == 1 assert output.count("preparing foo-2") == 1
assert output.count("preparing foo-3") == 1 assert output.count("preparing foo-3") == 1
@ -1588,7 +1590,7 @@ class TestMetafuncFunctionalAuto:
class TestMarkersWithParametrization: class TestMarkersWithParametrization:
"""#308""" """#308"""
def test_simple_mark(self, testdir: Testdir) -> None: def test_simple_mark(self, pytester: Pytester) -> None:
s = """ s = """
import pytest import pytest
@ -1601,7 +1603,7 @@ class TestMarkersWithParametrization:
def test_increment(n, expected): def test_increment(n, expected):
assert n + 1 == expected assert n + 1 == expected
""" """
items = testdir.getitems(s) items = pytester.getitems(s)
assert len(items) == 3 assert len(items) == 3
for item in items: for item in items:
assert "foo" in item.keywords assert "foo" in item.keywords
@ -1609,7 +1611,7 @@ class TestMarkersWithParametrization:
assert "bar" in items[1].keywords assert "bar" in items[1].keywords
assert "bar" not in items[2].keywords assert "bar" not in items[2].keywords
def test_select_based_on_mark(self, testdir: Testdir) -> None: def test_select_based_on_mark(self, pytester: Pytester) -> None:
s = """ s = """
import pytest import pytest
@ -1621,14 +1623,14 @@ class TestMarkersWithParametrization:
def test_increment(n, expected): def test_increment(n, expected):
assert n + 1 == expected assert n + 1 == expected
""" """
testdir.makepyfile(s) pytester.makepyfile(s)
rec = testdir.inline_run("-m", "foo") rec = pytester.inline_run("-m", "foo")
passed, skipped, fail = rec.listoutcomes() passed, skipped, fail = rec.listoutcomes()
assert len(passed) == 1 assert len(passed) == 1
assert len(skipped) == 0 assert len(skipped) == 0
assert len(fail) == 0 assert len(fail) == 0
def test_simple_xfail(self, testdir: Testdir) -> None: def test_simple_xfail(self, pytester: Pytester) -> None:
s = """ s = """
import pytest import pytest
@ -1640,12 +1642,12 @@ class TestMarkersWithParametrization:
def test_increment(n, expected): def test_increment(n, expected):
assert n + 1 == expected assert n + 1 == expected
""" """
testdir.makepyfile(s) pytester.makepyfile(s)
reprec = testdir.inline_run() reprec = pytester.inline_run()
# xfail is skip?? # xfail is skip??
reprec.assertoutcome(passed=2, skipped=1) reprec.assertoutcome(passed=2, skipped=1)
def test_simple_xfail_single_argname(self, testdir: Testdir) -> None: def test_simple_xfail_single_argname(self, pytester: Pytester) -> None:
s = """ s = """
import pytest import pytest
@ -1657,11 +1659,11 @@ class TestMarkersWithParametrization:
def test_isEven(n): def test_isEven(n):
assert n % 2 == 0 assert n % 2 == 0
""" """
testdir.makepyfile(s) pytester.makepyfile(s)
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=2, skipped=1) reprec.assertoutcome(passed=2, skipped=1)
def test_xfail_with_arg(self, testdir: Testdir) -> None: def test_xfail_with_arg(self, pytester: Pytester) -> None:
s = """ s = """
import pytest import pytest
@ -1673,11 +1675,11 @@ class TestMarkersWithParametrization:
def test_increment(n, expected): def test_increment(n, expected):
assert n + 1 == expected assert n + 1 == expected
""" """
testdir.makepyfile(s) pytester.makepyfile(s)
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=2, skipped=1) reprec.assertoutcome(passed=2, skipped=1)
def test_xfail_with_kwarg(self, testdir: Testdir) -> None: def test_xfail_with_kwarg(self, pytester: Pytester) -> None:
s = """ s = """
import pytest import pytest
@ -1689,11 +1691,11 @@ class TestMarkersWithParametrization:
def test_increment(n, expected): def test_increment(n, expected):
assert n + 1 == expected assert n + 1 == expected
""" """
testdir.makepyfile(s) pytester.makepyfile(s)
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=2, skipped=1) reprec.assertoutcome(passed=2, skipped=1)
def test_xfail_with_arg_and_kwarg(self, testdir: Testdir) -> None: def test_xfail_with_arg_and_kwarg(self, pytester: Pytester) -> None:
s = """ s = """
import pytest import pytest
@ -1705,12 +1707,12 @@ class TestMarkersWithParametrization:
def test_increment(n, expected): def test_increment(n, expected):
assert n + 1 == expected assert n + 1 == expected
""" """
testdir.makepyfile(s) pytester.makepyfile(s)
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=2, skipped=1) reprec.assertoutcome(passed=2, skipped=1)
@pytest.mark.parametrize("strict", [True, False]) @pytest.mark.parametrize("strict", [True, False])
def test_xfail_passing_is_xpass(self, testdir: Testdir, strict: bool) -> None: def test_xfail_passing_is_xpass(self, pytester: Pytester, strict: bool) -> None:
s = """ s = """
import pytest import pytest
@ -1726,12 +1728,12 @@ class TestMarkersWithParametrization:
""".format( """.format(
strict=strict strict=strict
) )
testdir.makepyfile(s) pytester.makepyfile(s)
reprec = testdir.inline_run() reprec = pytester.inline_run()
passed, failed = (2, 1) if strict else (3, 0) passed, failed = (2, 1) if strict else (3, 0)
reprec.assertoutcome(passed=passed, failed=failed) reprec.assertoutcome(passed=passed, failed=failed)
def test_parametrize_called_in_generate_tests(self, testdir: Testdir) -> None: def test_parametrize_called_in_generate_tests(self, pytester: Pytester) -> None:
s = """ s = """
import pytest import pytest
@ -1750,13 +1752,15 @@ class TestMarkersWithParametrization:
def test_increment(n, expected): def test_increment(n, expected):
assert n + 1 == expected assert n + 1 == expected
""" """
testdir.makepyfile(s) pytester.makepyfile(s)
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=2, skipped=2) reprec.assertoutcome(passed=2, skipped=2)
def test_parametrize_ID_generation_string_int_works(self, testdir: Testdir) -> None: def test_parametrize_ID_generation_string_int_works(
self, pytester: Pytester
) -> None:
"""#290""" """#290"""
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1769,11 +1773,11 @@ class TestMarkersWithParametrization:
return return
""" """
) )
reprec = testdir.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=2) reprec.assertoutcome(passed=2)
@pytest.mark.parametrize("strict", [True, False]) @pytest.mark.parametrize("strict", [True, False])
def test_parametrize_marked_value(self, testdir: Testdir, strict: bool) -> None: def test_parametrize_marked_value(self, pytester: Pytester, strict: bool) -> None:
s = """ s = """
import pytest import pytest
@ -1792,19 +1796,19 @@ class TestMarkersWithParametrization:
""".format( """.format(
strict=strict strict=strict
) )
testdir.makepyfile(s) pytester.makepyfile(s)
reprec = testdir.inline_run() reprec = pytester.inline_run()
passed, failed = (0, 2) if strict else (2, 0) passed, failed = (0, 2) if strict else (2, 0)
reprec.assertoutcome(passed=passed, failed=failed) reprec.assertoutcome(passed=passed, failed=failed)
def test_pytest_make_parametrize_id(self, testdir: Testdir) -> None: def test_pytest_make_parametrize_id(self, pytester: Pytester) -> None:
testdir.makeconftest( pytester.makeconftest(
""" """
def pytest_make_parametrize_id(config, val): def pytest_make_parametrize_id(config, val):
return str(val * 2) return str(val * 2)
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1813,17 +1817,17 @@ class TestMarkersWithParametrization:
pass pass
""" """
) )
result = testdir.runpytest("-v") result = pytester.runpytest("-v")
result.stdout.fnmatch_lines(["*test_func*0*PASS*", "*test_func*2*PASS*"]) result.stdout.fnmatch_lines(["*test_func*0*PASS*", "*test_func*2*PASS*"])
def test_pytest_make_parametrize_id_with_argname(self, testdir: Testdir) -> None: def test_pytest_make_parametrize_id_with_argname(self, pytester: Pytester) -> None:
testdir.makeconftest( pytester.makeconftest(
""" """
def pytest_make_parametrize_id(config, val, argname): def pytest_make_parametrize_id(config, val, argname):
return str(val * 2 if argname == 'x' else val * 10) return str(val * 2 if argname == 'x' else val * 10)
""" """
) )
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1836,13 +1840,13 @@ class TestMarkersWithParametrization:
pass pass
""" """
) )
result = testdir.runpytest("-v") result = pytester.runpytest("-v")
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
["*test_func_a*0*PASS*", "*test_func_a*2*PASS*", "*test_func_b*10*PASS*"] ["*test_func_a*0*PASS*", "*test_func_a*2*PASS*", "*test_func_b*10*PASS*"]
) )
def test_parametrize_positional_args(self, testdir: Testdir) -> None: def test_parametrize_positional_args(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
@ -1851,11 +1855,11 @@ class TestMarkersWithParametrization:
pass pass
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.assert_outcomes(passed=1) result.assert_outcomes(passed=1)
def test_parametrize_iterator(self, testdir: Testdir) -> None: def test_parametrize_iterator(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import itertools import itertools
import pytest import pytest
@ -1877,7 +1881,7 @@ class TestMarkersWithParametrization:
pass pass
""" """
) )
result = testdir.runpytest("-vv", "-s") result = pytester.runpytest("-vv", "-s")
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
[ [
"test_parametrize_iterator.py::test1[param0] PASSED", "test_parametrize_iterator.py::test1[param0] PASSED",

View File

@ -3,6 +3,7 @@ import sys
import pytest import pytest
from _pytest.outcomes import Failed from _pytest.outcomes import Failed
from _pytest.pytester import Pytester
class TestRaises: class TestRaises:
@ -50,8 +51,8 @@ class TestRaises:
pprint.pprint(excinfo) pprint.pprint(excinfo)
raise E() raise E()
def test_raises_as_contextmanager(self, testdir): def test_raises_as_contextmanager(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
import pytest import pytest
import _pytest._code import _pytest._code
@ -75,11 +76,11 @@ class TestRaises:
1/0 1/0
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["*3 passed*"]) result.stdout.fnmatch_lines(["*3 passed*"])
def test_does_not_raise(self, testdir): def test_does_not_raise(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
from contextlib import contextmanager from contextlib import contextmanager
import pytest import pytest
@ -100,11 +101,11 @@ class TestRaises:
assert (6 / example_input) is not None assert (6 / example_input) is not None
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["*4 passed*"]) result.stdout.fnmatch_lines(["*4 passed*"])
def test_does_not_raise_does_raise(self, testdir): def test_does_not_raise_does_raise(self, pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
""" """
from contextlib import contextmanager from contextlib import contextmanager
import pytest import pytest
@ -123,7 +124,7 @@ class TestRaises:
assert (6 / example_input) is not None assert (6 / example_input) is not None
""" """
) )
result = testdir.runpytest() result = pytester.runpytest()
result.stdout.fnmatch_lines(["*2 failed*"]) result.stdout.fnmatch_lines(["*2 failed*"])
def test_noclass(self) -> None: def test_noclass(self) -> None:

View File

@ -1,11 +1,14 @@
def test_no_items_should_not_show_output(testdir): from _pytest.pytester import Pytester
result = testdir.runpytest("--fixtures-per-test")
def test_no_items_should_not_show_output(pytester: Pytester) -> None:
result = pytester.runpytest("--fixtures-per-test")
result.stdout.no_fnmatch_line("*fixtures used by*") result.stdout.no_fnmatch_line("*fixtures used by*")
assert result.ret == 0 assert result.ret == 0
def test_fixtures_in_module(testdir): def test_fixtures_in_module(pytester: Pytester) -> None:
p = testdir.makepyfile( p = pytester.makepyfile(
''' '''
import pytest import pytest
@pytest.fixture @pytest.fixture
@ -19,7 +22,7 @@ def test_fixtures_in_module(testdir):
''' '''
) )
result = testdir.runpytest("--fixtures-per-test", p) result = pytester.runpytest("--fixtures-per-test", p)
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
@ -33,8 +36,8 @@ def test_fixtures_in_module(testdir):
result.stdout.no_fnmatch_line("*_arg0*") result.stdout.no_fnmatch_line("*_arg0*")
def test_fixtures_in_conftest(testdir): def test_fixtures_in_conftest(pytester: Pytester) -> None:
testdir.makeconftest( pytester.makeconftest(
''' '''
import pytest import pytest
@pytest.fixture @pytest.fixture
@ -50,7 +53,7 @@ def test_fixtures_in_conftest(testdir):
""" """
''' '''
) )
p = testdir.makepyfile( p = pytester.makepyfile(
""" """
def test_arg2(arg2): def test_arg2(arg2):
pass pass
@ -58,7 +61,7 @@ def test_fixtures_in_conftest(testdir):
pass pass
""" """
) )
result = testdir.runpytest("--fixtures-per-test", p) result = pytester.runpytest("--fixtures-per-test", p)
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
@ -80,8 +83,8 @@ def test_fixtures_in_conftest(testdir):
) )
def test_should_show_fixtures_used_by_test(testdir): def test_should_show_fixtures_used_by_test(pytester: Pytester) -> None:
testdir.makeconftest( pytester.makeconftest(
''' '''
import pytest import pytest
@pytest.fixture @pytest.fixture
@ -92,7 +95,7 @@ def test_should_show_fixtures_used_by_test(testdir):
"""arg2 from conftest""" """arg2 from conftest"""
''' '''
) )
p = testdir.makepyfile( p = pytester.makepyfile(
''' '''
import pytest import pytest
@pytest.fixture @pytest.fixture
@ -102,7 +105,7 @@ def test_should_show_fixtures_used_by_test(testdir):
pass pass
''' '''
) )
result = testdir.runpytest("--fixtures-per-test", p) result = pytester.runpytest("--fixtures-per-test", p)
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
@ -117,8 +120,8 @@ def test_should_show_fixtures_used_by_test(testdir):
) )
def test_verbose_include_private_fixtures_and_loc(testdir): def test_verbose_include_private_fixtures_and_loc(pytester: Pytester) -> None:
testdir.makeconftest( pytester.makeconftest(
''' '''
import pytest import pytest
@pytest.fixture @pytest.fixture
@ -129,7 +132,7 @@ def test_verbose_include_private_fixtures_and_loc(testdir):
"""arg2 from conftest""" """arg2 from conftest"""
''' '''
) )
p = testdir.makepyfile( p = pytester.makepyfile(
''' '''
import pytest import pytest
@pytest.fixture @pytest.fixture
@ -139,7 +142,7 @@ def test_verbose_include_private_fixtures_and_loc(testdir):
pass pass
''' '''
) )
result = testdir.runpytest("--fixtures-per-test", "-v", p) result = pytester.runpytest("--fixtures-per-test", "-v", p)
assert result.ret == 0 assert result.ret == 0
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
@ -156,8 +159,8 @@ def test_verbose_include_private_fixtures_and_loc(testdir):
) )
def test_doctest_items(testdir): def test_doctest_items(pytester: Pytester) -> None:
testdir.makepyfile( pytester.makepyfile(
''' '''
def foo(): def foo():
""" """
@ -166,13 +169,13 @@ def test_doctest_items(testdir):
""" """
''' '''
) )
testdir.maketxtfile( pytester.maketxtfile(
""" """
>>> 1 + 1 >>> 1 + 1
2 2
""" """
) )
result = testdir.runpytest( result = pytester.runpytest(
"--fixtures-per-test", "--doctest-modules", "--doctest-glob=*.txt", "-v" "--fixtures-per-test", "--doctest-modules", "--doctest-glob=*.txt", "-v"
) )
assert result.ret == 0 assert result.ret == 0