| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
from _pytest._code import getfslineno |
| 5 |
from _pytest.fixtures import getfixturemarker |
| 6 |
from _pytest.pytester import Pytester |
| 7 |
from _pytest.python import Function |
| 8 |
import pytest |
| 9 |
|
| 10 |
|
| 11 |
def test_wrapped_getfslineno() -> None: |
| 12 |
def func(): |
| 13 |
pass |
| 14 |
|
| 15 |
def wrap(f): |
| 16 |
func.__wrapped__ = f # type: ignore |
| 17 |
func.patchings = ["qwe"] # type: ignore |
| 18 |
return func |
| 19 |
|
| 20 |
@wrap |
| 21 |
def wrapped_func(x, y, z): |
| 22 |
pass |
| 23 |
|
| 24 |
_fs, lineno = getfslineno(wrapped_func) |
| 25 |
_fs2, lineno2 = getfslineno(wrap) |
| 26 |
assert lineno > lineno2, "getfslineno does not unwrap correctly" |
| 27 |
|
| 28 |
|
| 29 |
class TestMockDecoration: |
| 30 |
def test_wrapped_getfuncargnames(self) -> None: |
| 31 |
from _pytest.compat import getfuncargnames |
| 32 |
|
| 33 |
def wrap(f): |
| 34 |
def func(): |
| 35 |
pass |
| 36 |
|
| 37 |
func.__wrapped__ = f # type: ignore |
| 38 |
return func |
| 39 |
|
| 40 |
@wrap |
| 41 |
def f(x): |
| 42 |
pass |
| 43 |
|
| 44 |
values = getfuncargnames(f) |
| 45 |
assert values == ("x",) |
| 46 |
|
| 47 |
def test_getfuncargnames_patching(self): |
| 48 |
from unittest.mock import patch |
| 49 |
|
| 50 |
from _pytest.compat import getfuncargnames |
| 51 |
|
| 52 |
class T: |
| 53 |
def original(self, x, y, z): |
| 54 |
pass |
| 55 |
|
| 56 |
@patch.object(T, "original") |
| 57 |
def f(x, y, z): |
| 58 |
pass |
| 59 |
|
| 60 |
values = getfuncargnames(f) |
| 61 |
assert values == ("y", "z") |
| 62 |
|
| 63 |
def test_unittest_mock(self, pytester: Pytester) -> None: |
| 64 |
pytester.makepyfile( |
| 65 |
""" |
| 66 |
import unittest.mock |
| 67 |
class T(unittest.TestCase): |
| 68 |
@unittest.mock.patch("os.path.abspath") |
| 69 |
def test_hello(self, abspath): |
| 70 |
import os |
| 71 |
os.path.abspath("hello") |
| 72 |
abspath.assert_any_call("hello") |
| 73 |
""" |
| 74 |
) |
| 75 |
reprec = pytester.inline_run() |
| 76 |
reprec.assertoutcome(passed=1) |
| 77 |
|
| 78 |
def test_unittest_mock_and_fixture(self, pytester: Pytester) -> None: |
| 79 |
pytester.makepyfile( |
| 80 |
""" |
| 81 |
import os.path |
| 82 |
import unittest.mock |
| 83 |
import pytest |
| 84 |
|
| 85 |
@pytest.fixture |
| 86 |
def inject_me(): |
| 87 |
pass |
| 88 |
|
| 89 |
@unittest.mock.patch.object(os.path, "abspath", |
| 90 |
new=unittest.mock.MagicMock) |
| 91 |
def test_hello(inject_me): |
| 92 |
import os |
| 93 |
os.path.abspath("hello") |
| 94 |
""" |
| 95 |
) |
| 96 |
reprec = pytester.inline_run() |
| 97 |
reprec.assertoutcome(passed=1) |
| 98 |
|
| 99 |
def test_unittest_mock_and_pypi_mock(self, pytester: Pytester) -> None: |
| 100 |
pytest.importorskip("mock", "1.0.1") |
| 101 |
pytester.makepyfile( |
| 102 |
""" |
| 103 |
import mock |
| 104 |
import unittest.mock |
| 105 |
class TestBoth(object): |
| 106 |
@unittest.mock.patch("os.path.abspath") |
| 107 |
def test_hello(self, abspath): |
| 108 |
import os |
| 109 |
os.path.abspath("hello") |
| 110 |
abspath.assert_any_call("hello") |
| 111 |
|
| 112 |
@mock.patch("os.path.abspath") |
| 113 |
def test_hello_mock(self, abspath): |
| 114 |
import os |
| 115 |
os.path.abspath("hello") |
| 116 |
abspath.assert_any_call("hello") |
| 117 |
""" |
| 118 |
) |
| 119 |
reprec = pytester.inline_run() |
| 120 |
reprec.assertoutcome(passed=2) |
| 121 |
|
| 122 |
def test_mock_sentinel_check_against_numpy_like(self, pytester: Pytester) -> None: |
| 123 |
"""Ensure our function that detects mock arguments compares against sentinels using |
| 124 |
identity to circumvent objects which can't be compared with equality against others |
| 125 |
in a truth context, like with numpy arrays (#5606). |
| 126 |
""" |
| 127 |
pytester.makepyfile( |
| 128 |
dummy=""" |
| 129 |
class NumpyLike: |
| 130 |
def __init__(self, value): |
| 131 |
self.value = value |
| 132 |
def __eq__(self, other): |
| 133 |
raise ValueError("like numpy, cannot compare against others for truth") |
| 134 |
FOO = NumpyLike(10) |
| 135 |
""" |
| 136 |
) |
| 137 |
pytester.makepyfile( |
| 138 |
""" |
| 139 |
from unittest.mock import patch |
| 140 |
import dummy |
| 141 |
class Test(object): |
| 142 |
@patch("dummy.FOO", new=dummy.NumpyLike(50)) |
| 143 |
def test_hello(self): |
| 144 |
assert dummy.FOO.value == 50 |
| 145 |
""" |
| 146 |
) |
| 147 |
reprec = pytester.inline_run() |
| 148 |
reprec.assertoutcome(passed=1) |
| 149 |
|
| 150 |
def test_mock(self, pytester: Pytester) -> None: |
| 151 |
pytest.importorskip("mock", "1.0.1") |
| 152 |
pytester.makepyfile( |
| 153 |
""" |
| 154 |
import os |
| 155 |
import unittest |
| 156 |
import mock |
| 157 |
|
| 158 |
class T(unittest.TestCase): |
| 159 |
@mock.patch("os.path.abspath") |
| 160 |
def test_hello(self, abspath): |
| 161 |
os.path.abspath("hello") |
| 162 |
abspath.assert_any_call("hello") |
| 163 |
def mock_basename(path): |
| 164 |
return "mock_basename" |
| 165 |
@mock.patch("os.path.abspath") |
| 166 |
@mock.patch("os.path.normpath") |
| 167 |
@mock.patch("os.path.basename", new=mock_basename) |
| 168 |
def test_something(normpath, abspath, tmp_path): |
| 169 |
abspath.return_value = "this" |
| 170 |
os.path.normpath(os.path.abspath("hello")) |
| 171 |
normpath.assert_any_call("this") |
| 172 |
assert os.path.basename("123") == "mock_basename" |
| 173 |
""" |
| 174 |
) |
| 175 |
reprec = pytester.inline_run() |
| 176 |
reprec.assertoutcome(passed=2) |
| 177 |
calls = reprec.getcalls("pytest_runtest_logreport") |
| 178 |
funcnames = [ |
| 179 |
call.report.location[2] for call in calls if call.report.when == "call" |
| 180 |
] |
| 181 |
assert funcnames == ["T.test_hello", "test_something"] |
| 182 |
|
| 183 |
def test_mock_sorting(self, pytester: Pytester) -> None: |
| 184 |
pytest.importorskip("mock", "1.0.1") |
| 185 |
pytester.makepyfile( |
| 186 |
""" |
| 187 |
import os |
| 188 |
import mock |
| 189 |
|
| 190 |
@mock.patch("os.path.abspath") |
| 191 |
def test_one(abspath): |
| 192 |
pass |
| 193 |
@mock.patch("os.path.abspath") |
| 194 |
def test_two(abspath): |
| 195 |
pass |
| 196 |
@mock.patch("os.path.abspath") |
| 197 |
def test_three(abspath): |
| 198 |
pass |
| 199 |
""" |
| 200 |
) |
| 201 |
reprec = pytester.inline_run() |
| 202 |
calls = reprec.getreports("pytest_runtest_logreport") |
| 203 |
calls = [x for x in calls if x.when == "call"] |
| 204 |
names = [x.nodeid.split("::")[-1] for x in calls] |
| 205 |
assert names == ["test_one", "test_two", "test_three"] |
| 206 |
|
| 207 |
def test_mock_double_patch_issue473(self, pytester: Pytester) -> None: |
| 208 |
pytest.importorskip("mock", "1.0.1") |
| 209 |
pytester.makepyfile( |
| 210 |
""" |
| 211 |
from mock import patch |
| 212 |
from pytest import mark |
| 213 |
|
| 214 |
@patch('os.getcwd') |
| 215 |
@patch('os.path') |
| 216 |
@mark.slow |
| 217 |
class TestSimple(object): |
| 218 |
def test_simple_thing(self, mock_path, mock_getcwd): |
| 219 |
pass |
| 220 |
""" |
| 221 |
) |
| 222 |
reprec = pytester.inline_run() |
| 223 |
reprec.assertoutcome(passed=1) |
| 224 |
|
| 225 |
|
| 226 |
class TestReRunTests: |
| 227 |
def test_rerun(self, pytester: Pytester) -> None: |
| 228 |
pytester.makeconftest( |
| 229 |
""" |
| 230 |
from _pytest.runner import runtestprotocol |
| 231 |
def pytest_runtest_protocol(item, nextitem): |
| 232 |
runtestprotocol(item, log=False, nextitem=nextitem) |
| 233 |
runtestprotocol(item, log=True, nextitem=nextitem) |
| 234 |
""" |
| 235 |
) |
| 236 |
pytester.makepyfile( |
| 237 |
""" |
| 238 |
import pytest |
| 239 |
count = 0 |
| 240 |
req = None |
| 241 |
@pytest.fixture |
| 242 |
def fix(request): |
| 243 |
global count, req |
| 244 |
assert request != req |
| 245 |
req = request |
| 246 |
print("fix count %s" % count) |
| 247 |
count += 1 |
| 248 |
def test_fix(fix): |
| 249 |
pass |
| 250 |
""" |
| 251 |
) |
| 252 |
result = pytester.runpytest("-s") |
| 253 |
result.stdout.fnmatch_lines( |
| 254 |
""" |
| 255 |
*fix count 0* |
| 256 |
*fix count 1* |
| 257 |
""" |
| 258 |
) |
| 259 |
result.stdout.fnmatch_lines( |
| 260 |
""" |
| 261 |
*2 passed* |
| 262 |
""" |
| 263 |
) |
| 264 |
|
| 265 |
|
| 266 |
def test_pytestconfig_is_session_scoped() -> None: |
| 267 |
from _pytest.fixtures import pytestconfig |
| 268 |
|
| 269 |
marker = getfixturemarker(pytestconfig) |
| 270 |
assert marker is not None |
| 271 |
assert marker.scope == "session" |
| 272 |
|
| 273 |
|
| 274 |
class TestNoselikeTestAttribute: |
| 275 |
def test_module_with_global_test(self, pytester: Pytester) -> None: |
| 276 |
pytester.makepyfile( |
| 277 |
""" |
| 278 |
__test__ = False |
| 279 |
def test_hello(): |
| 280 |
pass |
| 281 |
""" |
| 282 |
) |
| 283 |
reprec = pytester.inline_run() |
| 284 |
assert not reprec.getfailedcollections() |
| 285 |
calls = reprec.getreports("pytest_runtest_logreport") |
| 286 |
assert not calls |
| 287 |
|
| 288 |
def test_class_and_method(self, pytester: Pytester) -> None: |
| 289 |
pytester.makepyfile( |
| 290 |
""" |
| 291 |
__test__ = True |
| 292 |
def test_func(): |
| 293 |
pass |
| 294 |
test_func.__test__ = False |
| 295 |
|
| 296 |
class TestSome(object): |
| 297 |
__test__ = False |
| 298 |
def test_method(self): |
| 299 |
pass |
| 300 |
""" |
| 301 |
) |
| 302 |
reprec = pytester.inline_run() |
| 303 |
assert not reprec.getfailedcollections() |
| 304 |
calls = reprec.getreports("pytest_runtest_logreport") |
| 305 |
assert not calls |
| 306 |
|
| 307 |
def test_unittest_class(self, pytester: Pytester) -> None: |
| 308 |
pytester.makepyfile( |
| 309 |
""" |
| 310 |
import unittest |
| 311 |
class TC(unittest.TestCase): |
| 312 |
def test_1(self): |
| 313 |
pass |
| 314 |
class TC2(unittest.TestCase): |
| 315 |
__test__ = False |
| 316 |
def test_2(self): |
| 317 |
pass |
| 318 |
""" |
| 319 |
) |
| 320 |
reprec = pytester.inline_run() |
| 321 |
assert not reprec.getfailedcollections() |
| 322 |
call = reprec.getcalls("pytest_collection_modifyitems")[0] |
| 323 |
assert len(call.items) == 1 |
| 324 |
assert call.items[0].cls.__name__ == "TC" |
| 325 |
|
| 326 |
def test_class_with_nasty_getattr(self, pytester: Pytester) -> None: |
| 327 |
"""Make sure we handle classes with a custom nasty __getattr__ right. |
| 328 |
|
| 329 |
With a custom __getattr__ which e.g. returns a function (like with a |
| 330 |
RPC wrapper), we shouldn't assume this meant "__test__ = True". |
| 331 |
""" |
| 332 |
# https://github.com/pytest-dev/pytest/issues/1204 |
| 333 |
pytester.makepyfile( |
| 334 |
""" |
| 335 |
class MetaModel(type): |
| 336 |
|
| 337 |
def __getattr__(cls, key): |
| 338 |
return lambda: None |
| 339 |
|
| 340 |
|
| 341 |
BaseModel = MetaModel('Model', (), {}) |
| 342 |
|
| 343 |
|
| 344 |
class Model(BaseModel): |
| 345 |
|
| 346 |
__metaclass__ = MetaModel |
| 347 |
|
| 348 |
def test_blah(self): |
| 349 |
pass |
| 350 |
""" |
| 351 |
) |
| 352 |
reprec = pytester.inline_run() |
| 353 |
assert not reprec.getfailedcollections() |
| 354 |
call = reprec.getcalls("pytest_collection_modifyitems")[0] |
| 355 |
assert not call.items |
| 356 |
|
| 357 |
|
| 358 |
class TestParameterize: |
| 359 |
"""#351""" |
| 360 |
|
| 361 |
def test_idfn_marker(self, pytester: Pytester) -> None: |
| 362 |
pytester.makepyfile( |
| 363 |
""" |
| 364 |
import pytest |
| 365 |
|
| 366 |
def idfn(param): |
| 367 |
if param == 0: |
| 368 |
return 'spam' |
| 369 |
elif param == 1: |
| 370 |
return 'ham' |
| 371 |
else: |
| 372 |
return None |
| 373 |
|
| 374 |
@pytest.mark.parametrize('a,b', [(0, 2), (1, 2)], ids=idfn) |
| 375 |
def test_params(a, b): |
| 376 |
pass |
| 377 |
""" |
| 378 |
) |
| 379 |
res = pytester.runpytest("--collect-only") |
| 380 |
res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"]) |
| 381 |
|
| 382 |
def test_idfn_fixture(self, pytester: Pytester) -> None: |
| 383 |
pytester.makepyfile( |
| 384 |
""" |
| 385 |
import pytest |
| 386 |
|
| 387 |
def idfn(param): |
| 388 |
if param == 0: |
| 389 |
return 'spam' |
| 390 |
elif param == 1: |
| 391 |
return 'ham' |
| 392 |
else: |
| 393 |
return None |
| 394 |
|
| 395 |
@pytest.fixture(params=[0, 1], ids=idfn) |
| 396 |
def a(request): |
| 397 |
return request.param |
| 398 |
|
| 399 |
@pytest.fixture(params=[1, 2], ids=idfn) |
| 400 |
def b(request): |
| 401 |
return request.param |
| 402 |
|
| 403 |
def test_params(a, b): |
| 404 |
pass |
| 405 |
""" |
| 406 |
) |
| 407 |
res = pytester.runpytest("--collect-only") |
| 408 |
res.stdout.fnmatch_lines(["*spam-2*", "*ham-2*"]) |
| 409 |
|
| 410 |
def test_param_rejects_usefixtures(self, pytester: Pytester) -> None: |
| 411 |
pytester.makepyfile( |
| 412 |
""" |
| 413 |
import pytest |
| 414 |
|
| 415 |
@pytest.mark.parametrize("x", [ |
| 416 |
pytest.param(1, marks=[pytest.mark.usefixtures("foo")]), |
| 417 |
]) |
| 418 |
def test_foo(x): |
| 419 |
pass |
| 420 |
""" |
| 421 |
) |
| 422 |
res = pytester.runpytest("--collect-only") |
| 423 |
res.stdout.fnmatch_lines( |
| 424 |
["*test_param_rejects_usefixtures.py:4*", "*pytest.param(*"] |
| 425 |
) |
| 426 |
|
| 427 |
|
| 428 |
def test_function_instance(pytester: Pytester) -> None: |
| 429 |
items = pytester.getitems( |
| 430 |
""" |
| 431 |
def test_func(): pass |
| 432 |
|
| 433 |
class TestIt: |
| 434 |
def test_method(self): pass |
| 435 |
|
| 436 |
@classmethod |
| 437 |
def test_class(cls): pass |
| 438 |
|
| 439 |
@staticmethod |
| 440 |
def test_static(): pass |
| 441 |
""" |
| 442 |
) |
| 443 |
assert len(items) == 4 |
| 444 |
|
| 445 |
assert isinstance(items[0], Function) |
| 446 |
assert items[0].name == "test_func" |
| 447 |
assert items[0].instance is None |
| 448 |
|
| 449 |
assert isinstance(items[1], Function) |
| 450 |
assert items[1].name == "test_method" |
| 451 |
assert items[1].instance is not None |
| 452 |
assert items[1].instance.__class__.__name__ == "TestIt" |
| 453 |
|
| 454 |
# Even class and static methods get an instance! |
| 455 |
# This is the instance used for bound fixture methods, which |
| 456 |
# class/staticmethod tests are perfectly able to request. |
| 457 |
assert isinstance(items[2], Function) |
| 458 |
assert items[2].name == "test_class" |
| 459 |
assert items[2].instance is not None |
| 460 |
|
| 461 |
assert isinstance(items[3], Function) |
| 462 |
assert items[3].name == "test_static" |
| 463 |
assert items[3].instance is not None |
| 464 |
|
| 465 |
assert items[1].instance is not items[2].instance is not items[3].instance |