tests: Migrate testing/python to pytester fixture

This commit is contained in:
antonblr
2020-12-09 21:47:58 -08:00
parent 7e2e6630ad
commit 4ed9a38519
7 changed files with 1250 additions and 1131 deletions

View File

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