2015-07-25 22:28:18 +08:00
|
|
|
import os
|
2016-09-07 17:00:27 +08:00
|
|
|
import sys
|
2019-05-28 07:31:52 +08:00
|
|
|
from unittest import mock
|
2018-11-19 06:16:13 +08:00
|
|
|
|
|
|
|
import pytest
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2018-11-19 06:16:13 +08:00
|
|
|
from _pytest.mark import MarkGenerator as Mark
|
2020-07-31 14:46:56 +08:00
|
|
|
from _pytest.mark.structures import EMPTY_PARAMETERSET_OPTION
|
2018-11-19 06:16:13 +08:00
|
|
|
from _pytest.nodes import Collector
|
|
|
|
from _pytest.nodes import Node
|
2018-03-18 04:42:43 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestMark:
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("attr", ["mark", "param"])
|
|
|
|
@pytest.mark.parametrize("modulename", ["py.test", "pytest"])
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_pytest_exists_in_namespace_all(self, attr: str, modulename: str) -> None:
|
2016-09-07 17:00:27 +08:00
|
|
|
module = sys.modules[modulename]
|
2020-05-01 19:40:17 +08:00
|
|
|
assert attr in module.__all__ # type: ignore
|
2010-11-18 21:56:16 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_pytest_mark_notcallable(self) -> None:
|
2009-10-22 21:21:58 +08:00
|
|
|
mark = Mark()
|
2019-06-30 22:40:24 +08:00
|
|
|
with pytest.raises(TypeError):
|
2020-07-10 14:44:14 +08:00
|
|
|
mark() # type: ignore[operator]
|
2009-10-22 21:21:58 +08:00
|
|
|
|
2017-07-21 13:26:56 +08:00
|
|
|
def test_mark_with_param(self):
|
|
|
|
def some_function(abc):
|
|
|
|
pass
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class SomeClass:
|
2017-07-21 13:26:56 +08:00
|
|
|
pass
|
|
|
|
|
2019-04-27 22:43:36 +08:00
|
|
|
assert pytest.mark.foo(some_function) is some_function
|
2020-05-01 19:40:17 +08:00
|
|
|
marked_with_args = pytest.mark.foo.with_args(some_function)
|
2020-07-10 14:44:14 +08:00
|
|
|
assert marked_with_args is not some_function # type: ignore[comparison-overlap]
|
2017-07-21 13:26:56 +08:00
|
|
|
|
2019-04-27 22:43:36 +08:00
|
|
|
assert pytest.mark.foo(SomeClass) is SomeClass
|
2020-07-10 14:44:14 +08:00
|
|
|
assert pytest.mark.foo.with_args(SomeClass) is not SomeClass # type: ignore[comparison-overlap]
|
2017-07-21 13:26:56 +08:00
|
|
|
|
2013-12-19 21:29:57 +08:00
|
|
|
def test_pytest_mark_name_starts_with_underscore(self):
|
|
|
|
mark = Mark()
|
2018-12-19 23:00:59 +08:00
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
mark._some_name
|
2010-11-21 03:17:38 +08:00
|
|
|
|
2011-11-12 06:56:11 +08:00
|
|
|
|
2020-01-17 02:42:29 +08:00
|
|
|
def test_marked_class_run_twice(testdir):
|
2015-07-25 22:28:18 +08:00
|
|
|
"""Test fails file is run twice that contains marked class.
|
|
|
|
See issue#683.
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
py_file = testdir.makepyfile(
|
|
|
|
"""
|
2015-07-25 22:28:18 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('abc', [1, 2, 3])
|
|
|
|
class Test1(object):
|
|
|
|
def test_1(self, abc):
|
|
|
|
assert abc in [1, 2, 3]
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-25 22:28:18 +08:00
|
|
|
file_name = os.path.basename(py_file.strpath)
|
|
|
|
rec = testdir.inline_run(file_name, file_name)
|
|
|
|
rec.assertoutcome(passed=6)
|
|
|
|
|
|
|
|
|
2011-11-12 06:56:11 +08:00
|
|
|
def test_ini_markers(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2011-11-12 06:56:11 +08:00
|
|
|
[pytest]
|
|
|
|
markers =
|
|
|
|
a1: this is a webtest marker
|
|
|
|
a2: this is a smoke marker
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2011-11-12 06:56:11 +08:00
|
|
|
def test_markers(pytestconfig):
|
|
|
|
markers = pytestconfig.getini("markers")
|
2018-11-22 16:15:14 +08:00
|
|
|
print(markers)
|
2011-11-12 06:56:11 +08:00
|
|
|
assert len(markers) >= 2
|
|
|
|
assert markers[0].startswith("a1:")
|
|
|
|
assert markers[1].startswith("a2:")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2011-11-12 06:56:11 +08:00
|
|
|
rec = testdir.inline_run()
|
|
|
|
rec.assertoutcome(passed=1)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2011-11-12 06:56:11 +08:00
|
|
|
def test_markers_option(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2011-11-12 06:56:11 +08:00
|
|
|
[pytest]
|
|
|
|
markers =
|
|
|
|
a1: this is a webtest marker
|
|
|
|
a1some: another marker
|
2017-11-22 20:47:15 +08:00
|
|
|
nodescription
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("--markers")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*a1*this is a webtest*", "*a1some*another marker", "*nodescription*"]
|
|
|
|
)
|
2011-11-12 06:56:11 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-10-24 16:17:01 +08:00
|
|
|
def test_ini_markers_whitespace(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeini(
|
|
|
|
"""
|
2017-10-24 16:17:01 +08:00
|
|
|
[pytest]
|
|
|
|
markers =
|
|
|
|
a1 : this is a whitespace marker
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-10-24 16:17:01 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.a1
|
|
|
|
def test_markers():
|
|
|
|
assert True
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-04-27 21:52:12 +08:00
|
|
|
rec = testdir.inline_run("--strict-markers", "-m", "a1")
|
2017-10-24 16:17:01 +08:00
|
|
|
rec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
2017-11-22 20:47:15 +08:00
|
|
|
def test_marker_without_description(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makefile(
|
|
|
|
".cfg",
|
|
|
|
setup="""
|
2017-11-22 20:47:15 +08:00
|
|
|
[tool:pytest]
|
|
|
|
markers=slow
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
)
|
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2017-11-22 20:47:15 +08:00
|
|
|
import pytest
|
|
|
|
pytest.mark.xfail('FAIL')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-11-22 20:47:15 +08:00
|
|
|
ftdir = testdir.mkdir("ft1_dummy")
|
|
|
|
testdir.tmpdir.join("conftest.py").move(ftdir.join("conftest.py"))
|
2019-04-27 21:52:12 +08:00
|
|
|
rec = testdir.runpytest("--strict-markers")
|
2017-11-22 20:47:15 +08:00
|
|
|
rec.assert_outcomes()
|
|
|
|
|
|
|
|
|
2014-01-22 21:16:39 +08:00
|
|
|
def test_markers_option_with_plugin_in_current_dir(testdir):
|
|
|
|
testdir.makeconftest('pytest_plugins = "flip_flop"')
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
flip_flop="""\
|
2014-01-22 21:16:39 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
config.addinivalue_line("markers", "flip:flop")
|
|
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
try:
|
|
|
|
mark = metafunc.function.flipper
|
|
|
|
except AttributeError:
|
|
|
|
return
|
2018-05-23 22:48:46 +08:00
|
|
|
metafunc.parametrize("x", (10, 20))"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""\
|
2014-01-22 21:16:39 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.flipper
|
|
|
|
def test_example(x):
|
2018-05-23 22:48:46 +08:00
|
|
|
assert x"""
|
|
|
|
)
|
2014-01-22 21:16:39 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest("--markers")
|
|
|
|
result.stdout.fnmatch_lines(["*flip*flop*"])
|
|
|
|
|
|
|
|
|
2013-10-11 20:36:54 +08:00
|
|
|
def test_mark_on_pseudo_function(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-10-11 20:36:54 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.r(lambda x: 0/0)
|
|
|
|
def test_hello():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-10-11 20:36:54 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
2011-11-12 06:56:11 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-04-27 21:52:12 +08:00
|
|
|
@pytest.mark.parametrize("option_name", ["--strict-markers", "--strict"])
|
|
|
|
def test_strict_prohibits_unregistered_markers(testdir, option_name):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2011-11-12 06:56:11 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.unregisteredmark
|
|
|
|
def test_hello():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-04-27 21:52:12 +08:00
|
|
|
result = testdir.runpytest(option_name)
|
2011-11-12 06:56:11 +08:00
|
|
|
assert result.ret != 0
|
2019-04-27 21:52:12 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["'unregisteredmark' not found in `markers` configuration option"]
|
|
|
|
)
|
2011-11-12 06:56:11 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
2020-05-08 01:42:04 +08:00
|
|
|
("expr", "expected_passed"),
|
2018-05-23 22:48:46 +08:00
|
|
|
[
|
2020-05-08 01:42:04 +08:00
|
|
|
("xyz", ["test_one"]),
|
|
|
|
("((( xyz)) )", ["test_one"]),
|
|
|
|
("not not xyz", ["test_one"]),
|
|
|
|
("xyz and xyz2", []),
|
|
|
|
("xyz2", ["test_two"]),
|
|
|
|
("xyz or xyz2", ["test_one", "test_two"]),
|
2018-05-23 22:48:46 +08:00
|
|
|
],
|
|
|
|
)
|
2020-05-08 01:42:04 +08:00
|
|
|
def test_mark_option(expr: str, expected_passed: str, testdir) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2011-11-12 07:02:06 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.xyz
|
|
|
|
def test_one():
|
|
|
|
pass
|
|
|
|
@pytest.mark.xyz2
|
|
|
|
def test_two():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-05-08 01:42:04 +08:00
|
|
|
rec = testdir.inline_run("-m", expr)
|
2011-11-12 07:02:06 +08:00
|
|
|
passed, skipped, fail = rec.listoutcomes()
|
|
|
|
passed = [x.nodeid.split("::")[-1] for x in passed]
|
2020-05-08 01:42:04 +08:00
|
|
|
assert passed == expected_passed
|
2011-11-12 07:02:06 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
2020-05-08 01:42:04 +08:00
|
|
|
("expr", "expected_passed"),
|
|
|
|
[("interface", ["test_interface"]), ("not interface", ["test_nointer"])],
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-05-08 01:42:04 +08:00
|
|
|
def test_mark_option_custom(expr: str, expected_passed: str, testdir) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2012-11-09 19:07:41 +08:00
|
|
|
import pytest
|
|
|
|
def pytest_collection_modifyitems(items):
|
|
|
|
for item in items:
|
|
|
|
if "interface" in item.nodeid:
|
2018-05-03 22:33:16 +08:00
|
|
|
item.add_marker(pytest.mark.interface)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2012-11-09 19:07:41 +08:00
|
|
|
def test_interface():
|
|
|
|
pass
|
|
|
|
def test_nointer():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-05-08 01:42:04 +08:00
|
|
|
rec = testdir.inline_run("-m", expr)
|
2012-11-09 19:07:41 +08:00
|
|
|
passed, skipped, fail = rec.listoutcomes()
|
|
|
|
passed = [x.nodeid.split("::")[-1] for x in passed]
|
2020-05-08 01:42:04 +08:00
|
|
|
assert passed == expected_passed
|
2011-11-12 07:02:06 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
2020-05-08 01:42:04 +08:00
|
|
|
("expr", "expected_passed"),
|
2018-05-23 22:48:46 +08:00
|
|
|
[
|
2020-05-08 01:42:04 +08:00
|
|
|
("interface", ["test_interface"]),
|
|
|
|
("not interface", ["test_nointer", "test_pass", "test_1", "test_2"]),
|
|
|
|
("pass", ["test_pass"]),
|
|
|
|
("not pass", ["test_interface", "test_nointer", "test_1", "test_2"]),
|
|
|
|
("not not not (pass)", ["test_interface", "test_nointer", "test_1", "test_2"]),
|
|
|
|
("1 or 2", ["test_1", "test_2"]),
|
|
|
|
("not (1 or 2)", ["test_interface", "test_nointer", "test_pass"]),
|
2018-05-23 22:48:46 +08:00
|
|
|
],
|
|
|
|
)
|
2020-05-08 01:42:04 +08:00
|
|
|
def test_keyword_option_custom(expr: str, expected_passed: str, testdir) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2012-11-09 19:29:33 +08:00
|
|
|
def test_interface():
|
|
|
|
pass
|
|
|
|
def test_nointer():
|
|
|
|
pass
|
2013-11-21 22:25:16 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
2020-04-25 18:38:53 +08:00
|
|
|
def test_1():
|
|
|
|
pass
|
|
|
|
def test_2():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-05-08 01:42:04 +08:00
|
|
|
rec = testdir.inline_run("-k", expr)
|
2012-11-09 19:29:33 +08:00
|
|
|
passed, skipped, fail = rec.listoutcomes()
|
|
|
|
passed = [x.nodeid.split("::")[-1] for x in passed]
|
2020-05-08 01:42:04 +08:00
|
|
|
assert passed == expected_passed
|
2012-11-09 19:29:33 +08:00
|
|
|
|
2013-10-03 21:43:56 +08:00
|
|
|
|
2019-01-19 05:05:41 +08:00
|
|
|
def test_keyword_option_considers_mark(testdir):
|
|
|
|
testdir.copy_example("marks/marks_considered_keywords")
|
|
|
|
rec = testdir.inline_run("-k", "foo")
|
|
|
|
passed = rec.listoutcomes()[0]
|
|
|
|
assert len(passed) == 1
|
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
2020-05-08 01:42:04 +08:00
|
|
|
("expr", "expected_passed"),
|
2018-05-23 22:48:46 +08:00
|
|
|
[
|
2020-05-08 01:42:04 +08:00
|
|
|
("None", ["test_func[None]"]),
|
|
|
|
("[1.3]", ["test_func[1.3]"]),
|
|
|
|
("2-3", ["test_func[2-3]"]),
|
2018-05-23 22:48:46 +08:00
|
|
|
],
|
|
|
|
)
|
2020-05-08 01:42:04 +08:00
|
|
|
def test_keyword_option_parametrize(expr: str, expected_passed: str, testdir) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-11-21 22:25:16 +08:00
|
|
|
import pytest
|
2014-10-06 18:11:48 +08:00
|
|
|
@pytest.mark.parametrize("arg", [None, 1.3, "2-3"])
|
2013-11-21 22:25:16 +08:00
|
|
|
def test_func(arg):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-05-08 01:42:04 +08:00
|
|
|
rec = testdir.inline_run("-k", expr)
|
2013-11-21 22:25:16 +08:00
|
|
|
passed, skipped, fail = rec.listoutcomes()
|
|
|
|
passed = [x.nodeid.split("::")[-1] for x in passed]
|
2020-05-08 01:42:04 +08:00
|
|
|
assert passed == expected_passed
|
2013-11-21 22:25:16 +08:00
|
|
|
|
2016-03-14 05:37:21 +08:00
|
|
|
|
2019-11-08 14:34:46 +08:00
|
|
|
def test_parametrize_with_module(testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize("arg", [pytest,])
|
|
|
|
def test_func(arg):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
rec = testdir.inline_run()
|
|
|
|
passed, skipped, fail = rec.listoutcomes()
|
|
|
|
expected_id = "test_func[" + pytest.__name__ + "]"
|
|
|
|
assert passed[0].nodeid.split("::")[-1] == expected_id
|
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
2020-05-08 01:42:04 +08:00
|
|
|
("expr", "expected_error"),
|
2018-05-23 22:48:46 +08:00
|
|
|
[
|
|
|
|
(
|
2020-04-25 18:38:53 +08:00
|
|
|
"foo or",
|
|
|
|
"at column 7: expected not OR left parenthesis OR identifier; got end of input",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"foo or or",
|
|
|
|
"at column 8: expected not OR left parenthesis OR identifier; got or",
|
|
|
|
),
|
|
|
|
("(foo", "at column 5: expected right parenthesis; got end of input",),
|
|
|
|
("foo bar", "at column 5: expected end of input; got identifier",),
|
|
|
|
(
|
|
|
|
"or or",
|
|
|
|
"at column 1: expected not OR left parenthesis OR identifier; got or",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"not or",
|
|
|
|
"at column 5: expected not OR left parenthesis OR identifier; got or",
|
2018-05-23 22:48:46 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2020-05-08 01:42:04 +08:00
|
|
|
def test_keyword_option_wrong_arguments(
|
|
|
|
expr: str, expected_error: str, testdir, capsys
|
|
|
|
) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2018-01-17 02:30:44 +08:00
|
|
|
def test_func(arg):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-05-08 01:42:04 +08:00
|
|
|
testdir.inline_run("-k", expr)
|
|
|
|
err = capsys.readouterr().err
|
|
|
|
assert expected_error in err
|
2018-01-17 02:30:44 +08:00
|
|
|
|
|
|
|
|
2016-03-14 05:37:21 +08:00
|
|
|
def test_parametrized_collected_from_command_line(testdir):
|
|
|
|
"""Parametrized test not collected if test named specified
|
|
|
|
in command line issue#649.
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
py_file = testdir.makepyfile(
|
|
|
|
"""
|
2016-03-14 05:37:21 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize("arg", [None, 1.3, "2-3"])
|
|
|
|
def test_func(arg):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-03-14 05:37:21 +08:00
|
|
|
file_name = os.path.basename(py_file.strpath)
|
|
|
|
rec = testdir.inline_run(file_name + "::" + "test_func")
|
|
|
|
rec.assertoutcome(passed=3)
|
|
|
|
|
|
|
|
|
2017-04-29 19:32:09 +08:00
|
|
|
def test_parametrized_collect_with_wrong_args(testdir):
|
|
|
|
"""Test collect parametrized func with wrong number of args."""
|
2018-05-23 22:48:46 +08:00
|
|
|
py_file = testdir.makepyfile(
|
|
|
|
"""
|
2017-04-29 19:32:09 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('foo, bar', [(1, 2, 3)])
|
|
|
|
def test_func(foo, bar):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-04-29 19:32:09 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest(py_file)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2018-12-21 02:13:43 +08:00
|
|
|
'test_parametrized_collect_with_wrong_args.py::test_func: in "parametrize" the number of names (2):',
|
|
|
|
" ['foo', 'bar']",
|
|
|
|
"must be equal to the number of values (3):",
|
|
|
|
" (1, 2, 3)",
|
2018-05-23 22:48:46 +08:00
|
|
|
]
|
|
|
|
)
|
2017-04-29 19:32:09 +08:00
|
|
|
|
|
|
|
|
2017-10-09 00:02:54 +08:00
|
|
|
def test_parametrized_with_kwargs(testdir):
|
|
|
|
"""Test collect parametrized func with wrong number of args."""
|
2018-05-23 22:48:46 +08:00
|
|
|
py_file = testdir.makepyfile(
|
|
|
|
"""
|
2017-10-09 00:02:54 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1,2])
|
|
|
|
def a(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(argnames='b', argvalues=[1, 2])
|
|
|
|
def test_func(a, b):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-10-09 00:02:54 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest(py_file)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert result.ret == 0
|
2017-10-09 00:02:54 +08:00
|
|
|
|
|
|
|
|
2019-06-02 04:50:15 +08:00
|
|
|
def test_parametrize_iterator(testdir):
|
|
|
|
"""parametrize should work with generators (#5354)."""
|
|
|
|
py_file = testdir.makepyfile(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def gen():
|
|
|
|
yield 1
|
|
|
|
yield 2
|
|
|
|
yield 3
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('a', gen())
|
|
|
|
def test(a):
|
|
|
|
assert a >= 1
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest(py_file)
|
|
|
|
assert result.ret == 0
|
|
|
|
# should not skip any tests
|
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestFunctional:
|
2012-11-06 04:52:12 +08:00
|
|
|
def test_merging_markers_deep(self, testdir):
|
|
|
|
# issue 199 - propagate markers into nested classes
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2012-11-06 04:52:12 +08:00
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestA(object):
|
2012-11-06 04:52:12 +08:00
|
|
|
pytestmark = pytest.mark.a
|
|
|
|
def test_b(self):
|
|
|
|
assert True
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestC(object):
|
2019-08-01 21:10:39 +08:00
|
|
|
# this one didn't get marked
|
2012-11-06 04:52:12 +08:00
|
|
|
def test_d(self):
|
|
|
|
assert True
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-06 04:52:12 +08:00
|
|
|
items, rec = testdir.inline_genitems(p)
|
|
|
|
for item in items:
|
2016-09-07 17:00:27 +08:00
|
|
|
print(item, item.keywords)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert [x for x in item.iter_markers() if x.name == "a"]
|
2012-11-06 04:52:12 +08:00
|
|
|
|
2015-07-16 08:03:30 +08:00
|
|
|
def test_mark_decorator_subclass_does_not_propagate_to_base(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2015-07-16 08:03:30 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.a
|
2017-02-17 02:41:51 +08:00
|
|
|
class Base(object): pass
|
2015-07-16 08:03:30 +08:00
|
|
|
|
|
|
|
@pytest.mark.b
|
|
|
|
class Test1(Base):
|
|
|
|
def test_foo(self): pass
|
|
|
|
|
|
|
|
class Test2(Base):
|
|
|
|
def test_bar(self): pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-16 08:03:30 +08:00
|
|
|
items, rec = testdir.inline_genitems(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
self.assert_markers(items, test_foo=("a", "b"), test_bar=("a",))
|
2015-07-16 08:03:30 +08:00
|
|
|
|
2016-06-27 17:57:21 +08:00
|
|
|
def test_mark_should_not_pass_to_siebling_class(self, testdir):
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#568"""
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2016-06-25 17:10:23 +08:00
|
|
|
import pytest
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestBase(object):
|
2016-06-25 17:10:23 +08:00
|
|
|
def test_foo(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.mark.b
|
|
|
|
class TestSub(TestBase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TestOtherSub(TestBase):
|
|
|
|
pass
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-06-25 17:10:23 +08:00
|
|
|
items, rec = testdir.inline_genitems(p)
|
|
|
|
base_item, sub_item, sub_item_other = items
|
2018-05-09 21:40:52 +08:00
|
|
|
print(items, [x.nodeid for x in items])
|
2019-11-01 11:28:25 +08:00
|
|
|
# new api segregates
|
2018-05-23 22:48:46 +08:00
|
|
|
assert not list(base_item.iter_markers(name="b"))
|
|
|
|
assert not list(sub_item_other.iter_markers(name="b"))
|
|
|
|
assert list(sub_item.iter_markers(name="b"))
|
2016-06-25 17:10:23 +08:00
|
|
|
|
2015-07-16 08:03:30 +08:00
|
|
|
def test_mark_decorator_baseclasses_merged(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2015-07-16 08:03:30 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.a
|
2017-02-17 02:41:51 +08:00
|
|
|
class Base(object): pass
|
2015-07-16 08:03:30 +08:00
|
|
|
|
|
|
|
@pytest.mark.b
|
|
|
|
class Base2(Base): pass
|
|
|
|
|
|
|
|
@pytest.mark.c
|
|
|
|
class Test1(Base2):
|
|
|
|
def test_foo(self): pass
|
|
|
|
|
|
|
|
class Test2(Base2):
|
|
|
|
@pytest.mark.d
|
|
|
|
def test_bar(self): pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-16 08:03:30 +08:00
|
|
|
items, rec = testdir.inline_genitems(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
self.assert_markers(items, test_foo=("a", "b", "c"), test_bar=("a", "b", "d"))
|
2015-07-16 08:03:30 +08:00
|
|
|
|
2018-05-09 21:40:52 +08:00
|
|
|
def test_mark_closest(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2018-05-09 21:40:52 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.c(location="class")
|
|
|
|
class Test:
|
|
|
|
@pytest.mark.c(location="function")
|
2020-04-09 00:11:04 +08:00
|
|
|
def test_has_own(self):
|
2018-05-09 21:40:52 +08:00
|
|
|
pass
|
|
|
|
|
2020-04-09 00:11:04 +08:00
|
|
|
def test_has_inherited(self):
|
2018-05-09 21:40:52 +08:00
|
|
|
pass
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-05-09 21:40:52 +08:00
|
|
|
items, rec = testdir.inline_genitems(p)
|
|
|
|
has_own, has_inherited = items
|
2018-05-23 22:48:46 +08:00
|
|
|
assert has_own.get_closest_marker("c").kwargs == {"location": "function"}
|
|
|
|
assert has_inherited.get_closest_marker("c").kwargs == {"location": "class"}
|
|
|
|
assert has_own.get_closest_marker("missing") is None
|
2018-05-09 21:40:52 +08:00
|
|
|
|
2011-12-28 23:47:19 +08:00
|
|
|
def test_mark_with_wrong_marker(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
reprec = testdir.inline_runsource(
|
|
|
|
"""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class pytestmark(object):
|
2010-10-26 05:08:56 +08:00
|
|
|
pass
|
|
|
|
def test_func():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getfailedcollections()
|
|
|
|
assert len(values) == 1
|
|
|
|
assert "TypeError" in str(values[0].longrepr)
|
2010-06-08 08:34:51 +08:00
|
|
|
|
|
|
|
def test_mark_dynamically_in_funcarg(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg(request):
|
2010-11-18 05:12:16 +08:00
|
|
|
request.applymarker(pytest.mark.hello)
|
2010-06-08 08:34:51 +08:00
|
|
|
def pytest_terminal_summary(terminalreporter):
|
2017-11-04 23:17:20 +08:00
|
|
|
values = terminalreporter.stats['passed']
|
2017-11-24 05:26:57 +08:00
|
|
|
terminalreporter._tw.line("keyword: %s" % values[0].keywords)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2010-06-08 08:34:51 +08:00
|
|
|
def test_func(arg):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-06-08 08:34:51 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["keyword: *hello*"])
|
2010-10-12 19:05:29 +08:00
|
|
|
|
2013-05-23 18:21:40 +08:00
|
|
|
def test_no_marker_match_on_unmarked_names(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2013-05-23 18:21:40 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.shouldmatch
|
|
|
|
def test_marked():
|
|
|
|
assert 1
|
|
|
|
|
|
|
|
def test_unmarked():
|
|
|
|
assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-05-23 18:21:40 +08:00
|
|
|
reprec = testdir.inline_run("-m", "test_unmarked", p)
|
|
|
|
passed, skipped, failed = reprec.listoutcomes()
|
|
|
|
assert len(passed) + len(skipped) + len(failed) == 0
|
|
|
|
dlist = reprec.getcalls("pytest_deselected")
|
|
|
|
deselected_tests = dlist[0].items
|
|
|
|
assert len(deselected_tests) == 2
|
|
|
|
|
2012-10-18 21:06:55 +08:00
|
|
|
def test_keywords_at_node_level(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2012-10-18 21:06:55 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def some(request):
|
|
|
|
request.keywords["hello"] = 42
|
|
|
|
assert "world" not in request.keywords
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
|
|
|
def funcsetup(request):
|
|
|
|
assert "world" in request.keywords
|
|
|
|
assert "hello" in request.keywords
|
|
|
|
|
|
|
|
@pytest.mark.world
|
|
|
|
def test_function():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-10-18 21:06:55 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
2010-10-12 19:05:29 +08:00
|
|
|
|
2013-10-03 21:43:56 +08:00
|
|
|
def test_keyword_added_for_session(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2013-10-03 21:43:56 +08:00
|
|
|
import pytest
|
|
|
|
def pytest_collection_modifyitems(session):
|
|
|
|
session.add_marker("mark1")
|
|
|
|
session.add_marker(pytest.mark.mark2)
|
|
|
|
session.add_marker(pytest.mark.mark3)
|
|
|
|
pytest.raises(ValueError, lambda:
|
|
|
|
session.add_marker(10))
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-10-03 21:43:56 +08:00
|
|
|
def test_some(request):
|
|
|
|
assert "mark1" in request.keywords
|
|
|
|
assert "mark2" in request.keywords
|
|
|
|
assert "mark3" in request.keywords
|
|
|
|
assert 10 not in request.keywords
|
2018-12-19 23:00:59 +08:00
|
|
|
marker = request.node.get_closest_marker("mark1")
|
2013-10-03 21:43:56 +08:00
|
|
|
assert marker.name == "mark1"
|
|
|
|
assert marker.args == ()
|
|
|
|
assert marker.kwargs == {}
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-06-30 23:56:27 +08:00
|
|
|
reprec = testdir.inline_run("-m", "mark1")
|
2013-10-03 21:43:56 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2015-07-16 08:03:30 +08:00
|
|
|
def assert_markers(self, items, **expected):
|
|
|
|
"""assert that given items have expected marker names applied to them.
|
|
|
|
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
|
|
|
|
to other modules.
|
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2018-05-18 05:31:16 +08:00
|
|
|
items = {x.name: x for x in items}
|
2015-07-16 08:03:30 +08:00
|
|
|
for name, expected_markers in expected.items():
|
2018-12-19 23:00:59 +08:00
|
|
|
markers = {m.name for m in items[name].iter_markers()}
|
|
|
|
assert markers == set(expected_markers)
|
2015-07-16 08:03:30 +08:00
|
|
|
|
2018-03-16 18:31:33 +08:00
|
|
|
@pytest.mark.filterwarnings("ignore")
|
2016-04-28 22:11:30 +08:00
|
|
|
def test_mark_from_parameters(self, testdir):
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#1540"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-04-28 22:11:30 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
pytestmark = pytest.mark.skipif(True, reason='skip all')
|
|
|
|
|
|
|
|
# skipifs inside fixture params
|
|
|
|
params = [pytest.mark.skipif(False, reason='dont skip')('parameter')]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(params=params)
|
|
|
|
def parameter(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
|
|
|
def test_1(parameter):
|
|
|
|
assert True
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2019-06-30 23:56:27 +08:00
|
|
|
reprec = testdir.inline_run()
|
2016-04-28 22:11:30 +08:00
|
|
|
reprec.assertoutcome(skipped=1)
|
2015-07-16 08:03:30 +08:00
|
|
|
|
2020-06-16 17:39:36 +08:00
|
|
|
def test_reevaluate_dynamic_expr(self, testdir):
|
|
|
|
"""#7360"""
|
|
|
|
py_file1 = testdir.makepyfile(
|
|
|
|
test_reevaluate_dynamic_expr1="""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
skip = True
|
|
|
|
|
|
|
|
@pytest.mark.skipif("skip")
|
|
|
|
def test_should_skip():
|
|
|
|
assert True
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
py_file2 = testdir.makepyfile(
|
|
|
|
test_reevaluate_dynamic_expr2="""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
skip = False
|
|
|
|
|
|
|
|
@pytest.mark.skipif("skip")
|
|
|
|
def test_should_not_skip():
|
|
|
|
assert True
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
file_name1 = os.path.basename(py_file1.strpath)
|
|
|
|
file_name2 = os.path.basename(py_file2.strpath)
|
|
|
|
reprec = testdir.inline_run(file_name1, file_name2)
|
|
|
|
reprec.assertoutcome(passed=1, skipped=1)
|
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestKeywordSelection:
|
2010-10-12 19:05:29 +08:00
|
|
|
def test_select_simple(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
file_test = testdir.makepyfile(
|
|
|
|
"""
|
2010-10-26 05:08:56 +08:00
|
|
|
def test_one():
|
|
|
|
assert 0
|
2010-10-12 19:05:29 +08:00
|
|
|
class TestClass(object):
|
|
|
|
def test_method_one(self):
|
|
|
|
assert 42 == 43
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-10-12 19:05:29 +08:00
|
|
|
def check(keyword, name):
|
|
|
|
reprec = testdir.inline_run("-s", "-k", keyword, file_test)
|
|
|
|
passed, skipped, failed = reprec.listoutcomes()
|
|
|
|
assert len(failed) == 1
|
|
|
|
assert failed[0].nodeid.split("::")[-1] == name
|
2018-05-23 22:48:46 +08:00
|
|
|
assert len(reprec.getcalls("pytest_deselected")) == 1
|
|
|
|
|
|
|
|
for keyword in ["test_one", "est_on"]:
|
|
|
|
check(keyword, "test_one")
|
|
|
|
check("TestClass and test", "test_method_one")
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"keyword",
|
|
|
|
[
|
|
|
|
"xxx",
|
|
|
|
"xxx and test_2",
|
|
|
|
"TestClass",
|
|
|
|
"xxx and not test_1",
|
|
|
|
"TestClass and test_2",
|
|
|
|
"xxx and TestClass and test_2",
|
|
|
|
],
|
|
|
|
)
|
2012-10-18 19:52:32 +08:00
|
|
|
def test_select_extra_keywords(self, testdir, keyword):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
test_select="""
|
2010-10-12 19:05:29 +08:00
|
|
|
def test_1():
|
|
|
|
pass
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2010-10-12 19:05:29 +08:00
|
|
|
def test_2(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
conftest="""
|
2014-10-09 02:23:40 +08:00
|
|
|
import pytest
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
2014-10-09 02:23:40 +08:00
|
|
|
def pytest_pycollect_makeitem(name):
|
|
|
|
outcome = yield
|
2010-10-14 00:41:53 +08:00
|
|
|
if name == "TestClass":
|
2014-10-09 02:23:40 +08:00
|
|
|
item = outcome.get_result()
|
2013-05-28 00:14:35 +08:00
|
|
|
item.extra_keyword_matches.add("xxx")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
reprec = testdir.inline_run(p.dirpath(), "-s", "-k", keyword)
|
2017-03-17 09:27:28 +08:00
|
|
|
print("keyword", repr(keyword))
|
2012-10-18 19:52:32 +08:00
|
|
|
passed, skipped, failed = reprec.listoutcomes()
|
|
|
|
assert len(passed) == 1
|
|
|
|
assert passed[0].nodeid.endswith("test_2")
|
|
|
|
dlist = reprec.getcalls("pytest_deselected")
|
|
|
|
assert len(dlist) == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
assert dlist[0].items[0].name == "test_1"
|
2010-10-12 19:05:29 +08:00
|
|
|
|
|
|
|
def test_select_starton(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
threepass = testdir.makepyfile(
|
|
|
|
test_threepass="""
|
2010-10-12 19:05:29 +08:00
|
|
|
def test_one(): assert 1
|
|
|
|
def test_two(): assert 1
|
|
|
|
def test_three(): assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-10-12 19:05:29 +08:00
|
|
|
reprec = testdir.inline_run("-k", "test_two:", threepass)
|
|
|
|
passed, skipped, failed = reprec.listoutcomes()
|
|
|
|
assert len(passed) == 2
|
|
|
|
assert not failed
|
|
|
|
dlist = reprec.getcalls("pytest_deselected")
|
|
|
|
assert len(dlist) == 1
|
|
|
|
item = dlist[0].items[0]
|
|
|
|
assert item.name == "test_one"
|
|
|
|
|
|
|
|
def test_keyword_extra(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2010-10-12 19:05:29 +08:00
|
|
|
def test_one():
|
|
|
|
assert 0
|
|
|
|
test_one.mykeyword = True
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-10-12 19:05:29 +08:00
|
|
|
reprec = testdir.inline_run("-k", "mykeyword", p)
|
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
|
|
|
assert failed == 1
|
2013-05-23 15:12:50 +08:00
|
|
|
|
2013-07-25 21:33:43 +08:00
|
|
|
@pytest.mark.xfail
|
|
|
|
def test_keyword_extra_dash(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2013-07-25 21:33:43 +08:00
|
|
|
def test_one():
|
|
|
|
assert 0
|
|
|
|
test_one.mykeyword = True
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-07-25 21:33:43 +08:00
|
|
|
# with argparse the argument to an option cannot
|
|
|
|
# start with '-'
|
|
|
|
reprec = testdir.inline_run("-k", "-mykeyword", p)
|
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
|
|
|
assert passed + skipped + failed == 0
|
|
|
|
|
2020-04-25 18:38:53 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"keyword", ["__", "+", ".."],
|
|
|
|
)
|
|
|
|
def test_no_magic_values(self, testdir, keyword: str) -> None:
|
2013-05-23 15:12:50 +08:00
|
|
|
"""Make sure the tests do not match on magic values,
|
2020-04-25 18:38:53 +08:00
|
|
|
no double underscored values, like '__dict__' and '+'.
|
2013-05-23 15:12:50 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2013-05-23 15:12:50 +08:00
|
|
|
def test_one(): assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-04-25 18:38:53 +08:00
|
|
|
reprec = testdir.inline_run("-k", keyword, p)
|
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
|
|
|
dlist = reprec.getcalls("pytest_deselected")
|
|
|
|
assert passed + skipped + failed == 0
|
|
|
|
deselected_tests = dlist[0].items
|
|
|
|
assert len(deselected_tests) == 1
|
2016-09-07 17:00:27 +08:00
|
|
|
|
2020-05-17 02:01:26 +08:00
|
|
|
def test_no_match_directories_outside_the_suite(self, testdir):
|
|
|
|
"""
|
|
|
|
-k should not match against directories containing the test suite (#7040).
|
|
|
|
"""
|
|
|
|
test_contents = """
|
|
|
|
def test_aaa(): pass
|
|
|
|
def test_ddd(): pass
|
|
|
|
"""
|
|
|
|
testdir.makepyfile(
|
|
|
|
**{"ddd/tests/__init__.py": "", "ddd/tests/test_foo.py": test_contents}
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_collected_names(*args):
|
|
|
|
_, rec = testdir.inline_genitems(*args)
|
|
|
|
calls = rec.getcalls("pytest_collection_finish")
|
|
|
|
assert len(calls) == 1
|
|
|
|
return [x.name for x in calls[0].session.items]
|
|
|
|
|
|
|
|
# sanity check: collect both tests in normal runs
|
|
|
|
assert get_collected_names() == ["test_aaa", "test_ddd"]
|
|
|
|
|
|
|
|
# do not collect anything based on names outside the collection tree
|
|
|
|
assert get_collected_names("-k", testdir.tmpdir.basename) == []
|
|
|
|
|
|
|
|
# "-k ddd" should only collect "test_ddd", but not
|
|
|
|
# 'test_aaa' just because one of its parent directories is named "ddd";
|
|
|
|
# this was matched previously because Package.name would contain the full path
|
|
|
|
# to the package
|
|
|
|
assert get_collected_names("-k", "ddd") == ["test_ddd"]
|
|
|
|
|
2016-09-07 17:00:27 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestMarkDecorator:
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"lhs, rhs, expected",
|
|
|
|
[
|
|
|
|
(pytest.mark.foo(), pytest.mark.foo(), True),
|
|
|
|
(pytest.mark.foo(), pytest.mark.bar(), False),
|
|
|
|
(pytest.mark.foo(), "bar", False),
|
|
|
|
("foo", pytest.mark.bar(), False),
|
|
|
|
],
|
|
|
|
)
|
2017-09-09 13:20:56 +08:00
|
|
|
def test__eq__(self, lhs, rhs, expected):
|
|
|
|
assert (lhs == rhs) == expected
|
2017-12-17 19:49:12 +08:00
|
|
|
|
2019-11-08 06:55:23 +08:00
|
|
|
def test_aliases(self) -> None:
|
|
|
|
md = pytest.mark.foo(1, "2", three=3)
|
|
|
|
assert md.name == "foo"
|
|
|
|
assert md.args == (1, "2")
|
|
|
|
assert md.kwargs == {"three": 3}
|
|
|
|
|
2017-12-17 19:49:12 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("mark", [None, "", "skip", "xfail"])
|
2017-12-17 19:49:12 +08:00
|
|
|
def test_parameterset_for_parametrize_marks(testdir, mark):
|
|
|
|
if mark is not None:
|
2018-09-12 18:04:45 +08:00
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
{}={}
|
|
|
|
""".format(
|
|
|
|
EMPTY_PARAMETERSET_OPTION, mark
|
|
|
|
)
|
|
|
|
)
|
2017-12-17 19:49:12 +08:00
|
|
|
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
from _pytest.mark import pytest_configure, get_empty_parameterset_mark
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2017-12-17 19:49:12 +08:00
|
|
|
pytest_configure(config)
|
2018-05-23 22:48:46 +08:00
|
|
|
result_mark = get_empty_parameterset_mark(config, ["a"], all)
|
|
|
|
if mark in (None, ""):
|
2017-12-17 19:49:12 +08:00
|
|
|
# normalize to the requested name
|
2018-05-23 22:48:46 +08:00
|
|
|
mark = "skip"
|
2017-12-17 19:49:12 +08:00
|
|
|
assert result_mark.name == mark
|
2018-05-23 22:48:46 +08:00
|
|
|
assert result_mark.kwargs["reason"].startswith("got empty parameter set ")
|
|
|
|
if mark == "xfail":
|
|
|
|
assert result_mark.kwargs.get("run") is False
|
2017-12-17 19:49:12 +08:00
|
|
|
|
|
|
|
|
2018-09-12 18:04:45 +08:00
|
|
|
def test_parameterset_for_fail_at_collect(testdir):
|
|
|
|
testdir.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
{}=fail_at_collect
|
|
|
|
""".format(
|
|
|
|
EMPTY_PARAMETERSET_OPTION
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
config = testdir.parseconfig()
|
|
|
|
from _pytest.mark import pytest_configure, get_empty_parameterset_mark
|
|
|
|
|
|
|
|
pytest_configure(config)
|
|
|
|
|
2019-03-26 23:37:26 +08:00
|
|
|
with pytest.raises(
|
|
|
|
Collector.CollectError,
|
|
|
|
match=r"Empty parameter set in 'pytest_configure' at line \d\d+",
|
|
|
|
):
|
|
|
|
get_empty_parameterset_mark(config, ["a"], pytest_configure)
|
|
|
|
|
|
|
|
p1 = testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
2018-09-12 18:04:45 +08:00
|
|
|
|
2019-03-26 23:37:26 +08:00
|
|
|
@pytest.mark.parametrize("empty", [])
|
|
|
|
def test():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest(str(p1))
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2019-10-27 23:02:37 +08:00
|
|
|
"collected 0 items / 1 error",
|
2019-03-26 23:37:26 +08:00
|
|
|
"* ERROR collecting test_parameterset_for_fail_at_collect.py *",
|
|
|
|
"Empty parameter set in 'test' at line 3",
|
|
|
|
"*= 1 error in *",
|
|
|
|
]
|
|
|
|
)
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.INTERRUPTED
|
2018-09-12 18:04:45 +08:00
|
|
|
|
|
|
|
|
2017-12-17 19:49:12 +08:00
|
|
|
def test_parameterset_for_parametrize_bad_markname(testdir):
|
|
|
|
with pytest.raises(pytest.UsageError):
|
2018-05-23 22:48:46 +08:00
|
|
|
test_parameterset_for_parametrize_marks(testdir, "bad")
|
2018-05-03 23:01:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_mark_expressions_no_smear(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2018-05-03 23:01:47 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
class BaseTests(object):
|
|
|
|
def test_something(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.mark.FOO
|
|
|
|
class TestFooClass(BaseTests):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.mark.BAR
|
|
|
|
class TestBarClass(BaseTests):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-05-03 23:01:47 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
reprec = testdir.inline_run("-m", "FOO")
|
2018-05-03 23:01:47 +08:00
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
|
|
|
dlist = reprec.getcalls("pytest_deselected")
|
|
|
|
assert passed == 1
|
|
|
|
assert skipped == failed == 0
|
|
|
|
deselected_tests = dlist[0].items
|
|
|
|
assert len(deselected_tests) == 1
|
|
|
|
|
2018-12-19 23:00:59 +08:00
|
|
|
# todo: fixed
|
2018-05-03 23:01:47 +08:00
|
|
|
# keywords smear - expected behaviour
|
2018-12-19 23:00:59 +08:00
|
|
|
# reprec_keywords = testdir.inline_run("-k", "FOO")
|
|
|
|
# passed_k, skipped_k, failed_k = reprec_keywords.countoutcomes()
|
|
|
|
# assert passed_k == 2
|
|
|
|
# assert skipped_k == failed_k == 0
|
2018-06-12 20:49:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_addmarker_order():
|
2019-10-17 03:52:04 +08:00
|
|
|
session = mock.Mock()
|
|
|
|
session.own_markers = []
|
|
|
|
session.parent = None
|
|
|
|
session.nodeid = ""
|
|
|
|
node = Node.from_parent(session, name="Test")
|
2019-04-27 22:43:36 +08:00
|
|
|
node.add_marker("foo")
|
|
|
|
node.add_marker("bar")
|
|
|
|
node.add_marker("baz", append=False)
|
2018-06-12 20:49:54 +08:00
|
|
|
extracted = [x.name for x in node.iter_markers()]
|
2019-04-27 22:43:36 +08:00
|
|
|
assert extracted == ["baz", "foo", "bar"]
|
2018-06-28 23:32:41 +08:00
|
|
|
|
|
|
|
|
2018-06-21 04:44:19 +08:00
|
|
|
@pytest.mark.filterwarnings("ignore")
|
2018-06-21 04:36:04 +08:00
|
|
|
def test_markers_from_parametrize(testdir):
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#3605"""
|
2018-06-21 04:36:04 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
first_custom_mark = pytest.mark.custom_marker
|
|
|
|
custom_mark = pytest.mark.custom_mark
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def trigger(request):
|
2018-12-19 23:00:59 +08:00
|
|
|
custom_mark = list(request.node.iter_markers('custom_mark'))
|
2018-06-21 04:36:04 +08:00
|
|
|
print("Custom mark %s" % custom_mark)
|
|
|
|
|
|
|
|
@custom_mark("custom mark non parametrized")
|
|
|
|
def test_custom_mark_non_parametrized():
|
|
|
|
print("Hey from test")
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"obj_type",
|
|
|
|
[
|
|
|
|
first_custom_mark("first custom mark")("template"),
|
|
|
|
pytest.param( # Think this should be recommended way?
|
|
|
|
"disk",
|
|
|
|
marks=custom_mark('custom mark1')
|
|
|
|
),
|
|
|
|
custom_mark("custom mark2")("vm"), # Tried also this
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_custom_mark_parametrized(obj_type):
|
|
|
|
print("obj_type is:", obj_type)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2019-06-30 23:56:27 +08:00
|
|
|
result = testdir.runpytest()
|
2018-06-21 14:00:23 +08:00
|
|
|
result.assert_outcomes(passed=4)
|
2018-11-19 06:16:13 +08:00
|
|
|
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_pytest_param_id_requires_string() -> None:
|
2018-11-19 06:16:13 +08:00
|
|
|
with pytest.raises(TypeError) as excinfo:
|
2020-07-10 14:44:14 +08:00
|
|
|
pytest.param(id=True) # type: ignore[arg-type]
|
2019-11-17 01:53:29 +08:00
|
|
|
(msg,) = excinfo.value.args
|
2019-05-28 07:31:52 +08:00
|
|
|
assert msg == "Expected id to be a string, got <class 'bool'>: True"
|
2018-11-19 06:16:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("s", (None, "hello world"))
|
|
|
|
def test_pytest_param_id_allows_none_or_string(s):
|
|
|
|
assert pytest.param(id=s)
|
2020-04-13 20:25:06 +08:00
|
|
|
|
|
|
|
|
2020-04-13 21:25:01 +08:00
|
|
|
@pytest.mark.parametrize("expr", ("NOT internal_err", "NOT (internal_err)", "bogus/"))
|
|
|
|
def test_marker_expr_eval_failure_handling(testdir, expr):
|
2020-04-13 20:25:06 +08:00
|
|
|
foo = testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.internal_err
|
|
|
|
def test_foo():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-04-25 18:38:53 +08:00
|
|
|
expected = "ERROR: Wrong expression passed to '-m': {}: *".format(expr)
|
2020-04-13 21:25:01 +08:00
|
|
|
result = testdir.runpytest(foo, "-m", expr)
|
2020-04-13 20:25:06 +08:00
|
|
|
result.stderr.fnmatch_lines([expected])
|
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|