2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2015-07-25 22:28:18 +08:00
|
|
|
import os
|
2016-09-07 17:00:27 +08:00
|
|
|
import sys
|
2015-07-25 22:28:18 +08:00
|
|
|
|
2016-09-07 17:00:27 +08:00
|
|
|
import pytest
|
2018-01-26 19:12:26 +08:00
|
|
|
from _pytest.mark import (
|
|
|
|
MarkGenerator as Mark, ParameterSet, transfer_markers,
|
|
|
|
EMPTY_PARAMETERSET_OPTION,
|
|
|
|
)
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestMark(object):
|
2011-06-01 14:03:06 +08:00
|
|
|
def test_markinfo_repr(self):
|
2016-09-08 15:52:22 +08:00
|
|
|
from _pytest.mark import MarkInfo, Mark
|
2017-07-17 07:25:08 +08:00
|
|
|
m = MarkInfo(Mark("hello", (1, 2), {}))
|
2011-06-01 14:03:06 +08:00
|
|
|
repr(m)
|
|
|
|
|
2016-09-07 17:00:27 +08:00
|
|
|
@pytest.mark.parametrize('attr', ['mark', 'param'])
|
|
|
|
@pytest.mark.parametrize('modulename', ['py.test', 'pytest'])
|
|
|
|
def test_pytest_exists_in_namespace_all(self, attr, modulename):
|
|
|
|
module = sys.modules[modulename]
|
|
|
|
assert attr in module.__all__
|
2010-11-18 21:56:16 +08:00
|
|
|
|
2009-10-22 21:21:58 +08:00
|
|
|
def test_pytest_mark_notcallable(self):
|
|
|
|
mark = Mark()
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises((AttributeError, TypeError), mark)
|
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
|
|
|
|
|
|
|
|
class SomeClass(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert pytest.mark.fun(some_function) is some_function
|
|
|
|
assert pytest.mark.fun.with_args(some_function) is not some_function
|
|
|
|
|
|
|
|
assert pytest.mark.fun(SomeClass) is SomeClass
|
|
|
|
assert pytest.mark.fun.with_args(SomeClass) is not SomeClass
|
|
|
|
|
2013-12-19 21:29:57 +08:00
|
|
|
def test_pytest_mark_name_starts_with_underscore(self):
|
|
|
|
mark = Mark()
|
|
|
|
pytest.raises(AttributeError, getattr, mark, '_some_name')
|
|
|
|
|
2009-10-22 21:21:58 +08:00
|
|
|
def test_pytest_mark_bare(self):
|
|
|
|
mark = Mark()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-10-26 05:08:56 +08:00
|
|
|
def f():
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2009-10-22 21:21:58 +08:00
|
|
|
mark.hello(f)
|
|
|
|
assert f.hello
|
|
|
|
|
|
|
|
def test_pytest_mark_keywords(self):
|
|
|
|
mark = Mark()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-10-26 05:08:56 +08:00
|
|
|
def f():
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2009-10-22 21:21:58 +08:00
|
|
|
mark.world(x=3, y=4)(f)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert f.world
|
2010-10-14 00:41:53 +08:00
|
|
|
assert f.world.kwargs['x'] == 3
|
|
|
|
assert f.world.kwargs['y'] == 4
|
2009-10-22 21:21:58 +08:00
|
|
|
|
|
|
|
def test_apply_multiple_and_merge(self):
|
|
|
|
mark = Mark()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-10-26 05:08:56 +08:00
|
|
|
def f():
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-10-12 21:39:22 +08:00
|
|
|
mark.world
|
2009-10-22 21:21:58 +08:00
|
|
|
mark.world(x=3)(f)
|
2010-10-14 00:41:53 +08:00
|
|
|
assert f.world.kwargs['x'] == 3
|
2009-10-22 21:21:58 +08:00
|
|
|
mark.world(y=4)(f)
|
2010-10-14 00:41:53 +08:00
|
|
|
assert f.world.kwargs['x'] == 3
|
|
|
|
assert f.world.kwargs['y'] == 4
|
2009-10-22 21:21:58 +08:00
|
|
|
mark.world(y=1)(f)
|
2010-10-14 00:41:53 +08:00
|
|
|
assert f.world.kwargs['y'] == 1
|
2009-10-23 02:57:21 +08:00
|
|
|
assert len(f.world.args) == 0
|
2009-10-22 21:21:58 +08:00
|
|
|
|
|
|
|
def test_pytest_mark_positional(self):
|
|
|
|
mark = Mark()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-10-26 05:08:56 +08:00
|
|
|
def f():
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2009-10-22 21:21:58 +08:00
|
|
|
mark.world("hello")(f)
|
2009-10-23 02:57:21 +08:00
|
|
|
assert f.world.args[0] == "hello"
|
2009-10-22 21:21:58 +08:00
|
|
|
mark.world("world")(f)
|
|
|
|
|
2014-01-20 08:27:33 +08:00
|
|
|
def test_pytest_mark_positional_func_and_keyword(self):
|
|
|
|
mark = Mark()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-01-20 08:27:33 +08:00
|
|
|
def f():
|
|
|
|
raise Exception
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-01-20 08:27:33 +08:00
|
|
|
m = mark.world(f, omega="hello")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-01-20 08:27:33 +08:00
|
|
|
def g():
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-01-20 08:27:33 +08:00
|
|
|
assert m(g) == g
|
|
|
|
assert g.world.args[0] is f
|
|
|
|
assert g.world.kwargs["omega"] == "hello"
|
|
|
|
|
2010-11-21 03:17:38 +08:00
|
|
|
def test_pytest_mark_reuse(self):
|
|
|
|
mark = Mark()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-11-21 03:17:38 +08:00
|
|
|
def f():
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-11-21 03:17:38 +08:00
|
|
|
w = mark.some
|
|
|
|
w("hello", reason="123")(f)
|
|
|
|
assert f.some.args[0] == "hello"
|
|
|
|
assert f.some.kwargs['reason'] == "123"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-11-21 03:17:38 +08:00
|
|
|
def g():
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-11-21 03:17:38 +08:00
|
|
|
w("world", reason2="456")(g)
|
|
|
|
assert g.some.args[0] == "world"
|
|
|
|
assert 'reason' not in g.some.kwargs
|
|
|
|
assert g.some.kwargs['reason2'] == "456"
|
|
|
|
|
2011-11-12 06:56:11 +08:00
|
|
|
|
2015-07-25 22:28:18 +08:00
|
|
|
def test_marked_class_run_twice(testdir, request):
|
|
|
|
"""Test fails file is run twice that contains marked class.
|
|
|
|
See issue#683.
|
|
|
|
"""
|
|
|
|
py_file = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('abc', [1, 2, 3])
|
|
|
|
class Test1(object):
|
|
|
|
def test_1(self, abc):
|
|
|
|
assert abc in [1, 2, 3]
|
|
|
|
""")
|
|
|
|
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):
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
markers =
|
|
|
|
a1: this is a webtest marker
|
|
|
|
a2: this is a smoke marker
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_markers(pytestconfig):
|
|
|
|
markers = pytestconfig.getini("markers")
|
|
|
|
print (markers)
|
|
|
|
assert len(markers) >= 2
|
|
|
|
assert markers[0].startswith("a1:")
|
|
|
|
assert markers[1].startswith("a2:")
|
|
|
|
""")
|
|
|
|
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):
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
markers =
|
|
|
|
a1: this is a webtest marker
|
|
|
|
a1some: another marker
|
2017-11-22 20:47:15 +08:00
|
|
|
nodescription
|
2011-11-12 06:56:11 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--markers", )
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*a1*this is a webtest*",
|
|
|
|
"*a1some*another marker",
|
2017-11-22 20:47:15 +08:00
|
|
|
"*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):
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
markers =
|
|
|
|
a1 : this is a whitespace marker
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.a1
|
|
|
|
def test_markers():
|
|
|
|
assert True
|
|
|
|
""")
|
|
|
|
rec = testdir.inline_run("--strict", "-m", "a1")
|
|
|
|
rec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
2017-11-22 20:47:15 +08:00
|
|
|
def test_marker_without_description(testdir):
|
|
|
|
testdir.makefile(".cfg", setup="""
|
|
|
|
[tool:pytest]
|
|
|
|
markers=slow
|
|
|
|
""")
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
pytest.mark.xfail('FAIL')
|
|
|
|
""")
|
|
|
|
ftdir = testdir.mkdir("ft1_dummy")
|
|
|
|
testdir.tmpdir.join("conftest.py").move(ftdir.join("conftest.py"))
|
|
|
|
rec = testdir.runpytest_subprocess("--strict")
|
|
|
|
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"')
|
|
|
|
testdir.makepyfile(flip_flop="""\
|
|
|
|
def pytest_configure(config):
|
|
|
|
config.addinivalue_line("markers", "flip:flop")
|
|
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
try:
|
|
|
|
mark = metafunc.function.flipper
|
|
|
|
except AttributeError:
|
|
|
|
return
|
|
|
|
metafunc.parametrize("x", (10, 20))""")
|
|
|
|
testdir.makepyfile("""\
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.flipper
|
|
|
|
def test_example(x):
|
|
|
|
assert x""")
|
|
|
|
|
|
|
|
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):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.r(lambda x: 0/0)
|
|
|
|
def test_hello():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
2011-11-12 06:56:11 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2011-11-12 06:56:11 +08:00
|
|
|
def test_strict_prohibits_unregistered_markers(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.unregisteredmark
|
|
|
|
def test_hello():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--strict")
|
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*unregisteredmark*not*registered*",
|
|
|
|
])
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-11-21 21:40:14 +08:00
|
|
|
@pytest.mark.parametrize("spec", [
|
2017-07-17 07:25:07 +08:00
|
|
|
("xyz", ("test_one",)),
|
2017-07-17 07:25:07 +08:00
|
|
|
("xyz and xyz2", ()),
|
|
|
|
("xyz2", ("test_two",)),
|
|
|
|
("xyz or xyz2", ("test_one", "test_two"),)
|
2011-11-12 07:02:06 +08:00
|
|
|
])
|
|
|
|
def test_mark_option(spec, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.xyz
|
|
|
|
def test_one():
|
|
|
|
pass
|
|
|
|
@pytest.mark.xyz2
|
|
|
|
def test_two():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
opt, passed_result = spec
|
|
|
|
rec = testdir.inline_run("-m", opt)
|
|
|
|
passed, skipped, fail = rec.listoutcomes()
|
|
|
|
passed = [x.nodeid.split("::")[-1] for x in passed]
|
|
|
|
assert len(passed) == len(passed_result)
|
|
|
|
assert list(passed) == list(passed_result)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-11-21 21:40:14 +08:00
|
|
|
@pytest.mark.parametrize("spec", [
|
2017-07-17 07:25:07 +08:00
|
|
|
("interface", ("test_interface",)),
|
2017-07-17 07:25:07 +08:00
|
|
|
("not interface", ("test_nointer",)),
|
2012-11-09 19:07:41 +08:00
|
|
|
])
|
|
|
|
def test_mark_option_custom(spec, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
def pytest_collection_modifyitems(items):
|
|
|
|
for item in items:
|
|
|
|
if "interface" in item.nodeid:
|
|
|
|
item.keywords["interface"] = pytest.mark.interface
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_interface():
|
|
|
|
pass
|
|
|
|
def test_nointer():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
opt, passed_result = spec
|
|
|
|
rec = testdir.inline_run("-m", opt)
|
|
|
|
passed, skipped, fail = rec.listoutcomes()
|
|
|
|
passed = [x.nodeid.split("::")[-1] for x in passed]
|
|
|
|
assert len(passed) == len(passed_result)
|
|
|
|
assert list(passed) == list(passed_result)
|
2011-11-12 07:02:06 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2013-11-21 21:40:14 +08:00
|
|
|
@pytest.mark.parametrize("spec", [
|
2017-07-17 07:25:07 +08:00
|
|
|
("interface", ("test_interface",)),
|
2017-07-17 07:25:07 +08:00
|
|
|
("not interface", ("test_nointer", "test_pass")),
|
|
|
|
("pass", ("test_pass",)),
|
|
|
|
("not pass", ("test_interface", "test_nointer")),
|
2012-11-09 19:29:33 +08:00
|
|
|
])
|
|
|
|
def test_keyword_option_custom(spec, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_interface():
|
|
|
|
pass
|
|
|
|
def test_nointer():
|
|
|
|
pass
|
2013-11-21 22:25:16 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
2012-11-09 19:29:33 +08:00
|
|
|
""")
|
|
|
|
opt, passed_result = spec
|
|
|
|
rec = testdir.inline_run("-k", opt)
|
|
|
|
passed, skipped, fail = rec.listoutcomes()
|
|
|
|
passed = [x.nodeid.split("::")[-1] for x in passed]
|
|
|
|
assert len(passed) == len(passed_result)
|
|
|
|
assert list(passed) == list(passed_result)
|
|
|
|
|
2013-10-03 21:43:56 +08:00
|
|
|
|
2013-11-21 22:25:16 +08:00
|
|
|
@pytest.mark.parametrize("spec", [
|
2017-07-17 07:25:07 +08:00
|
|
|
("None", ("test_func[None]",)),
|
2017-07-17 07:25:07 +08:00
|
|
|
("1.3", ("test_func[1.3]",)),
|
|
|
|
("2-3", ("test_func[2-3]",))
|
2013-11-21 22:25:16 +08:00
|
|
|
])
|
|
|
|
def test_keyword_option_parametrize(spec, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
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
|
|
|
|
""")
|
|
|
|
opt, passed_result = spec
|
|
|
|
rec = testdir.inline_run("-k", opt)
|
|
|
|
passed, skipped, fail = rec.listoutcomes()
|
|
|
|
passed = [x.nodeid.split("::")[-1] for x in passed]
|
|
|
|
assert len(passed) == len(passed_result)
|
|
|
|
assert list(passed) == list(passed_result)
|
|
|
|
|
2016-03-14 05:37:21 +08:00
|
|
|
|
2018-01-17 02:30:44 +08:00
|
|
|
@pytest.mark.parametrize("spec", [
|
2018-01-17 04:35:57 +08:00
|
|
|
("foo or import", "ERROR: Python keyword 'import' not accepted in expressions passed to '-k'"),
|
|
|
|
("foo or", "ERROR: Wrong expression passed to '-k': foo or")
|
2018-01-17 02:30:44 +08:00
|
|
|
])
|
|
|
|
def test_keyword_option_wrong_arguments(spec, testdir, capsys):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_func(arg):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
opt, expected_result = spec
|
|
|
|
testdir.inline_run("-k", opt)
|
2018-01-17 04:35:57 +08:00
|
|
|
out = capsys.readouterr().err
|
2018-01-17 02:30:44 +08:00
|
|
|
assert expected_result in out
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
py_file = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize("arg", [None, 1.3, "2-3"])
|
|
|
|
def test_func(arg):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
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."""
|
|
|
|
py_file = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('foo, bar', [(1, 2, 3)])
|
|
|
|
def test_func(foo, bar):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
|
|
|
|
result = testdir.runpytest(py_file)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
'E ValueError: In "parametrize" the number of values ((1, 2, 3)) '
|
|
|
|
'must be equal to the number of names ([\'foo\', \'bar\'])'
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2017-10-09 00:02:54 +08:00
|
|
|
def test_parametrized_with_kwargs(testdir):
|
|
|
|
"""Test collect parametrized func with wrong number of args."""
|
|
|
|
py_file = testdir.makepyfile("""
|
|
|
|
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
|
|
|
|
""")
|
|
|
|
|
|
|
|
result = testdir.runpytest(py_file)
|
|
|
|
assert(result.ret == 0)
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestFunctional(object):
|
2011-11-12 06:56:11 +08:00
|
|
|
|
2009-10-22 21:21:58 +08:00
|
|
|
def test_mark_per_function(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.hello
|
2009-10-22 21:21:58 +08:00
|
|
|
def test_hello():
|
|
|
|
assert hasattr(test_hello, 'hello')
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
2011-11-09 01:53:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2009-10-22 21:21:58 +08:00
|
|
|
|
|
|
|
def test_mark_per_module(self, testdir):
|
|
|
|
item = testdir.getitem("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
pytestmark = pytest.mark.hello
|
2009-10-22 21:21:58 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2010-06-08 08:34:51 +08:00
|
|
|
keywords = item.keywords
|
2009-10-22 21:21:58 +08:00
|
|
|
assert 'hello' in keywords
|
|
|
|
|
2010-05-22 00:11:47 +08:00
|
|
|
def test_marklist_per_class(self, testdir):
|
2010-10-26 05:08:56 +08:00
|
|
|
item = testdir.getitem("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2010-11-18 05:12:16 +08:00
|
|
|
pytestmark = [pytest.mark.hello, pytest.mark.world]
|
2009-10-22 21:21:58 +08:00
|
|
|
def test_func(self):
|
2010-07-27 03:15:15 +08:00
|
|
|
assert TestClass.test_func.hello
|
2010-05-22 00:11:47 +08:00
|
|
|
assert TestClass.test_func.world
|
2009-10-22 21:21:58 +08:00
|
|
|
""")
|
2010-06-08 08:34:51 +08:00
|
|
|
keywords = item.keywords
|
2009-10-22 21:21:58 +08:00
|
|
|
assert 'hello' in keywords
|
|
|
|
|
2010-05-22 00:11:47 +08:00
|
|
|
def test_marklist_per_module(self, testdir):
|
2010-10-26 05:08:56 +08:00
|
|
|
item = testdir.getitem("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
pytestmark = [pytest.mark.hello, pytest.mark.world]
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2010-05-22 00:11:47 +08:00
|
|
|
def test_func(self):
|
2010-07-27 03:15:15 +08:00
|
|
|
assert TestClass.test_func.hello
|
2010-05-22 00:11:47 +08:00
|
|
|
assert TestClass.test_func.world
|
|
|
|
""")
|
2010-06-08 08:34:51 +08:00
|
|
|
keywords = item.keywords
|
2010-05-22 00:11:47 +08:00
|
|
|
assert 'hello' in keywords
|
|
|
|
assert 'world' in keywords
|
|
|
|
|
|
|
|
def test_mark_per_class_decorator(self, testdir):
|
2010-10-26 05:08:56 +08:00
|
|
|
item = testdir.getitem("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.hello
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2010-05-22 00:11:47 +08:00
|
|
|
def test_func(self):
|
2010-07-27 03:15:15 +08:00
|
|
|
assert TestClass.test_func.hello
|
2010-05-22 00:11:47 +08:00
|
|
|
""")
|
2010-06-08 08:34:51 +08:00
|
|
|
keywords = item.keywords
|
2010-05-22 00:11:47 +08:00
|
|
|
assert 'hello' in keywords
|
|
|
|
|
|
|
|
def test_mark_per_class_decorator_plus_existing_dec(self, testdir):
|
2010-10-26 05:08:56 +08:00
|
|
|
item = testdir.getitem("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.hello
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2010-11-18 05:12:16 +08:00
|
|
|
pytestmark = pytest.mark.world
|
2010-05-22 00:11:47 +08:00
|
|
|
def test_func(self):
|
2010-07-27 03:15:15 +08:00
|
|
|
assert TestClass.test_func.hello
|
2010-05-22 00:11:47 +08:00
|
|
|
assert TestClass.test_func.world
|
|
|
|
""")
|
2010-06-08 08:34:51 +08:00
|
|
|
keywords = item.keywords
|
2010-05-22 00:11:47 +08:00
|
|
|
assert 'hello' in keywords
|
|
|
|
assert 'world' in keywords
|
|
|
|
|
2009-10-22 21:21:58 +08:00
|
|
|
def test_merging_markers(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
pytestmark = pytest.mark.hello("pos1", x=1, y=2)
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2009-10-22 21:21:58 +08:00
|
|
|
# classlevel overrides module level
|
2010-11-18 05:12:16 +08:00
|
|
|
pytestmark = pytest.mark.hello(x=3)
|
|
|
|
@pytest.mark.hello("pos0", z=4)
|
2009-10-22 21:21:58 +08:00
|
|
|
def test_func(self):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
items, rec = testdir.inline_genitems(p)
|
|
|
|
item, = items
|
2010-06-08 08:34:51 +08:00
|
|
|
keywords = item.keywords
|
2009-10-22 21:21:58 +08:00
|
|
|
marker = keywords['hello']
|
2010-11-21 03:17:38 +08:00
|
|
|
assert marker.args == ("pos0", "pos1")
|
2011-12-28 23:47:19 +08:00
|
|
|
assert marker.kwargs == {'x': 1, 'y': 2, 'z': 4}
|
2009-10-22 21:21:58 +08:00
|
|
|
|
2011-12-28 23:47:18 +08:00
|
|
|
# test the new __iter__ interface
|
2017-11-04 23:17:20 +08:00
|
|
|
values = list(marker)
|
|
|
|
assert len(values) == 3
|
|
|
|
assert values[0].args == ("pos0",)
|
|
|
|
assert values[1].args == ()
|
|
|
|
assert values[2].args == ("pos1", )
|
2011-12-28 23:47:18 +08:00
|
|
|
|
2012-11-06 04:52:12 +08:00
|
|
|
@pytest.mark.xfail(reason='unfixed')
|
|
|
|
def test_merging_markers_deep(self, testdir):
|
|
|
|
# issue 199 - propagate markers into nested classes
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
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):
|
2012-11-06 04:52:12 +08:00
|
|
|
# this one didnt get marked
|
|
|
|
def test_d(self):
|
|
|
|
assert True
|
|
|
|
""")
|
|
|
|
items, rec = testdir.inline_genitems(p)
|
|
|
|
for item in items:
|
2016-09-07 17:00:27 +08:00
|
|
|
print(item, item.keywords)
|
2012-11-06 04:52:12 +08:00
|
|
|
assert 'a' in item.keywords
|
|
|
|
|
2015-07-16 08:03:30 +08:00
|
|
|
def test_mark_decorator_subclass_does_not_propagate_to_base(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
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
|
|
|
|
""")
|
|
|
|
items, rec = testdir.inline_genitems(p)
|
|
|
|
self.assert_markers(items, test_foo=('a', 'b'), test_bar=('a',))
|
|
|
|
|
2016-06-25 17:10:23 +08:00
|
|
|
@pytest.mark.issue568
|
2016-06-27 17:57:21 +08:00
|
|
|
@pytest.mark.xfail(reason="markers smear on methods of base classes")
|
|
|
|
def test_mark_should_not_pass_to_siebling_class(self, testdir):
|
2016-06-25 17:10:23 +08:00
|
|
|
p = testdir.makepyfile("""
|
|
|
|
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
|
|
|
|
|
|
|
|
""")
|
|
|
|
items, rec = testdir.inline_genitems(p)
|
|
|
|
base_item, sub_item, sub_item_other = items
|
|
|
|
assert not hasattr(base_item.obj, 'b')
|
|
|
|
assert not hasattr(sub_item_other.obj, 'b')
|
|
|
|
|
2015-07-16 08:03:30 +08:00
|
|
|
def test_mark_decorator_baseclasses_merged(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
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
|
|
|
|
""")
|
|
|
|
items, rec = testdir.inline_genitems(p)
|
|
|
|
self.assert_markers(items, test_foo=('a', 'b', 'c'),
|
|
|
|
test_bar=('a', 'b', 'd'))
|
|
|
|
|
2011-12-28 23:47:19 +08:00
|
|
|
def test_mark_with_wrong_marker(self, testdir):
|
|
|
|
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
|
2011-12-28 23:47:19 +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):
|
|
|
|
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)
|
2010-06-08 08:34:51 +08:00
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_func(arg):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"keyword: *hello*"
|
|
|
|
])
|
2010-10-12 19:05:29 +08:00
|
|
|
|
2011-12-28 23:47:18 +08:00
|
|
|
def test_merging_markers_two_functions(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.hello("pos1", z=4)
|
|
|
|
@pytest.mark.hello("pos0", z=3)
|
2012-07-19 15:20:14 +08:00
|
|
|
def test_func():
|
2011-12-28 23:47:18 +08:00
|
|
|
pass
|
|
|
|
""")
|
|
|
|
items, rec = testdir.inline_genitems(p)
|
|
|
|
item, = items
|
|
|
|
keywords = item.keywords
|
|
|
|
marker = keywords['hello']
|
2017-11-04 23:17:20 +08:00
|
|
|
values = list(marker)
|
|
|
|
assert len(values) == 2
|
|
|
|
assert values[0].args == ("pos0",)
|
|
|
|
assert values[1].args == ("pos1",)
|
2011-12-28 23:47:18 +08:00
|
|
|
|
2013-05-23 18:21:40 +08:00
|
|
|
def test_no_marker_match_on_unmarked_names(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.shouldmatch
|
|
|
|
def test_marked():
|
|
|
|
assert 1
|
|
|
|
|
|
|
|
def test_unmarked():
|
|
|
|
assert 1
|
|
|
|
""")
|
|
|
|
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):
|
2013-10-12 21:39:22 +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
|
|
|
|
""")
|
|
|
|
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):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
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))
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
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
|
|
|
|
marker = request.node.get_marker("mark1")
|
|
|
|
assert marker.name == "mark1"
|
|
|
|
assert marker.args == ()
|
|
|
|
assert marker.kwargs == {}
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-m", "mark1")
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
from _pytest.mark import MarkInfo
|
|
|
|
items = dict((x.name, x) for x in items)
|
|
|
|
for name, expected_markers in expected.items():
|
|
|
|
markers = items[name].keywords._markers
|
|
|
|
marker_names = set([name for (name, v) in markers.items()
|
|
|
|
if isinstance(v, MarkInfo)])
|
|
|
|
assert marker_names == set(expected_markers)
|
|
|
|
|
2016-04-28 22:11:30 +08:00
|
|
|
@pytest.mark.issue1540
|
|
|
|
def test_mark_from_parameters(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
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
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(skipped=1)
|
2015-07-16 08:03:30 +08:00
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestKeywordSelection(object):
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-10-12 19:05:29 +08:00
|
|
|
def test_select_simple(self, testdir):
|
|
|
|
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
|
|
|
|
""")
|
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
|
|
|
|
assert len(reprec.getcalls('pytest_deselected')) == 1
|
|
|
|
|
|
|
|
for keyword in ['test_one', 'est_on']:
|
|
|
|
check(keyword, 'test_one')
|
2012-11-09 19:29:33 +08:00
|
|
|
check('TestClass and test', 'test_method_one')
|
2010-10-12 19:05:29 +08:00
|
|
|
|
2012-10-18 19:52:32 +08:00
|
|
|
@pytest.mark.parametrize("keyword", [
|
2014-10-06 18:11:48 +08:00
|
|
|
'xxx', 'xxx and test_2', 'TestClass', 'xxx and not test_1',
|
2012-11-09 19:29:33 +08:00
|
|
|
'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):
|
2010-10-12 19:05:29 +08:00
|
|
|
p = testdir.makepyfile(test_select="""
|
|
|
|
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
|
|
|
|
""")
|
|
|
|
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")
|
2010-10-12 19:05:29 +08:00
|
|
|
""")
|
2012-10-18 19:52:32 +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
|
|
|
|
assert dlist[0].items[0].name == 'test_1'
|
2010-10-12 19:05:29 +08:00
|
|
|
|
|
|
|
def test_select_starton(self, testdir):
|
|
|
|
threepass = testdir.makepyfile(test_threepass="""
|
|
|
|
def test_one(): assert 1
|
|
|
|
def test_two(): assert 1
|
|
|
|
def test_three(): assert 1
|
|
|
|
""")
|
|
|
|
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):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_one():
|
|
|
|
assert 0
|
|
|
|
test_one.mykeyword = True
|
|
|
|
""")
|
|
|
|
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):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_one():
|
|
|
|
assert 0
|
|
|
|
test_one.mykeyword = True
|
|
|
|
""")
|
|
|
|
# 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
|
|
|
|
|
2013-05-23 15:12:50 +08:00
|
|
|
def test_no_magic_values(self, testdir):
|
|
|
|
"""Make sure the tests do not match on magic values,
|
|
|
|
no double underscored values, like '__dict__',
|
|
|
|
and no instance values, like '()'.
|
|
|
|
"""
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_one(): assert 1
|
|
|
|
""")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-05-23 15:12:50 +08:00
|
|
|
def assert_test_is_not_selected(keyword):
|
|
|
|
reprec = testdir.inline_run("-k", keyword, p)
|
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
|
|
|
dlist = reprec.getcalls("pytest_deselected")
|
|
|
|
assert passed + skipped + failed == 0
|
2013-05-23 18:21:40 +08:00
|
|
|
deselected_tests = dlist[0].items
|
|
|
|
assert len(deselected_tests) == 1
|
2013-05-23 15:12:50 +08:00
|
|
|
|
|
|
|
assert_test_is_not_selected("__")
|
|
|
|
assert_test_is_not_selected("()")
|
2016-09-07 17:00:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('argval, expected', [
|
|
|
|
(pytest.mark.skip()((1, 2)),
|
|
|
|
ParameterSet(values=(1, 2), marks=[pytest.mark.skip], id=None)),
|
|
|
|
(pytest.mark.xfail(pytest.mark.skip()((1, 2))),
|
|
|
|
ParameterSet(values=(1, 2),
|
|
|
|
marks=[pytest.mark.xfail, pytest.mark.skip], id=None)),
|
|
|
|
|
|
|
|
])
|
2017-07-21 10:11:14 +08:00
|
|
|
@pytest.mark.filterwarnings('ignore')
|
2016-09-07 17:00:27 +08:00
|
|
|
def test_parameterset_extractfrom(argval, expected):
|
|
|
|
extracted = ParameterSet.extract_from(argval)
|
|
|
|
assert extracted == expected
|
2017-06-22 16:48:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_legacy_transfer():
|
|
|
|
|
|
|
|
class FakeModule(object):
|
|
|
|
pytestmark = []
|
|
|
|
|
|
|
|
class FakeClass(object):
|
|
|
|
pytestmark = pytest.mark.nofun
|
|
|
|
|
|
|
|
@pytest.mark.fun
|
|
|
|
def fake_method(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
transfer_markers(fake_method, FakeClass, FakeModule)
|
|
|
|
|
|
|
|
# legacy marks transfer smeared
|
|
|
|
assert fake_method.nofun
|
|
|
|
assert fake_method.fun
|
|
|
|
# pristine marks dont transfer
|
2017-07-18 08:16:14 +08:00
|
|
|
assert fake_method.pytestmark == [pytest.mark.fun.mark]
|
2017-09-09 13:20:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestMarkDecorator(object):
|
|
|
|
|
|
|
|
@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)
|
|
|
|
])
|
|
|
|
def test__eq__(self, lhs, rhs, expected):
|
|
|
|
assert (lhs == rhs) == expected
|
2017-12-17 19:49:12 +08:00
|
|
|
|
|
|
|
|
2018-01-26 19:05:52 +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-01-26 19:12:26 +08:00
|
|
|
testdir.makeini(
|
|
|
|
"[pytest]\n{}={}".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
|
|
|
|
pytest_configure(config)
|
|
|
|
result_mark = get_empty_parameterset_mark(config, ['a'], all)
|
2018-01-26 19:05:52 +08:00
|
|
|
if mark in (None, ''):
|
2017-12-17 19:49:12 +08:00
|
|
|
# normalize to the requested name
|
|
|
|
mark = 'skip'
|
|
|
|
assert result_mark.name == mark
|
2018-01-26 18:18:50 +08:00
|
|
|
assert result_mark.kwargs['reason'].startswith("got empty parameter set ")
|
2017-12-17 19:49:12 +08:00
|
|
|
if mark == 'xfail':
|
|
|
|
assert result_mark.kwargs.get('run') is False
|
|
|
|
|
|
|
|
|
|
|
|
def test_parameterset_for_parametrize_bad_markname(testdir):
|
|
|
|
with pytest.raises(pytest.UsageError):
|
|
|
|
test_parameterset_for_parametrize_marks(testdir, 'bad')
|