2015-11-27 22:43:01 +08:00
|
|
|
from textwrap import dedent
|
|
|
|
|
|
|
|
import _pytest._code
|
|
|
|
import pytest
|
2013-08-02 05:48:40 +08:00
|
|
|
from _pytest.pytester import get_public_names
|
2016-07-10 02:36:00 +08:00
|
|
|
from _pytest.fixtures import FixtureLookupError
|
|
|
|
from _pytest import fixtures
|
2010-09-26 00:23:26 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-05-12 01:23:57 +08:00
|
|
|
def test_getfuncargnames():
|
2017-07-17 07:25:10 +08:00
|
|
|
def f():
|
|
|
|
pass
|
2016-07-10 02:36:00 +08:00
|
|
|
assert not fixtures.getfuncargnames(f)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def g(arg):
|
|
|
|
pass
|
2016-07-10 02:36:00 +08:00
|
|
|
assert fixtures.getfuncargnames(g) == ('arg',)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def h(arg1, arg2="hello"):
|
|
|
|
pass
|
2016-07-10 02:36:00 +08:00
|
|
|
assert fixtures.getfuncargnames(h) == ('arg1',)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def h(arg1, arg2, arg3="hello"):
|
|
|
|
pass
|
2016-07-10 02:36:00 +08:00
|
|
|
assert fixtures.getfuncargnames(h) == ('arg1', 'arg2')
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2009-05-12 01:23:57 +08:00
|
|
|
def f(self, arg1, arg2="hello"):
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-08-18 07:37:51 +08:00
|
|
|
@staticmethod
|
|
|
|
def static(arg1, arg2):
|
|
|
|
pass
|
|
|
|
|
2016-07-10 02:36:00 +08:00
|
|
|
assert fixtures.getfuncargnames(A().f) == ('arg1',)
|
2017-08-18 07:37:51 +08:00
|
|
|
assert fixtures.getfuncargnames(A.static, cls=A) == ('arg1', 'arg2')
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestFillFixtures(object):
|
2011-03-06 15:56:58 +08:00
|
|
|
def test_fillfuncargs_exposed(self):
|
2012-10-05 20:24:44 +08:00
|
|
|
# used by oejskit, kept for compatibility
|
2016-07-10 02:36:00 +08:00
|
|
|
assert pytest._fillfuncargs == fixtures.fillfixtures
|
2011-03-06 15:56:58 +08:00
|
|
|
|
2009-04-14 08:23:42 +08:00
|
|
|
def test_funcarg_lookupfails(self, testdir):
|
2012-07-19 15:20:14 +08:00
|
|
|
testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def xyzsomething(request):
|
2009-05-19 05:26:16 +08:00
|
|
|
return 42
|
2012-07-19 15:20:14 +08:00
|
|
|
|
|
|
|
def test_func(some):
|
|
|
|
pass
|
2009-04-14 08:23:42 +08:00
|
|
|
""")
|
2017-07-17 07:25:09 +08:00
|
|
|
result = testdir.runpytest() # "--collect-only")
|
2012-07-19 15:20:14 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*def test_func(some)*",
|
2012-10-05 20:24:45 +08:00
|
|
|
"*fixture*some*not found*",
|
2012-07-19 15:20:14 +08:00
|
|
|
"*xyzsomething*",
|
|
|
|
])
|
2009-04-14 08:23:42 +08:00
|
|
|
|
|
|
|
def test_funcarg_basic(self, testdir):
|
2012-07-19 15:20:14 +08:00
|
|
|
item = testdir.getitem("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def some(request):
|
2010-07-27 03:15:15 +08:00
|
|
|
return request.function.__name__
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def other(request):
|
2009-04-14 08:23:42 +08:00
|
|
|
return 42
|
2012-07-19 15:20:14 +08:00
|
|
|
def test_func(some, other):
|
|
|
|
pass
|
|
|
|
""")
|
2016-07-10 02:36:00 +08:00
|
|
|
fixtures.fillfixtures(item)
|
2012-10-16 19:48:00 +08:00
|
|
|
del item.funcargs["request"]
|
2013-08-02 05:48:40 +08:00
|
|
|
assert len(get_public_names(item.funcargs)) == 2
|
2009-04-14 08:23:42 +08:00
|
|
|
assert item.funcargs['some'] == "test_func"
|
|
|
|
assert item.funcargs['other'] == 42
|
|
|
|
|
|
|
|
def test_funcarg_lookup_modulelevel(self, testdir):
|
2012-10-05 16:21:35 +08:00
|
|
|
testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2009-04-15 01:57:00 +08:00
|
|
|
return request.function.__name__
|
2009-04-14 08:23:42 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2009-04-14 08:23:42 +08:00
|
|
|
def test_method(self, something):
|
2012-10-05 16:21:35 +08:00
|
|
|
assert something == "test_method"
|
2009-04-14 08:23:42 +08:00
|
|
|
def test_func(something):
|
2012-10-05 16:21:35 +08:00
|
|
|
assert something == "test_func"
|
2009-04-14 08:23:42 +08:00
|
|
|
""")
|
2012-10-05 16:21:35 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
2009-04-14 08:23:42 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def test_funcarg_lookup_classlevel(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2016-07-12 09:03:53 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(self, request):
|
2010-07-27 03:15:15 +08:00
|
|
|
return request.instance
|
2016-07-12 09:03:53 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def test_method(self, something):
|
2010-07-27 03:15:15 +08:00
|
|
|
assert something is self
|
2009-05-19 05:26:16 +08:00
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(p)
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2009-05-19 05:26:16 +08:00
|
|
|
"*1 passed*"
|
|
|
|
])
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_conftest_funcargs_only_available_in_subdir(self, testdir):
|
|
|
|
sub1 = testdir.mkpydir("sub1")
|
|
|
|
sub2 = testdir.mkpydir("sub2")
|
2015-11-27 22:43:01 +08:00
|
|
|
sub1.join("conftest.py").write(_pytest._code.Source("""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg1(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
pytest.raises(Exception, "request.getfixturevalue('arg2')")
|
2012-11-02 23:04:57 +08:00
|
|
|
"""))
|
2015-11-27 22:43:01 +08:00
|
|
|
sub2.join("conftest.py").write(_pytest._code.Source("""
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg2(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
pytest.raises(Exception, "request.getfixturevalue('arg1')")
|
2012-11-02 23:04:57 +08:00
|
|
|
"""))
|
2009-05-22 01:08:33 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
sub1.join("test_in_sub1.py").write("def test_1(arg1): pass")
|
|
|
|
sub2.join("test_in_sub2.py").write("def test_2(arg2): pass")
|
2015-04-28 17:54:46 +08:00
|
|
|
result = testdir.runpytest("-v")
|
|
|
|
result.assert_outcomes(passed=2)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2013-04-18 19:24:53 +08:00
|
|
|
def test_extend_fixture_module_class(self, testdir):
|
|
|
|
testfile = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestSpam(object):
|
2013-04-18 19:24:53 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam(self, spam):
|
|
|
|
return spam * 2
|
|
|
|
|
|
|
|
def test_spam(self, spam):
|
|
|
|
assert spam == 'spamspam'
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(testfile)
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
|
|
|
def test_extend_fixture_conftest_module(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
""")
|
|
|
|
testfile = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam(spam):
|
|
|
|
return spam * 2
|
|
|
|
|
|
|
|
def test_spam(spam):
|
|
|
|
assert spam == 'spamspam'
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(testfile)
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
|
|
|
def test_extend_fixture_conftest_conftest(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
""")
|
|
|
|
pkg = testdir.mkpydir("pkg")
|
2015-11-27 22:43:01 +08:00
|
|
|
pkg.join("conftest.py").write(_pytest._code.Source("""
|
2013-04-18 19:24:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam(spam):
|
|
|
|
return spam * 2
|
|
|
|
"""))
|
|
|
|
testfile = pkg.join("test_spam.py")
|
2015-11-27 22:43:01 +08:00
|
|
|
testfile.write(_pytest._code.Source("""
|
2013-04-18 19:24:53 +08:00
|
|
|
def test_spam(spam):
|
|
|
|
assert spam == "spamspam"
|
|
|
|
"""))
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(testfile)
|
2013-04-18 19:24:53 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2013-08-02 01:58:28 +08:00
|
|
|
def test_extend_fixture_conftest_plugin(self, testdir):
|
2013-07-06 23:56:54 +08:00
|
|
|
testdir.makepyfile(testplugin="""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo():
|
|
|
|
return 7
|
|
|
|
""")
|
|
|
|
testdir.syspathinsert()
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
pytest_plugins = 'testplugin'
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo(foo):
|
|
|
|
return foo + 7
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_foo(foo):
|
|
|
|
assert foo == 14
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest('-s')
|
2013-07-06 23:56:54 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2013-08-02 01:58:28 +08:00
|
|
|
def test_extend_fixture_plugin_plugin(self, testdir):
|
|
|
|
# Two plugins should extend each order in loading order
|
|
|
|
testdir.makepyfile(testplugin0="""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo():
|
|
|
|
return 7
|
|
|
|
""")
|
|
|
|
testdir.makepyfile(testplugin1="""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo(foo):
|
|
|
|
return foo + 7
|
|
|
|
""")
|
|
|
|
testdir.syspathinsert()
|
|
|
|
testdir.makepyfile("""
|
|
|
|
pytest_plugins = ['testplugin0', 'testplugin1']
|
|
|
|
|
|
|
|
def test_foo(foo):
|
|
|
|
assert foo == 14
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2013-08-02 01:58:28 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2015-03-01 20:54:43 +08:00
|
|
|
def test_override_parametrized_fixture_conftest_module(self, testdir):
|
|
|
|
"""Test override of the parametrized fixture with non-parametrized one on the test module level."""
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2, 3])
|
|
|
|
def spam(request):
|
|
|
|
return request.param
|
|
|
|
""")
|
|
|
|
testfile = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
|
|
|
|
def test_spam(spam):
|
|
|
|
assert spam == 'spam'
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(testfile)
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
|
|
|
def test_override_parametrized_fixture_conftest_conftest(self, testdir):
|
|
|
|
"""Test override of the parametrized fixture with non-parametrized one on the conftest level."""
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2, 3])
|
|
|
|
def spam(request):
|
|
|
|
return request.param
|
|
|
|
""")
|
|
|
|
subdir = testdir.mkpydir('subdir')
|
2015-11-27 22:43:01 +08:00
|
|
|
subdir.join("conftest.py").write(_pytest._code.Source("""
|
2015-03-01 20:54:43 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
"""))
|
|
|
|
testfile = subdir.join("test_spam.py")
|
2015-11-27 22:43:01 +08:00
|
|
|
testfile.write(_pytest._code.Source("""
|
2015-03-01 20:54:43 +08:00
|
|
|
def test_spam(spam):
|
|
|
|
assert spam == "spam"
|
|
|
|
"""))
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(testfile)
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
|
|
|
def test_override_non_parametrized_fixture_conftest_module(self, testdir):
|
|
|
|
"""Test override of the non-parametrized fixture with parametrized one on the test module level."""
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
""")
|
|
|
|
testfile = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2, 3])
|
|
|
|
def spam(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
params = {'spam': 1}
|
|
|
|
|
|
|
|
def test_spam(spam):
|
|
|
|
assert spam == params['spam']
|
|
|
|
params['spam'] += 1
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(testfile)
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
|
|
|
|
|
|
|
def test_override_non_parametrized_fixture_conftest_conftest(self, testdir):
|
|
|
|
"""Test override of the non-parametrized fixture with parametrized one on the conftest level."""
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
""")
|
|
|
|
subdir = testdir.mkpydir('subdir')
|
2015-11-27 22:43:01 +08:00
|
|
|
subdir.join("conftest.py").write(_pytest._code.Source("""
|
2015-03-01 20:54:43 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2, 3])
|
|
|
|
def spam(request):
|
|
|
|
return request.param
|
|
|
|
"""))
|
|
|
|
testfile = subdir.join("test_spam.py")
|
2015-11-27 22:43:01 +08:00
|
|
|
testfile.write(_pytest._code.Source("""
|
2015-03-01 20:54:43 +08:00
|
|
|
params = {'spam': 1}
|
|
|
|
|
|
|
|
def test_spam(spam):
|
2016-07-23 21:48:41 +08:00
|
|
|
assert spam == params['spam']
|
|
|
|
params['spam'] += 1
|
|
|
|
"""))
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
|
|
|
result = testdir.runpytest(testfile)
|
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
|
|
|
|
|
|
|
def test_override_autouse_fixture_with_parametrized_fixture_conftest_conftest(self, testdir):
|
|
|
|
"""Test override of the autouse fixture with parametrized one on the conftest level.
|
|
|
|
This test covers the issue explained in issue 1601
|
|
|
|
"""
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def spam():
|
|
|
|
return 'spam'
|
|
|
|
""")
|
|
|
|
subdir = testdir.mkpydir('subdir')
|
|
|
|
subdir.join("conftest.py").write(_pytest._code.Source("""
|
2015-03-01 20:54:43 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2, 3])
|
|
|
|
def spam(request):
|
|
|
|
return request.param
|
|
|
|
"""))
|
|
|
|
testfile = subdir.join("test_spam.py")
|
2015-11-27 22:43:01 +08:00
|
|
|
testfile.write(_pytest._code.Source("""
|
2015-03-01 20:54:43 +08:00
|
|
|
params = {'spam': 1}
|
|
|
|
|
|
|
|
def test_spam(spam):
|
|
|
|
assert spam == params['spam']
|
|
|
|
params['spam'] += 1
|
|
|
|
"""))
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(testfile)
|
2015-03-01 20:54:43 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
|
|
|
|
2013-08-02 01:58:28 +08:00
|
|
|
def test_autouse_fixture_plugin(self, testdir):
|
|
|
|
# A fixture from a plugin has no baseid set, which screwed up
|
|
|
|
# the autouse fixture handling.
|
|
|
|
testdir.makepyfile(testplugin="""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def foo(request):
|
|
|
|
request.function.foo = 7
|
|
|
|
""")
|
|
|
|
testdir.syspathinsert()
|
|
|
|
testdir.makepyfile("""
|
|
|
|
pytest_plugins = 'testplugin'
|
|
|
|
|
|
|
|
def test_foo(request):
|
|
|
|
assert request.function.foo == 7
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2013-08-02 01:58:28 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_funcarg_lookup_error(self, testdir):
|
2016-08-03 06:43:39 +08:00
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def a_fixture(): pass
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def b_fixture(): pass
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def c_fixture(): pass
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def d_fixture(): pass
|
|
|
|
""")
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makepyfile("""
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_lookup_error(unknown):
|
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2016-08-12 06:57:29 +08:00
|
|
|
"*ERROR at setup of test_lookup_error*",
|
|
|
|
" def test_lookup_error(unknown):*",
|
|
|
|
"E fixture 'unknown' not found",
|
2016-08-18 09:39:23 +08:00
|
|
|
"> available fixtures:*a_fixture,*b_fixture,*c_fixture,*d_fixture*monkeypatch,*", # sorted
|
2016-08-12 06:57:29 +08:00
|
|
|
"> use 'py*test --fixtures *' for help on them.",
|
2012-11-02 23:04:57 +08:00
|
|
|
"*1 error*",
|
|
|
|
])
|
|
|
|
assert "INTERNAL" not in result.stdout.str()
|
2016-08-17 06:33:07 +08:00
|
|
|
|
2015-01-10 02:55:49 +08:00
|
|
|
def test_fixture_excinfo_leak(self, testdir):
|
|
|
|
# on python2 sys.excinfo would leak into fixture executions
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def leak():
|
|
|
|
if sys.exc_info()[0]: # python3 bug :)
|
|
|
|
traceback.print_exc()
|
|
|
|
#fails
|
|
|
|
assert sys.exc_info() == (None, None, None)
|
|
|
|
|
|
|
|
def test_leak(leak):
|
|
|
|
if sys.exc_info()[0]: # python3 bug :)
|
|
|
|
traceback.print_exc()
|
|
|
|
assert sys.exc_info() == (None, None, None)
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2015-01-10 02:55:49 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestRequestBasic(object):
|
2009-04-14 08:59:50 +08:00
|
|
|
def test_request_attributes(self, testdir):
|
|
|
|
item = testdir.getitem("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request): pass
|
2009-04-14 08:59:50 +08:00
|
|
|
def test_func(something): pass
|
|
|
|
""")
|
2016-07-10 02:36:00 +08:00
|
|
|
req = fixtures.FixtureRequest(item)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert req.function == item.obj
|
2012-09-17 23:32:23 +08:00
|
|
|
assert req.keywords == item.keywords
|
2009-05-12 01:23:57 +08:00
|
|
|
assert hasattr(req.module, 'test_func')
|
|
|
|
assert req.cls is None
|
2010-07-27 03:15:15 +08:00
|
|
|
assert req.function.__name__ == "test_func"
|
|
|
|
assert req.config == item.config
|
2009-04-15 03:36:57 +08:00
|
|
|
assert repr(req).find(req.function.__name__) != -1
|
2009-05-12 01:23:57 +08:00
|
|
|
|
|
|
|
def test_request_attributes_method(self, testdir):
|
|
|
|
item, = testdir.getitems("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestB(object):
|
2016-07-12 09:03:53 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(self, request):
|
2012-07-19 15:20:14 +08:00
|
|
|
return 1
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func(self, something):
|
2009-05-12 01:23:57 +08:00
|
|
|
pass
|
|
|
|
""")
|
2012-07-19 15:20:14 +08:00
|
|
|
req = item._request
|
2009-05-12 01:23:57 +08:00
|
|
|
assert req.cls.__name__ == "TestB"
|
2009-05-19 05:26:16 +08:00
|
|
|
assert req.instance.__class__ == req.cls
|
2009-05-21 15:45:43 +08:00
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
def test_request_contains_funcarg_arg2fixturedefs(self, testdir):
|
2009-04-14 08:23:42 +08:00
|
|
|
modcol = testdir.getmodulecol("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2009-04-14 08:23:42 +08:00
|
|
|
pass
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2009-04-14 08:23:42 +08:00
|
|
|
def test_method(self, something):
|
2009-05-21 15:45:43 +08:00
|
|
|
pass
|
2009-04-14 08:23:42 +08:00
|
|
|
""")
|
|
|
|
item1, = testdir.genitems([modcol])
|
|
|
|
assert item1.name == "test_method"
|
2016-07-10 02:36:00 +08:00
|
|
|
arg2fixturedefs = fixtures.FixtureRequest(item1)._arg2fixturedefs
|
2012-10-16 19:59:12 +08:00
|
|
|
assert len(arg2fixturedefs) == 1
|
2016-07-12 09:03:53 +08:00
|
|
|
assert arg2fixturedefs['something'][0].argname == "something"
|
2009-04-14 08:59:50 +08:00
|
|
|
|
2016-06-21 18:09:55 +08:00
|
|
|
def test_getfixturevalue_recursive(self, testdir):
|
2009-06-12 01:49:25 +08:00
|
|
|
testdir.makeconftest("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2009-06-12 01:49:25 +08:00
|
|
|
return 1
|
|
|
|
""")
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
return request.getfixturevalue("something") + 1
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func(something):
|
2009-06-12 01:49:25 +08:00
|
|
|
assert something == 2
|
2009-04-14 08:59:50 +08:00
|
|
|
""")
|
2012-07-20 20:16:28 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
2009-04-14 08:59:50 +08:00
|
|
|
|
2016-06-21 18:09:55 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'getfixmethod', ('getfixturevalue', 'getfuncargvalue'))
|
|
|
|
def test_getfixturevalue(self, testdir, getfixmethod):
|
2009-05-21 15:45:43 +08:00
|
|
|
item = testdir.getitem("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [2]
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def something(request): return 1
|
|
|
|
@pytest.fixture
|
|
|
|
def other(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
return values.pop()
|
2009-05-21 15:45:43 +08:00
|
|
|
def test_func(something): pass
|
|
|
|
""")
|
2017-03-21 09:01:22 +08:00
|
|
|
import contextlib
|
|
|
|
if getfixmethod == 'getfuncargvalue':
|
|
|
|
warning_expectation = pytest.warns(DeprecationWarning)
|
|
|
|
else:
|
|
|
|
# see #1830 for a cleaner way to accomplish this
|
|
|
|
@contextlib.contextmanager
|
2017-07-17 07:25:10 +08:00
|
|
|
def expecting_no_warning():
|
|
|
|
yield
|
2017-03-21 09:01:22 +08:00
|
|
|
|
|
|
|
warning_expectation = expecting_no_warning()
|
|
|
|
|
2012-07-19 15:20:14 +08:00
|
|
|
req = item._request
|
2017-03-21 09:01:22 +08:00
|
|
|
with warning_expectation:
|
|
|
|
fixture_fetcher = getattr(req, getfixmethod)
|
|
|
|
with pytest.raises(FixtureLookupError):
|
|
|
|
fixture_fetcher("notexists")
|
|
|
|
val = fixture_fetcher("something")
|
|
|
|
assert val == 1
|
|
|
|
val = fixture_fetcher("something")
|
|
|
|
assert val == 1
|
|
|
|
val2 = fixture_fetcher("other")
|
|
|
|
assert val2 == 2
|
|
|
|
val2 = fixture_fetcher("other") # see about caching
|
|
|
|
assert val2 == 2
|
|
|
|
pytest._fillfuncargs(item)
|
|
|
|
assert item.funcargs["something"] == 1
|
|
|
|
assert len(get_public_names(item.funcargs)) == 2
|
|
|
|
assert "request" in item.funcargs
|
2009-05-21 15:45:43 +08:00
|
|
|
|
2009-06-23 23:10:52 +08:00
|
|
|
def test_request_addfinalizer(self, testdir):
|
2009-04-14 08:59:50 +08:00
|
|
|
item = testdir.getitem("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2009-05-21 20:37:30 +08:00
|
|
|
teardownlist = []
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2009-06-23 23:10:52 +08:00
|
|
|
request.addfinalizer(lambda: teardownlist.append(1))
|
2009-04-14 08:59:50 +08:00
|
|
|
def test_func(something): pass
|
|
|
|
""")
|
2012-06-25 23:35:33 +08:00
|
|
|
item.session._setupstate.prepare(item)
|
|
|
|
pytest._fillfuncargs(item)
|
2010-07-27 03:15:15 +08:00
|
|
|
# successively check finalization calls
|
2010-11-13 16:05:11 +08:00
|
|
|
teardownlist = item.getparent(pytest.Module).obj.teardownlist
|
2011-05-27 06:08:56 +08:00
|
|
|
ss = item.session._setupstate
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not teardownlist
|
2011-12-03 05:00:19 +08:00
|
|
|
ss.teardown_exact(item, None)
|
2009-08-30 02:04:48 +08:00
|
|
|
print(ss.stack)
|
2009-06-23 23:10:52 +08:00
|
|
|
assert teardownlist == [1]
|
2009-05-19 01:06:16 +08:00
|
|
|
|
2016-06-22 02:10:22 +08:00
|
|
|
def test_mark_as_fixture_with_prefix_and_decorator_fails(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def pytest_funcarg__marked_with_prefix_and_decorator():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest_subprocess()
|
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines([
|
2016-07-12 09:03:53 +08:00
|
|
|
"*AssertionError: fixtures cannot have*@pytest.fixture*",
|
|
|
|
"*pytest_funcarg__marked_with_prefix_and_decorator*"
|
2016-06-22 02:10:22 +08:00
|
|
|
])
|
|
|
|
|
2013-07-17 16:29:11 +08:00
|
|
|
def test_request_addfinalizer_failing_setup(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [1]
|
2013-07-17 16:29:11 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def myfix(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(values.pop)
|
2013-07-17 16:29:11 +08:00
|
|
|
assert 0
|
|
|
|
def test_fix(myfix):
|
|
|
|
pass
|
|
|
|
def test_finalizer_ran():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert not values
|
2013-07-17 16:29:11 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-s")
|
|
|
|
reprec.assertoutcome(failed=1, passed=1)
|
|
|
|
|
|
|
|
def test_request_addfinalizer_failing_setup_module(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [1, 2]
|
2013-07-17 16:29:11 +08:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def myfix(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(values.pop)
|
|
|
|
request.addfinalizer(values.pop)
|
2013-07-17 16:29:11 +08:00
|
|
|
assert 0
|
|
|
|
def test_fix(myfix):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-s")
|
|
|
|
mod = reprec.getcalls("pytest_runtest_setup")[0].item.module
|
2017-11-04 23:17:20 +08:00
|
|
|
assert not mod.values
|
2013-07-17 16:29:11 +08:00
|
|
|
|
2009-07-05 20:22:01 +08:00
|
|
|
def test_request_addfinalizer_partial_setup_failure(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append(None))
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func(something, missingarg):
|
2009-07-05 20:22:01 +08:00
|
|
|
pass
|
|
|
|
def test_second():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2009-07-05 20:22:01 +08:00
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(p)
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2012-07-19 15:20:14 +08:00
|
|
|
"*1 error*" # XXX the whole module collection fails
|
2017-07-17 07:25:07 +08:00
|
|
|
])
|
2009-07-05 20:22:01 +08:00
|
|
|
|
2017-06-13 08:39:42 +08:00
|
|
|
def test_request_subrequest_addfinalizer_exceptions(self, testdir):
|
2017-06-14 06:54:14 +08:00
|
|
|
"""
|
|
|
|
Ensure exceptions raised during teardown by a finalizer are suppressed
|
|
|
|
until all finalizers are called, re-raising the first exception (#2440)
|
|
|
|
"""
|
2017-06-13 08:39:42 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2017-06-14 06:54:14 +08:00
|
|
|
def _excepts(where):
|
|
|
|
raise Exception('Error in %s fixture' % where)
|
2017-06-13 08:39:42 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def subrequest(request):
|
|
|
|
return request
|
|
|
|
@pytest.fixture
|
|
|
|
def something(subrequest):
|
2017-11-04 23:17:20 +08:00
|
|
|
subrequest.addfinalizer(lambda: values.append(1))
|
|
|
|
subrequest.addfinalizer(lambda: values.append(2))
|
2017-06-14 06:54:14 +08:00
|
|
|
subrequest.addfinalizer(lambda: _excepts('something'))
|
2017-06-13 08:39:42 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def excepts(subrequest):
|
2017-06-14 06:54:14 +08:00
|
|
|
subrequest.addfinalizer(lambda: _excepts('excepts'))
|
2017-11-04 23:17:20 +08:00
|
|
|
subrequest.addfinalizer(lambda: values.append(3))
|
2017-06-13 08:39:42 +08:00
|
|
|
def test_first(something, excepts):
|
|
|
|
pass
|
|
|
|
def test_second():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [3, 2, 1]
|
2017-06-13 08:39:42 +08:00
|
|
|
""")
|
2017-06-14 06:54:14 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
'*Exception: Error in excepts fixture',
|
|
|
|
'* 2 passed, 1 error in *',
|
|
|
|
])
|
2009-07-05 20:22:01 +08:00
|
|
|
|
2009-05-21 15:45:43 +08:00
|
|
|
def test_request_getmodulepath(self, testdir):
|
|
|
|
modcol = testdir.getmodulecol("def test_somefunc(): pass")
|
|
|
|
item, = testdir.genitems([modcol])
|
2016-07-10 02:36:00 +08:00
|
|
|
req = fixtures.FixtureRequest(item)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert req.fspath == modcol.fspath
|
2009-05-21 15:45:43 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_request_fixturenames(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2013-08-02 05:48:40 +08:00
|
|
|
from _pytest.pytester import get_public_names
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture()
|
|
|
|
def arg1():
|
|
|
|
pass
|
|
|
|
@pytest.fixture()
|
|
|
|
def farg(arg1):
|
|
|
|
pass
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def sarg(tmpdir):
|
|
|
|
pass
|
|
|
|
def test_function(request, farg):
|
2013-08-02 05:48:40 +08:00
|
|
|
assert set(get_public_names(request.fixturenames)) == \
|
2015-07-16 07:03:58 +08:00
|
|
|
set(["tmpdir", "sarg", "arg1", "request", "farg",
|
|
|
|
"tmpdir_factory"])
|
2012-11-02 23:04:57 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
def test_funcargnames_compatattr(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2012-11-02 23:04:57 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
assert metafunc.funcargnames == metafunc.fixturenames
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def fn(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
assert request._pyfuncitem.funcargnames == \
|
|
|
|
request._pyfuncitem.fixturenames
|
|
|
|
return request.funcargnames, request.fixturenames
|
|
|
|
|
|
|
|
def test_hello(fn):
|
|
|
|
assert fn[0] == fn[1]
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
def test_setupdecorator_and_xunit(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture(scope='module', autouse=True)
|
|
|
|
def setup_module():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("module")
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_function():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("function")
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
|
|
|
def setup_class(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("class")
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_method(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("method")
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_method(self):
|
|
|
|
pass
|
|
|
|
def test_all():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["module", "function", "class",
|
2012-11-02 23:04:57 +08:00
|
|
|
"function", "method", "function"]
|
|
|
|
""")
|
2013-11-21 19:21:52 +08:00
|
|
|
reprec = testdir.inline_run("-v")
|
2012-11-02 23:04:57 +08:00
|
|
|
reprec.assertoutcome(passed=3)
|
|
|
|
|
|
|
|
def test_fixtures_sub_subdir_normalize_sep(self, testdir):
|
2015-03-05 00:00:24 +08:00
|
|
|
# this tests that normalization of nodeids takes place
|
2012-11-02 23:04:57 +08:00
|
|
|
b = testdir.mkdir("tests").mkdir("unit")
|
2015-11-27 22:43:01 +08:00
|
|
|
b.join("conftest.py").write(_pytest._code.Source("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
2012-11-02 23:04:57 +08:00
|
|
|
pass
|
|
|
|
"""))
|
|
|
|
p = b.join("test_module.py")
|
|
|
|
p.write("def test_func(arg1): pass")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest(p, "--fixtures")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*fixtures defined*conftest*
|
|
|
|
*arg1*
|
|
|
|
""")
|
|
|
|
|
2015-07-19 03:39:55 +08:00
|
|
|
def test_show_fixtures_color_yes(self, testdir):
|
|
|
|
testdir.makepyfile("def test_this(): assert 1")
|
|
|
|
result = testdir.runpytest('--color=yes', '--fixtures')
|
|
|
|
assert '\x1b[32mtmpdir' in result.stdout.str()
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_newstyle_with_request(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture()
|
|
|
|
def arg(request):
|
|
|
|
pass
|
|
|
|
def test_1(arg):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
def test_setupcontext_no_param(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(params=[1,2])
|
|
|
|
def arg(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mysetup(request, arg):
|
|
|
|
assert not hasattr(request, "param")
|
|
|
|
def test_1(arg):
|
|
|
|
assert arg in (1,2)
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestRequestMarking(object):
|
2012-09-17 23:32:23 +08:00
|
|
|
def test_applymarker(self, testdir):
|
2017-07-17 07:25:08 +08:00
|
|
|
item1, item2 = testdir.getitems("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2010-06-08 08:34:51 +08:00
|
|
|
pass
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-09-17 23:32:23 +08:00
|
|
|
def test_func1(self, something):
|
|
|
|
pass
|
|
|
|
def test_func2(self, something):
|
|
|
|
pass
|
|
|
|
""")
|
2016-07-10 02:36:00 +08:00
|
|
|
req1 = fixtures.FixtureRequest(item1)
|
2012-09-17 23:32:23 +08:00
|
|
|
assert 'xfail' not in item1.keywords
|
|
|
|
req1.applymarker(pytest.mark.xfail)
|
|
|
|
assert 'xfail' in item1.keywords
|
|
|
|
assert 'skipif' not in item1.keywords
|
|
|
|
req1.applymarker(pytest.mark.skipif)
|
|
|
|
assert 'skipif' in item1.keywords
|
|
|
|
pytest.raises(ValueError, "req1.applymarker(42)")
|
|
|
|
|
2012-10-18 19:52:32 +08:00
|
|
|
def test_accesskeywords(self, testdir):
|
2012-09-17 23:32:23 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-10-18 19:52:32 +08:00
|
|
|
def keywords(request):
|
|
|
|
return request.keywords
|
2012-09-17 23:32:23 +08:00
|
|
|
@pytest.mark.XYZ
|
2012-10-18 19:52:32 +08:00
|
|
|
def test_function(keywords):
|
|
|
|
assert keywords["XYZ"]
|
|
|
|
assert "abc" not in keywords
|
2012-09-17 23:32:23 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
def test_accessmarker_dynamic(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-10-18 19:52:32 +08:00
|
|
|
def keywords(request):
|
|
|
|
return request.keywords
|
2012-09-17 23:32:23 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
2012-09-17 23:32:23 +08:00
|
|
|
def marking(request):
|
|
|
|
request.applymarker(pytest.mark.XYZ("hello"))
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-18 19:52:32 +08:00
|
|
|
def test_fun1(keywords):
|
|
|
|
assert keywords["XYZ"] is not None
|
|
|
|
assert "abc" not in keywords
|
|
|
|
def test_fun2(keywords):
|
|
|
|
assert keywords["XYZ"] is not None
|
|
|
|
assert "abc" not in keywords
|
2012-09-17 23:32:23 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestRequestCachedSetup(object):
|
2012-07-19 15:20:14 +08:00
|
|
|
def test_request_cachedsetup_defaultmodule(self, testdir):
|
|
|
|
reprec = testdir.inline_runsource("""
|
|
|
|
mysetup = ["hello",].pop
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2012-07-19 15:20:14 +08:00
|
|
|
return request.cached_setup(mysetup, scope="module")
|
|
|
|
|
|
|
|
def test_func1(something):
|
|
|
|
assert something == "hello"
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-07-19 15:20:14 +08:00
|
|
|
def test_func1a(self, something):
|
|
|
|
assert something == "hello"
|
2009-05-19 01:06:16 +08:00
|
|
|
""")
|
2012-07-19 15:20:14 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
2009-05-19 01:06:16 +08:00
|
|
|
|
2010-11-21 01:03:18 +08:00
|
|
|
def test_request_cachedsetup_class(self, testdir):
|
2012-07-19 15:20:14 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2013-07-07 03:30:24 +08:00
|
|
|
mysetup = ["hello", "hello2", "hello3"].pop
|
2012-07-19 15:20:14 +08:00
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2012-07-19 15:20:14 +08:00
|
|
|
return request.cached_setup(mysetup, scope="class")
|
|
|
|
def test_func1(something):
|
2013-07-07 03:30:24 +08:00
|
|
|
assert something == "hello3"
|
2012-07-19 15:20:14 +08:00
|
|
|
def test_func2(something):
|
|
|
|
assert something == "hello2"
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2010-11-21 01:03:18 +08:00
|
|
|
def test_func1a(self, something):
|
2012-07-19 15:20:14 +08:00
|
|
|
assert something == "hello"
|
2010-11-21 01:03:18 +08:00
|
|
|
def test_func2b(self, something):
|
2012-07-19 15:20:14 +08:00
|
|
|
assert something == "hello"
|
2010-11-21 01:03:18 +08:00
|
|
|
""")
|
2012-07-19 15:20:14 +08:00
|
|
|
reprec.assertoutcome(passed=4)
|
2010-11-21 01:03:18 +08:00
|
|
|
|
2009-05-19 01:06:16 +08:00
|
|
|
def test_request_cachedsetup_extrakey(self, testdir):
|
|
|
|
item1 = testdir.getitem("def test_func(): pass")
|
2016-07-10 02:36:00 +08:00
|
|
|
req1 = fixtures.FixtureRequest(item1)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = ["hello", "world"]
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2009-05-19 01:06:16 +08:00
|
|
|
def setup():
|
2017-11-04 23:17:20 +08:00
|
|
|
return values.pop()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2009-05-19 01:06:16 +08:00
|
|
|
ret1 = req1.cached_setup(setup, extrakey=1)
|
|
|
|
ret2 = req1.cached_setup(setup, extrakey=2)
|
|
|
|
assert ret2 == "hello"
|
|
|
|
assert ret1 == "world"
|
|
|
|
ret1b = req1.cached_setup(setup, extrakey=1)
|
|
|
|
ret2b = req1.cached_setup(setup, extrakey=2)
|
|
|
|
assert ret1 == ret1b
|
|
|
|
assert ret2 == ret2b
|
|
|
|
|
2009-09-10 05:07:42 +08:00
|
|
|
def test_request_cachedsetup_cache_deletion(self, testdir):
|
|
|
|
item1 = testdir.getitem("def test_func(): pass")
|
2016-07-10 02:36:00 +08:00
|
|
|
req1 = fixtures.FixtureRequest(item1)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2009-09-10 05:07:42 +08:00
|
|
|
def setup():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("setup")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2009-09-10 05:07:42 +08:00
|
|
|
def teardown(val):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("teardown")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-10-12 21:39:22 +08:00
|
|
|
req1.cached_setup(setup, teardown, scope="function")
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ['setup']
|
2009-09-10 05:07:42 +08:00
|
|
|
# artificial call of finalizer
|
2012-06-25 23:35:33 +08:00
|
|
|
setupstate = req1._pyfuncitem.session._setupstate
|
|
|
|
setupstate._callfinalizers(item1)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["setup", "teardown"]
|
2013-10-12 21:39:22 +08:00
|
|
|
req1.cached_setup(setup, teardown, scope="function")
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["setup", "teardown", "setup"]
|
2012-06-25 23:35:33 +08:00
|
|
|
setupstate._callfinalizers(item1)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["setup", "teardown", "setup", "teardown"]
|
2009-09-10 05:07:42 +08:00
|
|
|
|
2009-09-30 18:52:40 +08:00
|
|
|
def test_request_cached_setup_two_args(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1(request):
|
2009-09-30 18:52:40 +08:00
|
|
|
return request.cached_setup(lambda: 42)
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg2(request):
|
2009-09-30 18:52:40 +08:00
|
|
|
return request.cached_setup(lambda: 17)
|
|
|
|
def test_two_different_setups(arg1, arg2):
|
2010-07-27 03:15:15 +08:00
|
|
|
assert arg1 != arg2
|
2009-09-30 18:52:40 +08:00
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v")
|
2009-09-30 18:52:40 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 passed*"
|
|
|
|
])
|
|
|
|
|
2016-06-21 18:09:55 +08:00
|
|
|
def test_request_cached_setup_getfixturevalue(self, testdir):
|
2009-09-30 18:52:40 +08:00
|
|
|
testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
arg1 = request.getfixturevalue("arg2")
|
2009-09-30 18:52:40 +08:00
|
|
|
return request.cached_setup(lambda: arg1 + 1)
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg2(request):
|
2009-09-30 18:52:40 +08:00
|
|
|
return request.cached_setup(lambda: 10)
|
|
|
|
def test_two_funcarg(arg1):
|
|
|
|
assert arg1 == 11
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v")
|
2009-09-30 18:52:40 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 passed*"
|
|
|
|
])
|
|
|
|
|
2009-05-19 01:06:16 +08:00
|
|
|
def test_request_cached_setup_functional(self, testdir):
|
|
|
|
testdir.makepyfile(test_0="""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
2010-10-03 17:17:37 +08:00
|
|
|
val = request.cached_setup(fsetup, fteardown)
|
2010-07-27 03:15:15 +08:00
|
|
|
return val
|
2010-10-03 17:17:37 +08:00
|
|
|
def fsetup(mycache=[1]):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(mycache.pop())
|
|
|
|
return values
|
2010-10-03 17:17:37 +08:00
|
|
|
def fteardown(something):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.remove(something[0])
|
|
|
|
values.append(2)
|
2009-05-19 01:06:16 +08:00
|
|
|
def test_list_once(something):
|
|
|
|
assert something == [1]
|
|
|
|
def test_list_twice(something):
|
2012-11-02 23:04:57 +08:00
|
|
|
assert something == [1]
|
|
|
|
""")
|
|
|
|
testdir.makepyfile(test_1="""
|
|
|
|
import test_0 # should have run already
|
|
|
|
def test_check_test0_has_teardown_correct():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert test_0.values == [2]
|
2012-11-02 23:04:57 +08:00
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v")
|
2012-10-17 15:21:04 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*3 passed*"
|
|
|
|
])
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_issue117_sessionscopeteardown(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def app(request):
|
2012-11-02 23:04:57 +08:00
|
|
|
app = request.cached_setup(
|
|
|
|
scope='session',
|
|
|
|
setup=lambda: 0,
|
|
|
|
teardown=lambda x: 3/x)
|
|
|
|
return app
|
|
|
|
def test_func(app):
|
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-11-02 23:04:57 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*3/x*",
|
|
|
|
"*ZeroDivisionError*",
|
|
|
|
])
|
2009-12-30 21:05:41 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestFixtureUsages(object):
|
2012-10-07 03:03:55 +08:00
|
|
|
def test_noargfixturedec(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def test_func(arg1):
|
|
|
|
assert arg1 == 1
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2012-07-23 16:55:09 +08:00
|
|
|
def test_receives_funcargs(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg1():
|
2012-07-23 16:55:09 +08:00
|
|
|
return 1
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg2(arg1):
|
2012-07-23 16:55:09 +08:00
|
|
|
return arg1 + 1
|
|
|
|
|
|
|
|
def test_add(arg2):
|
|
|
|
assert arg2 == 2
|
|
|
|
def test_all(arg1, arg2):
|
|
|
|
assert arg1 == 1
|
|
|
|
assert arg2 == 2
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
|
|
|
def test_receives_funcargs_scope_mismatch(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg1():
|
2012-07-23 16:55:09 +08:00
|
|
|
return 1
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg2(arg1):
|
2012-07-23 16:55:09 +08:00
|
|
|
return arg1 + 1
|
|
|
|
|
|
|
|
def test_add(arg2):
|
|
|
|
assert arg2 == 2
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-07-23 16:55:09 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*ScopeMismatch*involved factories*",
|
|
|
|
"* def arg2*",
|
|
|
|
"* def arg1*",
|
|
|
|
"*1 error*"
|
|
|
|
])
|
|
|
|
|
2015-04-02 00:42:48 +08:00
|
|
|
def test_receives_funcargs_scope_mismatch_issue660(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def arg1():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def arg2(arg1):
|
|
|
|
return arg1 + 1
|
|
|
|
|
|
|
|
def test_add(arg1, arg2):
|
|
|
|
assert arg2 == 2
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2015-04-02 00:42:48 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*ScopeMismatch*involved factories*",
|
|
|
|
"* def arg2*",
|
|
|
|
"*1 error*"
|
|
|
|
])
|
|
|
|
|
2016-09-07 02:16:25 +08:00
|
|
|
def test_invalid_scope(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope="functions")
|
|
|
|
def badscope():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_nothing(badscope):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest_inprocess()
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
("*ValueError: fixture badscope from test_invalid_scope.py has an unsupported"
|
|
|
|
" scope value 'functions'")
|
|
|
|
)
|
|
|
|
|
2012-07-23 16:55:09 +08:00
|
|
|
def test_funcarg_parametrized_and_used_twice(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=[1,2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg1(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-09-17 22:36:10 +08:00
|
|
|
return request.param
|
2012-07-23 16:55:09 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg2(arg1):
|
2012-07-23 16:55:09 +08:00
|
|
|
return arg1 + 1
|
|
|
|
|
|
|
|
def test_add(arg1, arg2):
|
|
|
|
assert arg2 == arg1 + 1
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == arg1
|
2012-07-23 16:55:09 +08:00
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-07-23 16:55:09 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*2 passed*"
|
|
|
|
])
|
2012-06-25 23:35:33 +08:00
|
|
|
|
2012-08-23 03:43:42 +08:00
|
|
|
def test_factory_uses_unknown_funcarg_as_dependency_error(self, testdir):
|
2012-08-23 01:49:50 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-08-23 03:20:18 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-23 03:20:18 +08:00
|
|
|
def fail(missing):
|
|
|
|
return
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-23 03:20:18 +08:00
|
|
|
def call_fail(fail):
|
2012-08-23 01:49:50 +08:00
|
|
|
return
|
|
|
|
|
2012-08-23 03:20:18 +08:00
|
|
|
def test_missing(call_fail):
|
2012-08-23 01:49:50 +08:00
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-10-05 20:24:45 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*pytest.fixture()*
|
|
|
|
*def call_fail(fail)*
|
|
|
|
*pytest.fixture()*
|
|
|
|
*def fail*
|
|
|
|
*fixture*'missing'*not found*
|
|
|
|
""")
|
2012-08-23 01:49:50 +08:00
|
|
|
|
2012-10-08 17:22:31 +08:00
|
|
|
def test_factory_setup_as_classes_fails(self, testdir):
|
2012-09-18 20:00:47 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class arg1(object):
|
2012-09-18 20:00:47 +08:00
|
|
|
def __init__(self, request):
|
|
|
|
self.x = 1
|
2012-10-05 16:21:35 +08:00
|
|
|
arg1 = pytest.fixture()(arg1)
|
2012-09-18 20:00:47 +08:00
|
|
|
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getfailedcollections()
|
|
|
|
assert len(values) == 1
|
2012-09-18 20:00:47 +08:00
|
|
|
|
2012-10-01 15:23:39 +08:00
|
|
|
def test_request_can_be_overridden(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-10-01 15:23:39 +08:00
|
|
|
def request(request):
|
|
|
|
request.a = 1
|
|
|
|
return request
|
|
|
|
def test_request(request):
|
|
|
|
assert request.a == 1
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2012-10-06 01:20:40 +08:00
|
|
|
def test_usefixtures_marker(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-06 01:20:40 +08:00
|
|
|
|
|
|
|
@pytest.fixture(scope="class")
|
|
|
|
def myfix(request):
|
|
|
|
request.cls.hello = "world"
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-10-06 01:20:40 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-06 01:20:40 +08:00
|
|
|
def test_one(self):
|
|
|
|
assert self.hello == "world"
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2012-10-06 01:20:40 +08:00
|
|
|
def test_two(self):
|
|
|
|
assert self.hello == "world"
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2012-10-06 01:20:40 +08:00
|
|
|
pytest.mark.usefixtures("myfix")(TestClass)
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
|
|
|
def test_usefixtures_ini(self, testdir):
|
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
usefixtures = myfix
|
|
|
|
""")
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="class")
|
|
|
|
def myfix(request):
|
|
|
|
request.cls.hello = "world"
|
|
|
|
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-06 01:20:40 +08:00
|
|
|
def test_one(self):
|
|
|
|
assert self.hello == "world"
|
|
|
|
def test_two(self):
|
|
|
|
assert self.hello == "world"
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
2012-07-16 16:46:44 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_usefixtures_seen_in_showmarkers(self, testdir):
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--markers")
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*usefixtures(fixturename1*mark tests*fixtures*
|
|
|
|
""")
|
|
|
|
|
2012-10-20 15:59:20 +08:00
|
|
|
def test_request_instance_issue203(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-20 15:59:20 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def setup1(self, request):
|
|
|
|
assert self == request.instance
|
|
|
|
self.arg1 = 1
|
|
|
|
def test_hello(self, setup1):
|
|
|
|
assert self.arg1 == 1
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2013-12-09 17:48:15 +08:00
|
|
|
def test_fixture_parametrized_with_iterator(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2013-12-09 17:48:15 +08:00
|
|
|
def f():
|
|
|
|
yield 1
|
|
|
|
yield 2
|
|
|
|
dec = pytest.fixture(scope="module", params=f())
|
|
|
|
|
|
|
|
@dec
|
|
|
|
def arg(request):
|
|
|
|
return request.param
|
|
|
|
@dec
|
|
|
|
def arg2(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2013-12-09 17:48:15 +08:00
|
|
|
def test_2(arg2):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg2*10)
|
2013-12-09 17:48:15 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-v")
|
|
|
|
reprec.assertoutcome(passed=4)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
|
|
|
assert values == [1, 2, 10, 20]
|
2013-12-09 17:48:15 +08:00
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestFixtureManagerParseFactories(object):
|
2016-07-12 09:03:53 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def testdir(self, request):
|
2016-06-21 18:09:55 +08:00
|
|
|
testdir = request.getfixturevalue("testdir")
|
2012-07-20 20:16:28 +08:00
|
|
|
testdir.makeconftest("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hello(request):
|
2012-07-20 20:16:28 +08:00
|
|
|
return "conftest"
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def fm(request):
|
2012-10-05 20:24:44 +08:00
|
|
|
return request._fixturemanager
|
2012-07-20 20:16:28 +08:00
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def item(request):
|
2012-07-20 20:16:28 +08:00
|
|
|
return request._pyfuncitem
|
|
|
|
""")
|
|
|
|
return testdir
|
|
|
|
|
2012-11-01 00:00:55 +08:00
|
|
|
def test_parsefactories_evil_objects_issue214(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2012-11-01 00:00:55 +08:00
|
|
|
def __call__(self):
|
|
|
|
pass
|
|
|
|
def __getattr__(self, name):
|
|
|
|
raise RuntimeError()
|
|
|
|
a = A()
|
|
|
|
def test_hello():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1, failed=0)
|
|
|
|
|
2012-07-20 20:16:28 +08:00
|
|
|
def test_parsefactories_conftest(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_hello(item, fm):
|
|
|
|
for name in ("fm", "hello", "item"):
|
2012-10-16 19:59:12 +08:00
|
|
|
faclist = fm.getfixturedefs(name, item.nodeid)
|
2012-07-20 20:16:28 +08:00
|
|
|
assert len(faclist) == 1
|
|
|
|
fac = faclist[0]
|
2016-07-12 09:03:53 +08:00
|
|
|
assert fac.func.__name__ == name
|
2012-07-20 20:16:28 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-s")
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
def test_parsefactories_conftest_and_module_and_class(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def hello(request):
|
2012-07-20 20:16:28 +08:00
|
|
|
return "module"
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def hello(self, request):
|
2012-07-20 20:16:28 +08:00
|
|
|
return "class"
|
|
|
|
def test_hello(self, item, fm):
|
2012-10-16 19:59:12 +08:00
|
|
|
faclist = fm.getfixturedefs("hello", item.nodeid)
|
2012-08-01 15:07:32 +08:00
|
|
|
print (faclist)
|
2012-07-20 20:16:28 +08:00
|
|
|
assert len(faclist) == 3
|
2012-07-30 18:39:45 +08:00
|
|
|
assert faclist[0].func(item._request) == "conftest"
|
|
|
|
assert faclist[1].func(item._request) == "module"
|
|
|
|
assert faclist[2].func(item._request) == "class"
|
2012-07-20 20:16:28 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-s")
|
|
|
|
reprec.assertoutcome(passed=1)
|
2012-07-20 20:16:46 +08:00
|
|
|
|
2014-09-15 18:44:16 +08:00
|
|
|
def test_parsefactories_relative_node_ids(self, testdir):
|
|
|
|
# example mostly taken from:
|
|
|
|
# https://mail.python.org/pipermail/pytest-dev/2014-September/002617.html
|
|
|
|
runner = testdir.mkdir("runner")
|
|
|
|
package = testdir.mkdir("package")
|
|
|
|
package.join("conftest.py").write(dedent("""\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def one():
|
|
|
|
return 1
|
|
|
|
"""))
|
|
|
|
package.join("test_x.py").write(dedent("""\
|
|
|
|
def test_x(one):
|
|
|
|
assert one == 1
|
|
|
|
"""))
|
|
|
|
sub = package.mkdir("sub")
|
|
|
|
sub.join("__init__.py").ensure()
|
|
|
|
sub.join("conftest.py").write(dedent("""\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def one():
|
|
|
|
return 2
|
|
|
|
"""))
|
|
|
|
sub.join("test_y.py").write(dedent("""\
|
|
|
|
def test_x(one):
|
|
|
|
assert one == 2
|
|
|
|
"""))
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
with runner.as_cwd():
|
|
|
|
reprec = testdir.inline_run("..")
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestAutouseDiscovery(object):
|
2016-07-12 09:03:53 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def testdir(self, testdir):
|
2012-07-24 18:10:04 +08:00
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
2012-10-17 15:21:04 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def perfunction(request, tmpdir):
|
2012-07-30 17:51:50 +08:00
|
|
|
pass
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg1(tmpdir):
|
2012-07-24 18:10:04 +08:00
|
|
|
pass
|
2012-10-17 15:21:04 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2012-08-01 19:57:09 +08:00
|
|
|
def perfunction2(arg1):
|
2012-07-24 18:10:04 +08:00
|
|
|
pass
|
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def fm(request):
|
2012-10-05 20:24:44 +08:00
|
|
|
return request._fixturemanager
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def item(request):
|
2012-07-24 18:10:04 +08:00
|
|
|
return request._pyfuncitem
|
|
|
|
""")
|
|
|
|
return testdir
|
|
|
|
|
|
|
|
def test_parsefactories_conftest(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
2013-08-02 05:48:40 +08:00
|
|
|
from _pytest.pytester import get_public_names
|
2012-07-24 18:10:04 +08:00
|
|
|
def test_check_setup(item, fm):
|
2012-10-16 22:13:12 +08:00
|
|
|
autousenames = fm._getautousenames(item.nodeid)
|
2013-08-02 05:48:40 +08:00
|
|
|
assert len(get_public_names(autousenames)) == 2
|
2012-10-16 22:13:12 +08:00
|
|
|
assert "perfunction2" in autousenames
|
|
|
|
assert "perfunction" in autousenames
|
2012-07-24 18:10:04 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-s")
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2012-10-16 19:47:59 +08:00
|
|
|
def test_two_classes_separated_autouse(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestA(object):
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-16 19:47:59 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup1(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
self.values.append(1)
|
2012-10-16 19:47:59 +08:00
|
|
|
def test_setup1(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert self.values == [1]
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestB(object):
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-16 19:47:59 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup2(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
self.values.append(1)
|
2012-10-16 19:47:59 +08:00
|
|
|
def test_setup2(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert self.values == [1]
|
2012-10-16 19:47:59 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2012-09-18 16:54:12 +08:00
|
|
|
def test_setup_at_classlevel(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2012-09-18 16:54:12 +08:00
|
|
|
def permethod(self, request):
|
|
|
|
request.instance.funcname = request.function.__name__
|
|
|
|
def test_method1(self):
|
|
|
|
assert self.funcname == "test_method1"
|
|
|
|
def test_method2(self):
|
|
|
|
assert self.funcname == "test_method2"
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-s")
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2012-09-25 21:04:30 +08:00
|
|
|
@pytest.mark.xfail(reason="'enabled' feature not implemented")
|
|
|
|
def test_setup_enabled_functionnode(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def enabled(parentnode, markers):
|
|
|
|
return "needsdb" in markers
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=[1,2])
|
2012-09-25 21:04:30 +08:00
|
|
|
def db(request):
|
|
|
|
return request.param
|
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(enabled=enabled, autouse=True)
|
2012-09-25 21:04:30 +08:00
|
|
|
def createdb(db):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_func1(request):
|
2012-10-05 20:24:44 +08:00
|
|
|
assert "db" not in request.fixturenames
|
2012-09-25 21:04:30 +08:00
|
|
|
|
|
|
|
@pytest.mark.needsdb
|
|
|
|
def test_func2(request):
|
2012-10-05 20:24:44 +08:00
|
|
|
assert "db" in request.fixturenames
|
2012-09-25 21:04:30 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-s")
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2012-08-19 18:36:49 +08:00
|
|
|
def test_callables_nocode(self, testdir):
|
|
|
|
"""
|
|
|
|
a imported mock.call would break setup/factory discovery
|
|
|
|
due to it being callable and __code__ not being a code object
|
|
|
|
"""
|
|
|
|
testdir.makepyfile("""
|
|
|
|
class _call(tuple):
|
|
|
|
def __call__(self, *k, **kw):
|
|
|
|
pass
|
|
|
|
def __getattr__(self, k):
|
|
|
|
return self
|
|
|
|
|
|
|
|
call = _call()
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-s")
|
|
|
|
reprec.assertoutcome(failed=0, passed=0)
|
|
|
|
|
2012-10-16 22:13:12 +08:00
|
|
|
def test_autouse_in_conftests(self, testdir):
|
|
|
|
a = testdir.mkdir("a")
|
|
|
|
b = testdir.mkdir("a1")
|
|
|
|
conftest = testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def hello():
|
|
|
|
xxx
|
|
|
|
""")
|
|
|
|
conftest.move(a.join(conftest.basename))
|
|
|
|
a.join("test_something.py").write("def test_func(): pass")
|
|
|
|
b.join("test_otherthing.py").write("def test_func(): pass")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-10-16 22:13:12 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*1 passed*1 error*
|
|
|
|
""")
|
|
|
|
|
|
|
|
def test_autouse_in_module_and_two_classes(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-16 22:13:12 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def append1():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("module")
|
2012-10-16 22:13:12 +08:00
|
|
|
def test_x():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["module"]
|
2012-10-16 22:13:12 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestA(object):
|
2012-10-16 22:13:12 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def append2(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("A")
|
2012-10-16 22:13:12 +08:00
|
|
|
def test_hello(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["module", "module", "A"], values
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestA2(object):
|
2012-10-16 22:13:12 +08:00
|
|
|
def test_world(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["module", "module", "A", "module"], values
|
2012-10-16 22:13:12 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=3)
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2012-11-20 05:17:55 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestAutouseManagement(object):
|
2012-11-20 05:17:55 +08:00
|
|
|
def test_autouse_conftest_mid_directory(self, testdir):
|
|
|
|
pkgdir = testdir.mkpydir("xyz123")
|
2015-11-27 22:43:01 +08:00
|
|
|
pkgdir.join("conftest.py").write(_pytest._code.Source("""
|
2012-11-20 05:17:55 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def app():
|
|
|
|
import sys
|
|
|
|
sys._myapp = "hello"
|
|
|
|
"""))
|
|
|
|
t = pkgdir.ensure("tests", "test_app.py")
|
2015-11-27 22:43:01 +08:00
|
|
|
t.write(_pytest._code.Source("""
|
2012-11-20 05:17:55 +08:00
|
|
|
import sys
|
|
|
|
def test_app():
|
|
|
|
assert sys._myapp == "hello"
|
|
|
|
"""))
|
|
|
|
reprec = testdir.inline_run("-s")
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2012-11-20 05:17:59 +08:00
|
|
|
def test_autouse_honored_for_yield(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def tst():
|
|
|
|
global x
|
|
|
|
x = 3
|
|
|
|
def test_gen():
|
|
|
|
def f(hello):
|
|
|
|
assert x == abs(hello)
|
|
|
|
yield f, 3
|
|
|
|
yield f, -3
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2012-07-24 18:10:04 +08:00
|
|
|
def test_funcarg_and_setup(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-07-24 18:10:04 +08:00
|
|
|
return 0
|
2013-07-07 03:30:24 +08:00
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
2012-08-01 19:57:09 +08:00
|
|
|
def something(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(2)
|
2012-07-24 18:10:04 +08:00
|
|
|
|
|
|
|
def test_hello(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 2
|
|
|
|
assert values == [1,2]
|
2012-07-24 18:10:04 +08:00
|
|
|
assert arg == 0
|
|
|
|
|
|
|
|
def test_hello2(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 2
|
|
|
|
assert values == [1,2]
|
2012-07-24 18:10:04 +08:00
|
|
|
assert arg == 0
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2012-10-17 15:21:04 +08:00
|
|
|
def test_uses_parametrized_resource(self, testdir):
|
2012-07-24 18:10:04 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=[1,2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2012-10-17 15:21:04 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2012-08-01 19:57:09 +08:00
|
|
|
def something(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-24 18:10:04 +08:00
|
|
|
|
|
|
|
def test_hello():
|
2017-11-04 23:17:20 +08:00
|
|
|
if len(values) == 1:
|
|
|
|
assert values == [1]
|
|
|
|
elif len(values) == 2:
|
|
|
|
assert values == [1, 2]
|
2012-07-24 18:10:04 +08:00
|
|
|
else:
|
|
|
|
0/0
|
|
|
|
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-s")
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2012-10-17 15:21:04 +08:00
|
|
|
def test_session_parametrized_function(self, testdir):
|
2012-07-24 18:10:04 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="session", params=[1,2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def append(request, arg):
|
|
|
|
if request.function.__name__ == "test_some":
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-24 18:10:04 +08:00
|
|
|
|
|
|
|
def test_some():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_result(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == arg
|
|
|
|
assert values[:arg] == [1,2][:arg]
|
2012-07-24 18:10:04 +08:00
|
|
|
""")
|
2012-07-30 16:46:03 +08:00
|
|
|
reprec = testdir.inline_run("-v", "-s")
|
2012-07-24 18:10:04 +08:00
|
|
|
reprec.assertoutcome(passed=4)
|
|
|
|
|
2012-08-01 15:07:32 +08:00
|
|
|
def test_class_function_parametrization_finalization(self, testdir):
|
|
|
|
p = testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
import pprint
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function", params=[1,2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def farg(request):
|
|
|
|
return request.param
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="class", params=list("ab"))
|
2012-09-17 22:36:10 +08:00
|
|
|
def carg(request):
|
|
|
|
return request.param
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def append(request, farg, carg):
|
2012-08-01 15:07:32 +08:00
|
|
|
def fin():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("fin_%s%s" % (carg, farg))
|
2012-09-17 22:36:10 +08:00
|
|
|
request.addfinalizer(fin)
|
2012-08-01 15:07:32 +08:00
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-08-01 15:07:32 +08:00
|
|
|
def test_1(self):
|
|
|
|
pass
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass2(object):
|
2012-08-01 15:07:32 +08:00
|
|
|
def test_2(self):
|
|
|
|
pass
|
|
|
|
""")
|
2016-07-14 15:14:19 +08:00
|
|
|
confcut = "--confcutdir={0}".format(testdir.tmpdir)
|
2017-07-17 07:25:08 +08:00
|
|
|
reprec = testdir.inline_run("-v", "-s", confcut)
|
2012-08-01 15:07:32 +08:00
|
|
|
reprec.assertoutcome(passed=8)
|
|
|
|
config = reprec.getcalls("pytest_unconfigure")[0].config
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.pluginmanager._getconftestmodules(p)[0].values
|
|
|
|
assert values == ["fin_a1", "fin_a2", "fin_b1", "fin_b2"] * 2
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-17 15:21:04 +08:00
|
|
|
def test_scope_ordering(self, testdir):
|
2012-08-13 19:37:14 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2012-08-13 19:37:14 +08:00
|
|
|
def fappend2():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(2)
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
2012-08-13 19:37:14 +08:00
|
|
|
def classappend3():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(3)
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
2012-08-13 19:37:14 +08:00
|
|
|
def mappend():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-08-13 19:37:14 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestHallo(object):
|
2012-08-13 19:37:14 +08:00
|
|
|
def test_method(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1,3,2]
|
2012-08-13 19:37:14 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2012-09-26 18:24:04 +08:00
|
|
|
def test_parametrization_setup_teardown_ordering(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-09-26 18:24:04 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
if metafunc.cls is not None:
|
|
|
|
metafunc.parametrize("item", [1,2], scope="class")
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
2012-09-26 18:24:04 +08:00
|
|
|
def addteardown(self, item, request):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("setup-%d" % item)
|
|
|
|
request.addfinalizer(lambda: values.append("teardown-%d" % item))
|
2012-09-26 18:24:04 +08:00
|
|
|
def test_step1(self, item):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("step1-%d" % item)
|
2012-09-26 18:24:04 +08:00
|
|
|
def test_step2(self, item):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("step2-%d" % item)
|
2012-09-26 18:24:04 +08:00
|
|
|
|
|
|
|
def test_finish():
|
2017-11-04 23:17:20 +08:00
|
|
|
print (values)
|
|
|
|
assert values == ["setup-1", "step1-1", "step2-1", "teardown-1",
|
2012-09-26 18:24:04 +08:00
|
|
|
"setup-2", "step1-2", "step2-2", "teardown-2",]
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=5)
|
|
|
|
|
2012-11-16 17:03:51 +08:00
|
|
|
def test_ordering_autouse_before_explicit(self, testdir):
|
2012-11-02 23:04:57 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def fix1():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture()
|
|
|
|
def arg1():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(2)
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_hello(arg1):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1,2]
|
2012-11-02 23:04:57 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
2012-09-26 18:24:04 +08:00
|
|
|
|
2012-11-16 17:03:51 +08:00
|
|
|
@pytest.mark.issue226
|
2017-07-17 07:25:08 +08:00
|
|
|
@pytest.mark.parametrize("param1", ["", "params=[1]"], ids=["p00", "p01"])
|
|
|
|
@pytest.mark.parametrize("param2", ["", "params=[1]"], ids=["p10", "p11"])
|
2012-11-16 17:03:51 +08:00
|
|
|
def test_ordering_dependencies_torndown_first(self, testdir, param1, param2):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-11-16 17:03:51 +08:00
|
|
|
@pytest.fixture(%(param1)s)
|
|
|
|
def arg1(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin1"))
|
|
|
|
values.append("new1")
|
2012-11-16 17:03:51 +08:00
|
|
|
@pytest.fixture(%(param2)s)
|
|
|
|
def arg2(request, arg1):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin2"))
|
|
|
|
values.append("new2")
|
2012-11-16 17:03:51 +08:00
|
|
|
|
|
|
|
def test_arg(arg2):
|
|
|
|
pass
|
|
|
|
def test_check():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["new1", "new2", "fin2", "fin1"]
|
2012-11-16 17:03:51 +08:00
|
|
|
""" % locals())
|
|
|
|
reprec = testdir.inline_run("-s")
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2014-01-27 19:53:44 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestFixtureMarker(object):
|
2012-07-20 20:16:46 +08:00
|
|
|
def test_parametrize(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(params=["a", "b", "c"])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-20 20:16:46 +08:00
|
|
|
def test_param(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-20 20:16:46 +08:00
|
|
|
def test_result():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == list("abc")
|
2012-07-20 20:16:46 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=4)
|
|
|
|
|
2015-08-08 08:09:08 +08:00
|
|
|
def test_multiple_parametrization_issue_736(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1,2,3])
|
|
|
|
def foo(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('foobar', [4,5,6])
|
|
|
|
def test_issue(foo, foobar):
|
|
|
|
assert foo in [1,2,3]
|
|
|
|
assert foobar in [4,5,6]
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=9)
|
|
|
|
|
2015-08-31 20:38:39 +08:00
|
|
|
@pytest.mark.parametrize('param_args', ["'fixt, val'", "'fixt,val'", "['fixt', 'val']", "('fixt', 'val')"])
|
|
|
|
def test_override_parametrized_fixture_issue_979(self, testdir, param_args):
|
2015-08-31 21:11:57 +08:00
|
|
|
"""Make sure a parametrized argument can override a parametrized fixture.
|
|
|
|
|
|
|
|
This was a regression introduced in the fix for #736.
|
|
|
|
"""
|
2015-08-31 20:38:39 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[1, 2])
|
|
|
|
def fixt(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(%s, [(3, 'x'), (4, 'x')])
|
|
|
|
def test_foo(fixt, val):
|
|
|
|
pass
|
|
|
|
""" % param_args)
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
2012-07-20 20:16:46 +08:00
|
|
|
def test_scope_session(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-07-20 20:16:46 +08:00
|
|
|
return 1
|
|
|
|
|
|
|
|
def test_1(arg):
|
|
|
|
assert arg == 1
|
|
|
|
def test_2(arg):
|
|
|
|
assert arg == 1
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-07-20 20:16:46 +08:00
|
|
|
def test3(self, arg):
|
|
|
|
assert arg == 1
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2012-07-20 20:16:46 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=3)
|
|
|
|
|
2014-04-16 10:22:41 +08:00
|
|
|
def test_scope_session_exc(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2014-04-16 10:22:41 +08:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def fix():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2014-04-16 10:22:41 +08:00
|
|
|
pytest.skip('skipping')
|
|
|
|
|
|
|
|
def test_1(fix):
|
|
|
|
pass
|
|
|
|
def test_2(fix):
|
|
|
|
pass
|
|
|
|
def test_last():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1]
|
2014-04-16 10:22:41 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(skipped=2, passed=1)
|
|
|
|
|
2014-06-16 02:57:52 +08:00
|
|
|
def test_scope_session_exc_two_fix(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2014-06-16 02:57:52 +08:00
|
|
|
m = []
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def a():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2014-06-16 02:57:52 +08:00
|
|
|
pytest.skip('skipping')
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def b(a):
|
|
|
|
m.append(1)
|
|
|
|
|
|
|
|
def test_1(b):
|
|
|
|
pass
|
|
|
|
def test_2(b):
|
|
|
|
pass
|
|
|
|
def test_last():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1]
|
2014-06-16 02:57:52 +08:00
|
|
|
assert m == []
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(skipped=2, passed=1)
|
|
|
|
|
2014-07-18 08:30:29 +08:00
|
|
|
def test_scope_exc(self, testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
test_foo="""
|
|
|
|
def test_foo(fix):
|
|
|
|
pass
|
|
|
|
""",
|
|
|
|
test_bar="""
|
|
|
|
def test_bar(fix):
|
|
|
|
pass
|
|
|
|
""",
|
|
|
|
conftest="""
|
|
|
|
import pytest
|
|
|
|
reqs = []
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def fix(request):
|
|
|
|
reqs.append(1)
|
|
|
|
pytest.skip()
|
|
|
|
@pytest.fixture
|
|
|
|
def req_list():
|
|
|
|
return reqs
|
|
|
|
""",
|
|
|
|
test_real="""
|
|
|
|
def test_last(req_list):
|
|
|
|
assert req_list == [1]
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(skipped=2, passed=1)
|
|
|
|
|
2012-07-20 20:16:46 +08:00
|
|
|
def test_scope_module_uses_session(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-07-20 20:16:46 +08:00
|
|
|
return 1
|
|
|
|
|
|
|
|
def test_1(arg):
|
|
|
|
assert arg == 1
|
|
|
|
def test_2(arg):
|
|
|
|
assert arg == 1
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-07-20 20:16:46 +08:00
|
|
|
def test3(self, arg):
|
|
|
|
assert arg == 1
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2012-07-20 20:16:46 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=3)
|
|
|
|
|
|
|
|
def test_scope_module_and_finalizer(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
2016-07-12 09:03:53 +08:00
|
|
|
finalized_list = []
|
|
|
|
created_list = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
2016-07-12 09:03:53 +08:00
|
|
|
created_list.append(1)
|
2012-09-17 22:36:10 +08:00
|
|
|
assert request.scope == "module"
|
2016-07-12 09:03:53 +08:00
|
|
|
request.addfinalizer(lambda: finalized_list.append(1))
|
|
|
|
@pytest.fixture
|
|
|
|
def created(request):
|
|
|
|
return len(created_list)
|
|
|
|
@pytest.fixture
|
|
|
|
def finalized(request):
|
|
|
|
return len(finalized_list)
|
2012-07-20 20:16:46 +08:00
|
|
|
""")
|
|
|
|
testdir.makepyfile(
|
|
|
|
test_mod1="""
|
|
|
|
def test_1(arg, created, finalized):
|
|
|
|
assert created == 1
|
|
|
|
assert finalized == 0
|
|
|
|
def test_2(arg, created, finalized):
|
|
|
|
assert created == 1
|
|
|
|
assert finalized == 0""",
|
|
|
|
test_mod2="""
|
|
|
|
def test_3(arg, created, finalized):
|
|
|
|
assert created == 2
|
|
|
|
assert finalized == 1""",
|
|
|
|
test_mode3="""
|
|
|
|
def test_4(arg, created, finalized):
|
|
|
|
assert created == 3
|
|
|
|
assert finalized == 2
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=4)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("method", [
|
2016-06-21 18:09:55 +08:00
|
|
|
'request.getfixturevalue("arg")',
|
2012-07-20 20:16:46 +08:00
|
|
|
'request.cached_setup(lambda: None, scope="function")',
|
2016-06-21 18:09:55 +08:00
|
|
|
], ids=["getfixturevalue", "cached_setup"])
|
2013-10-12 21:39:22 +08:00
|
|
|
def test_scope_mismatch_various(self, testdir, method):
|
2012-07-20 20:16:46 +08:00
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
finalized = []
|
|
|
|
created = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function")
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
2012-07-20 20:16:46 +08:00
|
|
|
pass
|
|
|
|
""")
|
|
|
|
testdir.makepyfile(
|
|
|
|
test_mod1="""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="session")
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
2012-07-20 20:16:46 +08:00
|
|
|
%s
|
|
|
|
def test_1(arg):
|
|
|
|
pass
|
|
|
|
""" % method)
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-07-20 20:16:46 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines([
|
2012-07-23 16:55:09 +08:00
|
|
|
"*ScopeMismatch*You tried*function*session*request*",
|
2012-07-20 20:16:46 +08:00
|
|
|
])
|
|
|
|
|
2012-07-20 20:16:49 +08:00
|
|
|
def test_register_only_with_mark(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg():
|
2012-07-20 20:16:49 +08:00
|
|
|
return 1
|
|
|
|
""")
|
|
|
|
testdir.makepyfile(
|
|
|
|
test_mod1="""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg(arg):
|
|
|
|
return arg + 1
|
2012-07-20 20:16:49 +08:00
|
|
|
def test_1(arg):
|
|
|
|
assert arg == 2
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2012-07-20 20:16:50 +08:00
|
|
|
def test_parametrize_and_scope(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params=["a", "b", "c"])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-20 20:16:50 +08:00
|
|
|
def test_param(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-20 20:16:50 +08:00
|
|
|
""")
|
2012-08-01 15:07:32 +08:00
|
|
|
reprec = testdir.inline_run("-v")
|
|
|
|
reprec.assertoutcome(passed=3)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
|
|
|
assert len(values) == 3
|
|
|
|
assert "a" in values
|
|
|
|
assert "b" in values
|
|
|
|
assert "c" in values
|
2012-07-30 16:46:03 +08:00
|
|
|
|
|
|
|
def test_scope_mismatch(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function")
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
2012-07-30 16:46:03 +08:00
|
|
|
pass
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="session")
|
2012-08-01 19:57:09 +08:00
|
|
|
def arg(arg):
|
2012-07-30 16:46:03 +08:00
|
|
|
pass
|
|
|
|
def test_mismatch(arg):
|
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-07-30 16:46:03 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*ScopeMismatch*",
|
|
|
|
"*1 error*",
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_parametrize_separated_order(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params=[1, 2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_2(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-v")
|
2012-08-01 15:07:32 +08:00
|
|
|
reprec.assertoutcome(passed=4)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
|
|
|
assert values == [1, 1, 2, 2]
|
2012-08-01 15:07:32 +08:00
|
|
|
|
|
|
|
def test_module_parametrized_ordering(self, testdir):
|
2017-09-28 01:42:55 +08:00
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
console_output_style=classic
|
|
|
|
""")
|
2012-08-01 15:07:32 +08:00
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="session", params="s1 s2".split())
|
2012-08-01 19:57:09 +08:00
|
|
|
def sarg():
|
2012-08-01 15:07:32 +08:00
|
|
|
pass
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params="m1 m2".split())
|
2012-08-01 19:57:09 +08:00
|
|
|
def marg():
|
2012-08-01 15:07:32 +08:00
|
|
|
pass
|
|
|
|
""")
|
|
|
|
testdir.makepyfile(test_mod1="""
|
|
|
|
def test_func(sarg):
|
|
|
|
pass
|
|
|
|
def test_func1(marg):
|
|
|
|
pass
|
|
|
|
""", test_mod2="""
|
|
|
|
def test_func2(sarg):
|
|
|
|
pass
|
|
|
|
def test_func3(sarg, marg):
|
|
|
|
pass
|
|
|
|
def test_func3b(sarg, marg):
|
|
|
|
pass
|
|
|
|
def test_func4(marg):
|
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v")
|
2012-08-01 15:07:32 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
2014-08-01 07:29:35 +08:00
|
|
|
test_mod1.py::test_func[s1] PASSED
|
|
|
|
test_mod2.py::test_func2[s1] PASSED
|
|
|
|
test_mod2.py::test_func3[s1-m1] PASSED
|
|
|
|
test_mod2.py::test_func3b[s1-m1] PASSED
|
|
|
|
test_mod2.py::test_func3[s1-m2] PASSED
|
|
|
|
test_mod2.py::test_func3b[s1-m2] PASSED
|
|
|
|
test_mod1.py::test_func[s2] PASSED
|
|
|
|
test_mod2.py::test_func2[s2] PASSED
|
|
|
|
test_mod2.py::test_func3[s2-m1] PASSED
|
|
|
|
test_mod2.py::test_func3b[s2-m1] PASSED
|
|
|
|
test_mod2.py::test_func4[m1] PASSED
|
|
|
|
test_mod2.py::test_func3[s2-m2] PASSED
|
|
|
|
test_mod2.py::test_func3b[s2-m2] PASSED
|
|
|
|
test_mod2.py::test_func4[m2] PASSED
|
|
|
|
test_mod1.py::test_func1[m1] PASSED
|
|
|
|
test_mod1.py::test_func1[m2] PASSED
|
2012-08-01 15:07:32 +08:00
|
|
|
""")
|
|
|
|
|
|
|
|
def test_class_ordering(self, testdir):
|
2017-09-28 01:42:55 +08:00
|
|
|
testdir.makeini("""
|
|
|
|
[pytest]
|
|
|
|
console_output_style=classic
|
|
|
|
""")
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makeconftest("""
|
2012-08-01 15:07:32 +08:00
|
|
|
import pytest
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function", params=[1,2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def farg(request):
|
|
|
|
return request.param
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="class", params=list("ab"))
|
2012-09-17 22:36:10 +08:00
|
|
|
def carg(request):
|
|
|
|
return request.param
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="function", autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def append(request, farg, carg):
|
2012-08-01 15:07:32 +08:00
|
|
|
def fin():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("fin_%s%s" % (carg, farg))
|
2012-09-17 22:36:10 +08:00
|
|
|
request.addfinalizer(fin)
|
2012-08-01 15:07:32 +08:00
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass2(object):
|
2012-08-01 15:07:32 +08:00
|
|
|
def test_1(self):
|
|
|
|
pass
|
|
|
|
def test_2(self):
|
|
|
|
pass
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-08-01 15:07:32 +08:00
|
|
|
def test_3(self):
|
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-vs")
|
2012-08-01 15:07:32 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
2014-08-01 07:29:35 +08:00
|
|
|
test_class_ordering.py::TestClass2::test_1[1-a] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_1[2-a] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_2[1-a] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_2[2-a] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_1[1-b] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_1[2-b] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_2[1-b] PASSED
|
|
|
|
test_class_ordering.py::TestClass2::test_2[2-b] PASSED
|
|
|
|
test_class_ordering.py::TestClass::test_3[1-a] PASSED
|
|
|
|
test_class_ordering.py::TestClass::test_3[2-a] PASSED
|
|
|
|
test_class_ordering.py::TestClass::test_3[1-b] PASSED
|
|
|
|
test_class_ordering.py::TestClass::test_3[2-b] PASSED
|
2012-08-01 15:07:32 +08:00
|
|
|
""")
|
2012-07-30 16:46:03 +08:00
|
|
|
|
|
|
|
def test_parametrize_separated_order_higher_scope_first(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function", params=[1, 2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
param = request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin:%s" % param))
|
|
|
|
values.append("create:%s" % param)
|
2012-09-17 22:36:10 +08:00
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params=["mod1", "mod2"])
|
2012-09-17 22:36:10 +08:00
|
|
|
def modarg(request):
|
|
|
|
param = request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin:%s" % param))
|
|
|
|
values.append("create:%s" % param)
|
2012-09-17 22:36:10 +08:00
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test1")
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_2(modarg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test2")
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_3(arg, modarg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test3")
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_4(modarg, arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test4")
|
2012-07-30 16:46:03 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-v")
|
2013-12-07 23:37:46 +08:00
|
|
|
reprec.assertoutcome(passed=12)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
2013-12-07 23:37:46 +08:00
|
|
|
expected = [
|
|
|
|
'create:1', 'test1', 'fin:1', 'create:2', 'test1',
|
|
|
|
'fin:2', 'create:mod1', 'test2', 'create:1', 'test3',
|
|
|
|
'fin:1', 'create:2', 'test3', 'fin:2', 'create:1',
|
|
|
|
'test4', 'fin:1', 'create:2', 'test4', 'fin:2',
|
|
|
|
'fin:mod1', 'create:mod2', 'test2', 'create:1', 'test3',
|
|
|
|
'fin:1', 'create:2', 'test3', 'fin:2', 'create:1',
|
|
|
|
'test4', 'fin:1', 'create:2', 'test4', 'fin:2',
|
2017-07-17 07:25:07 +08:00
|
|
|
'fin:mod2']
|
2013-12-07 23:37:46 +08:00
|
|
|
import pprint
|
2017-11-04 23:17:20 +08:00
|
|
|
pprint.pprint(list(zip(values, expected)))
|
|
|
|
assert values == expected
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2012-10-22 18:16:54 +08:00
|
|
|
def test_parametrized_fixture_teardown_order(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture(params=[1,2], scope="class")
|
|
|
|
def param1(request):
|
|
|
|
return request.param
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-22 18:16:54 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2012-10-22 18:16:54 +08:00
|
|
|
@classmethod
|
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
|
|
|
def setup1(self, request, param1):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2012-10-22 18:16:54 +08:00
|
|
|
request.addfinalizer(self.teardown1)
|
|
|
|
@classmethod
|
|
|
|
def teardown1(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values.pop() == 1
|
2012-10-22 18:16:54 +08:00
|
|
|
@pytest.fixture(scope="class", autouse=True)
|
|
|
|
def setup2(self, request, param1):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(2)
|
2012-10-22 18:16:54 +08:00
|
|
|
request.addfinalizer(self.teardown2)
|
|
|
|
@classmethod
|
|
|
|
def teardown2(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values.pop() == 2
|
2012-10-22 18:16:54 +08:00
|
|
|
def test(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_finish():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert not values
|
2012-10-22 18:16:54 +08:00
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-v")
|
2012-10-22 18:16:54 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*3 passed*
|
|
|
|
""")
|
|
|
|
assert "error" not in result.stdout.str()
|
|
|
|
|
2013-11-21 16:26:45 +08:00
|
|
|
def test_fixture_finalizer(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def browser(request):
|
|
|
|
|
|
|
|
def finalize():
|
|
|
|
sys.stdout.write('Finalized')
|
|
|
|
request.addfinalizer(finalize)
|
|
|
|
return {}
|
|
|
|
""")
|
|
|
|
b = testdir.mkdir("subdir")
|
2016-07-24 00:09:42 +08:00
|
|
|
b.join("test_overridden_fixture_finalizer.py").write(dedent("""
|
2013-11-21 16:26:45 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def browser(browser):
|
|
|
|
browser['visited'] = True
|
|
|
|
return browser
|
|
|
|
|
|
|
|
def test_browser(browser):
|
|
|
|
assert browser['visited'] is True
|
|
|
|
"""))
|
2015-04-28 18:05:08 +08:00
|
|
|
reprec = testdir.runpytest("-s")
|
2013-11-21 16:26:45 +08:00
|
|
|
for test in ['test_browser']:
|
|
|
|
reprec.stdout.fnmatch_lines('*Finalized*')
|
|
|
|
|
|
|
|
def test_class_scope_with_normal_tests(self, testdir):
|
|
|
|
testpath = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Box(object):
|
2013-11-21 16:26:45 +08:00
|
|
|
value = 0
|
|
|
|
|
|
|
|
@pytest.fixture(scope='class')
|
|
|
|
def a(request):
|
|
|
|
Box.value += 1
|
|
|
|
return Box.value
|
|
|
|
|
|
|
|
def test_a(a):
|
|
|
|
assert a == 1
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Test1(object):
|
2013-11-21 16:26:45 +08:00
|
|
|
def test_b(self, a):
|
|
|
|
assert a == 2
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class Test2(object):
|
2013-11-21 16:26:45 +08:00
|
|
|
def test_c(self, a):
|
|
|
|
assert a == 3""")
|
|
|
|
reprec = testdir.inline_run(testpath)
|
|
|
|
for test in ['test_a', 'test_b', 'test_c']:
|
|
|
|
assert reprec.matchreport(test).passed
|
|
|
|
|
2013-11-21 19:21:52 +08:00
|
|
|
def test_request_is_clean(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2013-11-21 19:21:52 +08:00
|
|
|
@pytest.fixture(params=[1, 2])
|
|
|
|
def fix(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append(request.param))
|
2013-11-21 19:21:52 +08:00
|
|
|
def test_fix(fix):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-s")
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
|
|
|
assert values == [1, 2]
|
2013-11-21 19:21:52 +08:00
|
|
|
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_parametrize_separated_lifecycle(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params=[1, 2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
x = request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin%s" % x))
|
2012-09-17 22:36:10 +08:00
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_2(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
""")
|
2013-11-21 21:16:44 +08:00
|
|
|
reprec = testdir.inline_run("-vs")
|
2012-08-01 15:07:32 +08:00
|
|
|
reprec.assertoutcome(passed=4)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
2012-08-01 15:07:32 +08:00
|
|
|
import pprint
|
2017-11-04 23:17:20 +08:00
|
|
|
pprint.pprint(values)
|
|
|
|
# assert len(values) == 6
|
|
|
|
assert values[0] == values[1] == 1
|
|
|
|
assert values[2] == "fin1"
|
|
|
|
assert values[3] == values[4] == 2
|
|
|
|
assert values[5] == "fin2"
|
2012-08-01 15:07:32 +08:00
|
|
|
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_parametrize_function_scoped_finalizers_called(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="function", params=[1, 2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
x = request.param
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin%s" % x))
|
2012-09-17 22:36:10 +08:00
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_2(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_3():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 8
|
|
|
|
assert values == [1, "fin1", 2, "fin2", 1, "fin1", 2, "fin2"]
|
2012-07-30 16:46:03 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-v")
|
|
|
|
reprec.assertoutcome(passed=5)
|
|
|
|
|
2013-11-21 16:42:24 +08:00
|
|
|
@pytest.mark.issue246
|
2013-11-21 21:16:44 +08:00
|
|
|
@pytest.mark.parametrize("scope", ["session", "function", "module"])
|
2013-11-21 16:42:24 +08:00
|
|
|
def test_finalizer_order_on_parametrization(self, scope, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2013-11-21 16:42:24 +08:00
|
|
|
|
|
|
|
@pytest.fixture(scope=%(scope)r, params=["1"])
|
|
|
|
def fix1(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture(scope=%(scope)r)
|
|
|
|
def fix2(request, base):
|
|
|
|
def cleanup_fix2():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert not values, "base should not have been finalized"
|
2013-11-21 16:42:24 +08:00
|
|
|
request.addfinalizer(cleanup_fix2)
|
|
|
|
|
|
|
|
@pytest.fixture(scope=%(scope)r)
|
|
|
|
def base(request, fix1):
|
|
|
|
def cleanup_base():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("fin_base")
|
2013-12-04 23:09:37 +08:00
|
|
|
print ("finalizing base")
|
2013-11-21 16:42:24 +08:00
|
|
|
request.addfinalizer(cleanup_base)
|
|
|
|
|
2013-12-04 23:09:37 +08:00
|
|
|
def test_begin():
|
|
|
|
pass
|
2013-11-21 16:42:24 +08:00
|
|
|
def test_baz(base, fix2):
|
|
|
|
pass
|
2013-11-21 21:16:44 +08:00
|
|
|
def test_other():
|
|
|
|
pass
|
2013-11-21 16:42:24 +08:00
|
|
|
""" % {"scope": scope})
|
2013-12-04 23:09:37 +08:00
|
|
|
reprec = testdir.inline_run("-lvs")
|
|
|
|
reprec.assertoutcome(passed=3)
|
2013-11-21 16:42:24 +08:00
|
|
|
|
2013-12-08 03:55:17 +08:00
|
|
|
@pytest.mark.issue396
|
|
|
|
def test_class_scope_parametrization_ordering(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2013-12-08 03:55:17 +08:00
|
|
|
@pytest.fixture(params=["John", "Doe"], scope="class")
|
|
|
|
def human(request):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin %s" % request.param))
|
2013-12-08 03:55:17 +08:00
|
|
|
return request.param
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestGreetings(object):
|
2013-12-08 03:55:17 +08:00
|
|
|
def test_hello(self, human):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test_hello")
|
2013-12-08 03:55:17 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestMetrics(object):
|
2013-12-08 03:55:17 +08:00
|
|
|
def test_name(self, human):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test_name")
|
2013-12-08 03:55:17 +08:00
|
|
|
|
|
|
|
def test_population(self, human):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append("test_population")
|
2013-12-08 03:55:17 +08:00
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=6)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_runtest_call")[0].item.module.values
|
|
|
|
assert values == ["test_hello", "fin John", "test_hello", "fin Doe",
|
|
|
|
"test_name", "test_population", "fin John",
|
|
|
|
"test_name", "test_population", "fin Doe"]
|
2013-12-08 03:55:17 +08:00
|
|
|
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_parametrize_setup_function(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope="module", params=[1, 2])
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
|
|
|
return request.param
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def mysetup(request, arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
request.addfinalizer(lambda: values.append("fin%s" % arg))
|
|
|
|
values.append("setup%s" % arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_1(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_2(arg):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(arg)
|
2012-07-30 16:46:03 +08:00
|
|
|
def test_3():
|
|
|
|
import pprint
|
2017-11-04 23:17:20 +08:00
|
|
|
pprint.pprint(values)
|
2012-08-01 15:07:32 +08:00
|
|
|
if arg == 1:
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["setup1", 1, 1, ]
|
2012-08-01 15:07:32 +08:00
|
|
|
elif arg == 2:
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == ["setup1", 1, 1, "fin1",
|
2012-08-01 15:07:32 +08:00
|
|
|
"setup2", 2, 2, ]
|
2012-07-30 16:46:03 +08:00
|
|
|
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run("-v")
|
2012-08-01 15:07:32 +08:00
|
|
|
reprec.assertoutcome(passed=6)
|
2012-07-30 16:46:03 +08:00
|
|
|
|
2013-04-29 16:31:51 +08:00
|
|
|
def test_fixture_marked_function_not_collected_as_test(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def test_app():
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def test_something(test_app):
|
|
|
|
assert test_app == 1
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2013-12-16 06:15:15 +08:00
|
|
|
def test_params_and_ids(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[object(), object()],
|
|
|
|
ids=['alpha', 'beta'])
|
|
|
|
def fix(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_foo(fix):
|
|
|
|
assert 1
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
res = testdir.runpytest('-v')
|
2013-12-16 06:15:15 +08:00
|
|
|
res.stdout.fnmatch_lines([
|
|
|
|
'*test_foo*alpha*',
|
|
|
|
'*test_foo*beta*'])
|
|
|
|
|
|
|
|
def test_params_and_ids_yieldfixture(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.yield_fixture(params=[object(), object()],
|
|
|
|
ids=['alpha', 'beta'])
|
|
|
|
def fix(request):
|
|
|
|
yield request.param
|
|
|
|
|
|
|
|
def test_foo(fix):
|
|
|
|
assert 1
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
res = testdir.runpytest('-v')
|
2013-12-16 06:15:15 +08:00
|
|
|
res.stdout.fnmatch_lines([
|
|
|
|
'*test_foo*alpha*',
|
|
|
|
'*test_foo*beta*'])
|
|
|
|
|
2017-07-26 17:08:54 +08:00
|
|
|
@pytest.mark.issue920
|
|
|
|
def test_deterministic_fixture_collection(self, testdir, monkeypatch):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module",
|
|
|
|
params=["A",
|
|
|
|
"B",
|
|
|
|
"C"])
|
|
|
|
def A(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module",
|
|
|
|
params=["DDDDDDDDD", "EEEEEEEEEEEE", "FFFFFFFFFFF", "banansda"])
|
|
|
|
def B(request, A):
|
|
|
|
return request.param
|
2013-04-29 16:31:51 +08:00
|
|
|
|
2017-07-26 17:08:54 +08:00
|
|
|
def test_foo(B):
|
|
|
|
# Something funky is going on here.
|
|
|
|
# Despite specified seeds, on what is collected,
|
|
|
|
# sometimes we get unexpected passes. hashing B seems
|
|
|
|
# to help?
|
|
|
|
assert hash(B) or True
|
|
|
|
""")
|
|
|
|
monkeypatch.setenv("PYTHONHASHSEED", "1")
|
|
|
|
out1 = testdir.runpytest_subprocess("-v")
|
|
|
|
monkeypatch.setenv("PYTHONHASHSEED", "2")
|
|
|
|
out2 = testdir.runpytest_subprocess("-v")
|
|
|
|
out1 = [line for line in out1.outlines if line.startswith("test_deterministic_fixture_collection.py::test_foo")]
|
|
|
|
out2 = [line for line in out2.outlines if line.startswith("test_deterministic_fixture_collection.py::test_foo")]
|
|
|
|
assert len(out1) == 12
|
|
|
|
assert out1 == out2
|
|
|
|
|
2013-04-29 16:31:51 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestRequestScopeAccess(object):
|
2017-07-17 07:25:08 +08:00
|
|
|
pytestmark = pytest.mark.parametrize(("scope", "ok", "error"), [
|
2012-09-17 23:32:23 +08:00
|
|
|
["session", "", "fspath class function module"],
|
|
|
|
["module", "module fspath", "cls function"],
|
|
|
|
["class", "module fspath cls", "function"],
|
|
|
|
["function", "module fspath cls function", ""]
|
|
|
|
])
|
|
|
|
|
2012-08-01 19:57:09 +08:00
|
|
|
def test_setup(self, testdir, scope, ok, error):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope=%r, autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def myscoped(request):
|
2012-08-01 19:57:09 +08:00
|
|
|
for x in %r:
|
2012-09-17 22:36:10 +08:00
|
|
|
assert hasattr(request, x)
|
2012-08-01 19:57:09 +08:00
|
|
|
for x in %r:
|
|
|
|
pytest.raises(AttributeError, lambda:
|
2012-09-17 22:36:10 +08:00
|
|
|
getattr(request, x))
|
|
|
|
assert request.session
|
|
|
|
assert request.config
|
2012-08-01 19:57:09 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
2017-07-17 07:25:08 +08:00
|
|
|
""" % (scope, ok.split(), error.split()))
|
2012-09-17 22:36:10 +08:00
|
|
|
reprec = testdir.inline_run("-l")
|
2012-08-01 19:57:09 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2012-08-08 17:48:53 +08:00
|
|
|
def test_funcarg(self, testdir, scope, ok, error):
|
2012-08-01 19:57:09 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture(scope=%r)
|
2012-09-17 22:36:10 +08:00
|
|
|
def arg(request):
|
2012-08-01 19:57:09 +08:00
|
|
|
for x in %r:
|
2012-09-17 22:36:10 +08:00
|
|
|
assert hasattr(request, x)
|
2012-08-01 19:57:09 +08:00
|
|
|
for x in %r:
|
|
|
|
pytest.raises(AttributeError, lambda:
|
2012-09-17 22:36:10 +08:00
|
|
|
getattr(request, x))
|
|
|
|
assert request.session
|
|
|
|
assert request.config
|
2012-08-01 19:57:09 +08:00
|
|
|
def test_func(arg):
|
|
|
|
pass
|
2017-07-17 07:25:08 +08:00
|
|
|
""" % (scope, ok.split(), error.split()))
|
2012-08-01 19:57:09 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestErrors(object):
|
2012-08-08 20:53:47 +08:00
|
|
|
def test_subfactory_missing_funcarg(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-05 16:21:35 +08:00
|
|
|
@pytest.fixture()
|
2012-09-17 22:36:10 +08:00
|
|
|
def gen(qwe123):
|
2012-08-08 20:53:47 +08:00
|
|
|
return 1
|
|
|
|
def test_something(gen):
|
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-08-08 20:53:47 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines([
|
2012-09-17 22:36:10 +08:00
|
|
|
"*def gen(qwe123):*",
|
2012-10-05 20:24:45 +08:00
|
|
|
"*fixture*qwe123*not found*",
|
2012-08-08 20:53:47 +08:00
|
|
|
"*1 error*",
|
|
|
|
])
|
|
|
|
|
2014-04-07 19:29:57 +08:00
|
|
|
def test_issue498_fixture_finalizer_failing(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fix1(request):
|
|
|
|
def f():
|
|
|
|
raise KeyError
|
2014-04-15 05:42:02 +08:00
|
|
|
request.addfinalizer(f)
|
2014-04-07 19:29:57 +08:00
|
|
|
return object()
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2014-04-07 19:29:57 +08:00
|
|
|
def test_1(fix1):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(fix1)
|
2014-04-07 19:29:57 +08:00
|
|
|
def test_2(fix1):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(fix1)
|
2014-04-07 19:29:57 +08:00
|
|
|
def test_3():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values[0] != values[1]
|
2014-04-07 19:29:57 +08:00
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2014-04-07 19:29:57 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*ERROR*teardown*test_1*
|
|
|
|
*KeyError*
|
|
|
|
*ERROR*teardown*test_2*
|
|
|
|
*KeyError*
|
|
|
|
*3 pass*2 error*
|
|
|
|
""")
|
2014-04-15 05:42:02 +08:00
|
|
|
|
2012-08-08 20:53:47 +08:00
|
|
|
def test_setupfunc_missing_funcarg(self, testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2012-10-17 15:21:04 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2012-09-17 22:36:10 +08:00
|
|
|
def gen(qwe123):
|
2012-08-08 20:53:47 +08:00
|
|
|
return 1
|
|
|
|
def test_something():
|
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest()
|
2012-08-08 20:53:47 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines([
|
2012-09-17 22:36:10 +08:00
|
|
|
"*def gen(qwe123):*",
|
2012-10-05 20:24:45 +08:00
|
|
|
"*fixture*qwe123*not found*",
|
2012-08-08 20:53:47 +08:00
|
|
|
"*1 error*",
|
|
|
|
])
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestShowFixtures(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_funcarg_compat(self, testdir):
|
|
|
|
config = testdir.parseconfigure("--funcargs")
|
|
|
|
assert config.option.showfixtures
|
2012-08-08 17:48:53 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_show_fixtures(self, testdir):
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--fixtures")
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2017-07-17 07:25:07 +08:00
|
|
|
"*tmpdir*",
|
|
|
|
"*temporary directory*",
|
|
|
|
])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_show_fixtures_verbose(self, testdir):
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--fixtures", "-v")
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2017-07-17 07:25:07 +08:00
|
|
|
"*tmpdir*--*tmpdir.py*",
|
|
|
|
"*temporary directory*",
|
|
|
|
])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_show_fixtures_testmodule(self, testdir):
|
|
|
|
p = testdir.makepyfile('''
|
2012-08-01 19:57:09 +08:00
|
|
|
import pytest
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def _arg0():
|
|
|
|
""" hidden """
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
""" hello world """
|
|
|
|
''')
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--fixtures", p)
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*tmpdir
|
|
|
|
*fixtures defined from*
|
|
|
|
*arg1*
|
|
|
|
*hello world*
|
2012-08-01 19:57:09 +08:00
|
|
|
""")
|
2012-11-02 23:04:57 +08:00
|
|
|
assert "arg0" not in result.stdout.str()
|
2012-08-01 19:57:09 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.mark.parametrize("testmod", [True, False])
|
|
|
|
def test_show_fixtures_conftest(self, testdir, testmod):
|
|
|
|
testdir.makeconftest('''
|
2012-08-01 19:57:09 +08:00
|
|
|
import pytest
|
2012-11-02 23:04:57 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
""" hello world """
|
|
|
|
''')
|
|
|
|
if testmod:
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_hello():
|
|
|
|
pass
|
|
|
|
""")
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--fixtures")
|
2012-11-02 23:04:57 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*tmpdir*
|
|
|
|
*fixtures defined from*conftest*
|
|
|
|
*arg1*
|
|
|
|
*hello world*
|
2012-08-01 19:57:09 +08:00
|
|
|
""")
|
2012-09-17 22:36:10 +08:00
|
|
|
|
2015-03-05 00:00:24 +08:00
|
|
|
def test_show_fixtures_trimmed_doc(self, testdir):
|
2017-07-15 19:33:11 +08:00
|
|
|
p = testdir.makepyfile(dedent('''
|
2015-03-05 00:00:24 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
"""
|
|
|
|
line1
|
|
|
|
line2
|
|
|
|
|
|
|
|
"""
|
|
|
|
@pytest.fixture
|
|
|
|
def arg2():
|
|
|
|
"""
|
|
|
|
line1
|
|
|
|
line2
|
|
|
|
|
|
|
|
"""
|
2017-07-15 19:33:11 +08:00
|
|
|
'''))
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("--fixtures", p)
|
2017-07-15 19:33:11 +08:00
|
|
|
result.stdout.fnmatch_lines(dedent("""
|
2015-03-26 16:34:10 +08:00
|
|
|
* fixtures defined from test_show_fixtures_trimmed_doc *
|
2015-03-05 00:00:24 +08:00
|
|
|
arg2
|
|
|
|
line1
|
|
|
|
line2
|
|
|
|
arg1
|
|
|
|
line1
|
|
|
|
line2
|
|
|
|
|
2017-07-15 19:33:11 +08:00
|
|
|
"""))
|
|
|
|
|
|
|
|
def test_show_fixtures_indented_doc(self, testdir):
|
|
|
|
p = testdir.makepyfile(dedent('''
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fixture1():
|
|
|
|
"""
|
|
|
|
line1
|
|
|
|
indented line
|
|
|
|
"""
|
|
|
|
'''))
|
|
|
|
result = testdir.runpytest("--fixtures", p)
|
|
|
|
result.stdout.fnmatch_lines(dedent("""
|
|
|
|
* fixtures defined from test_show_fixtures_indented_doc *
|
|
|
|
fixture1
|
|
|
|
line1
|
|
|
|
indented line
|
|
|
|
"""))
|
|
|
|
|
|
|
|
def test_show_fixtures_indented_doc_first_line_unindented(self, testdir):
|
|
|
|
p = testdir.makepyfile(dedent('''
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fixture1():
|
|
|
|
"""line1
|
|
|
|
line2
|
|
|
|
indented line
|
|
|
|
"""
|
|
|
|
'''))
|
|
|
|
result = testdir.runpytest("--fixtures", p)
|
|
|
|
result.stdout.fnmatch_lines(dedent("""
|
|
|
|
* fixtures defined from test_show_fixtures_indented_doc_first_line_unindented *
|
|
|
|
fixture1
|
|
|
|
line1
|
|
|
|
line2
|
|
|
|
indented line
|
|
|
|
"""))
|
2013-05-05 20:48:37 +08:00
|
|
|
|
2017-07-15 19:33:11 +08:00
|
|
|
def test_show_fixtures_indented_in_class(self, testdir):
|
|
|
|
p = testdir.makepyfile(dedent('''
|
|
|
|
import pytest
|
|
|
|
class TestClass:
|
|
|
|
@pytest.fixture
|
2017-10-20 07:01:26 +08:00
|
|
|
def fixture1(self):
|
2017-07-15 19:33:11 +08:00
|
|
|
"""line1
|
|
|
|
line2
|
|
|
|
indented line
|
|
|
|
"""
|
|
|
|
'''))
|
|
|
|
result = testdir.runpytest("--fixtures", p)
|
|
|
|
result.stdout.fnmatch_lines(dedent("""
|
|
|
|
* fixtures defined from test_show_fixtures_indented_in_class *
|
|
|
|
fixture1
|
|
|
|
line1
|
|
|
|
line2
|
|
|
|
indented line
|
|
|
|
"""))
|
2013-05-05 20:48:37 +08:00
|
|
|
|
2015-07-13 04:32:39 +08:00
|
|
|
def test_show_fixtures_different_files(self, testdir):
|
|
|
|
"""
|
|
|
|
#833: --fixtures only shows fixtures from first file
|
|
|
|
"""
|
|
|
|
testdir.makepyfile(test_a='''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix_a():
|
|
|
|
"""Fixture A"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_a(fix_a):
|
|
|
|
pass
|
|
|
|
''')
|
|
|
|
testdir.makepyfile(test_b='''
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix_b():
|
|
|
|
"""Fixture B"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_b(fix_b):
|
|
|
|
pass
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest("--fixtures")
|
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
* fixtures defined from test_a *
|
|
|
|
fix_a
|
|
|
|
Fixture A
|
|
|
|
|
|
|
|
* fixtures defined from test_b *
|
|
|
|
fix_b
|
|
|
|
Fixture B
|
|
|
|
""")
|
|
|
|
|
2016-05-31 18:46:35 +08:00
|
|
|
def test_show_fixtures_with_same_name(self, testdir):
|
|
|
|
testdir.makeconftest('''
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
"""Hello World in conftest.py"""
|
|
|
|
return "Hello World"
|
|
|
|
''')
|
|
|
|
testdir.makepyfile('''
|
|
|
|
def test_foo(arg1):
|
|
|
|
assert arg1 == "Hello World"
|
|
|
|
''')
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
"""Hi from test module"""
|
|
|
|
return "Hi"
|
|
|
|
def test_bar(arg1):
|
|
|
|
assert arg1 == "Hi"
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest("--fixtures")
|
|
|
|
result.stdout.fnmatch_lines('''
|
|
|
|
* fixtures defined from conftest *
|
|
|
|
arg1
|
|
|
|
Hello World in conftest.py
|
|
|
|
|
|
|
|
* fixtures defined from test_show_fixtures_with_same_name *
|
|
|
|
arg1
|
|
|
|
Hi from test module
|
|
|
|
''')
|
|
|
|
|
2015-07-13 04:32:39 +08:00
|
|
|
|
2016-06-03 08:01:31 +08:00
|
|
|
@pytest.mark.parametrize('flavor', ['fixture', 'yield_fixture'])
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestContextManagerFixtureFuncs(object):
|
2016-06-03 08:01:31 +08:00
|
|
|
|
|
|
|
def test_simple(self, testdir, flavor):
|
2013-05-05 20:48:37 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2016-06-03 08:01:31 +08:00
|
|
|
@pytest.{flavor}
|
2013-05-05 20:48:37 +08:00
|
|
|
def arg1():
|
|
|
|
print ("setup")
|
|
|
|
yield 1
|
|
|
|
print ("teardown")
|
|
|
|
def test_1(arg1):
|
|
|
|
print ("test1 %s" % arg1)
|
|
|
|
def test_2(arg1):
|
|
|
|
print ("test2 %s" % arg1)
|
|
|
|
assert 0
|
2016-06-03 08:01:31 +08:00
|
|
|
""".format(flavor=flavor))
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-s")
|
2013-05-05 20:48:37 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
2013-09-27 18:28:34 +08:00
|
|
|
*setup*
|
|
|
|
*test1 1*
|
|
|
|
*teardown*
|
|
|
|
*setup*
|
|
|
|
*test2 1*
|
|
|
|
*teardown*
|
2013-05-05 20:48:37 +08:00
|
|
|
""")
|
|
|
|
|
2016-06-03 08:01:31 +08:00
|
|
|
def test_scoped(self, testdir, flavor):
|
2013-05-05 20:48:37 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2016-06-03 08:01:31 +08:00
|
|
|
@pytest.{flavor}(scope="module")
|
2013-05-05 20:48:37 +08:00
|
|
|
def arg1():
|
|
|
|
print ("setup")
|
|
|
|
yield 1
|
|
|
|
print ("teardown")
|
|
|
|
def test_1(arg1):
|
|
|
|
print ("test1 %s" % arg1)
|
|
|
|
def test_2(arg1):
|
|
|
|
print ("test2 %s" % arg1)
|
2016-06-03 08:01:31 +08:00
|
|
|
""".format(flavor=flavor))
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-s")
|
2013-05-05 20:48:37 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
2013-09-27 18:28:34 +08:00
|
|
|
*setup*
|
|
|
|
*test1 1*
|
|
|
|
*test2 1*
|
|
|
|
*teardown*
|
2013-05-05 20:48:37 +08:00
|
|
|
""")
|
|
|
|
|
2016-06-03 08:01:31 +08:00
|
|
|
def test_setup_exception(self, testdir, flavor):
|
2013-05-05 20:48:37 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2016-06-03 08:01:31 +08:00
|
|
|
@pytest.{flavor}(scope="module")
|
2013-05-05 20:48:37 +08:00
|
|
|
def arg1():
|
|
|
|
pytest.fail("setup")
|
|
|
|
yield 1
|
|
|
|
def test_1(arg1):
|
|
|
|
pass
|
2016-06-03 08:01:31 +08:00
|
|
|
""".format(flavor=flavor))
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-s")
|
2013-05-05 20:48:37 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*pytest.fail*setup*
|
|
|
|
*1 error*
|
|
|
|
""")
|
|
|
|
|
2016-06-03 08:01:31 +08:00
|
|
|
def test_teardown_exception(self, testdir, flavor):
|
2013-05-05 20:48:37 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2016-06-03 08:01:31 +08:00
|
|
|
@pytest.{flavor}(scope="module")
|
2013-05-05 20:48:37 +08:00
|
|
|
def arg1():
|
|
|
|
yield 1
|
|
|
|
pytest.fail("teardown")
|
|
|
|
def test_1(arg1):
|
|
|
|
pass
|
2016-06-03 08:01:31 +08:00
|
|
|
""".format(flavor=flavor))
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-s")
|
2013-05-05 20:48:37 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*pytest.fail*teardown*
|
|
|
|
*1 passed*1 error*
|
|
|
|
""")
|
|
|
|
|
2016-06-03 08:01:31 +08:00
|
|
|
def test_yields_more_than_one(self, testdir, flavor):
|
2013-05-05 20:48:37 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
2016-06-03 08:01:31 +08:00
|
|
|
@pytest.{flavor}(scope="module")
|
2013-05-05 20:48:37 +08:00
|
|
|
def arg1():
|
|
|
|
yield 1
|
|
|
|
yield 2
|
|
|
|
def test_1(arg1):
|
|
|
|
pass
|
2016-06-03 08:01:31 +08:00
|
|
|
""".format(flavor=flavor))
|
2015-04-28 18:05:08 +08:00
|
|
|
result = testdir.runpytest("-s")
|
2013-05-05 20:48:37 +08:00
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*fixture function*
|
|
|
|
*test_yields*:2*
|
|
|
|
""")
|
|
|
|
|
2016-06-08 07:05:07 +08:00
|
|
|
def test_custom_name(self, testdir, flavor):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
@pytest.{flavor}(name='meow')
|
|
|
|
def arg1():
|
|
|
|
return 'mew'
|
|
|
|
def test_1(meow):
|
|
|
|
print(meow)
|
|
|
|
""".format(flavor=flavor))
|
|
|
|
result = testdir.runpytest("-s")
|
|
|
|
result.stdout.fnmatch_lines("*mew*")
|
2016-06-21 18:09:55 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestParameterizedSubRequest(object):
|
2016-06-20 23:20:24 +08:00
|
|
|
def test_call_from_fixture(self, testdir):
|
|
|
|
testfile = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[0, 1, 2])
|
|
|
|
def fix_with_param(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def get_named_fixture(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
return request.getfixturevalue('fix_with_param')
|
2016-06-20 23:20:24 +08:00
|
|
|
|
|
|
|
def test_foo(request, get_named_fixture):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
E*Failed: The requested fixture has no parameter defined for the current test.
|
|
|
|
E*
|
|
|
|
E*Requested fixture 'fix_with_param' defined in:
|
|
|
|
E*{0}:4
|
|
|
|
E*Requested here:
|
|
|
|
E*{1}:9
|
|
|
|
*1 error*
|
|
|
|
""".format(testfile.basename, testfile.basename))
|
|
|
|
|
|
|
|
def test_call_from_test(self, testdir):
|
|
|
|
testfile = testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[0, 1, 2])
|
|
|
|
def fix_with_param(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_foo(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
request.getfixturevalue('fix_with_param')
|
2016-06-20 23:20:24 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
E*Failed: The requested fixture has no parameter defined for the current test.
|
|
|
|
E*
|
|
|
|
E*Requested fixture 'fix_with_param' defined in:
|
|
|
|
E*{0}:4
|
|
|
|
E*Requested here:
|
|
|
|
E*{1}:8
|
|
|
|
*1 failed*
|
|
|
|
""".format(testfile.basename, testfile.basename))
|
|
|
|
|
|
|
|
def test_external_fixture(self, testdir):
|
|
|
|
conffile = testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[0, 1, 2])
|
|
|
|
def fix_with_param(request):
|
|
|
|
return request.param
|
|
|
|
""")
|
|
|
|
|
|
|
|
testfile = testdir.makepyfile("""
|
|
|
|
def test_foo(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
request.getfixturevalue('fix_with_param')
|
2016-06-20 23:20:24 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
E*Failed: The requested fixture has no parameter defined for the current test.
|
|
|
|
E*
|
|
|
|
E*Requested fixture 'fix_with_param' defined in:
|
|
|
|
E*{0}:4
|
|
|
|
E*Requested here:
|
|
|
|
E*{1}:2
|
|
|
|
*1 failed*
|
|
|
|
""".format(conffile.basename, testfile.basename))
|
|
|
|
|
|
|
|
def test_non_relative_path(self, testdir):
|
|
|
|
tests_dir = testdir.mkdir('tests')
|
|
|
|
fixdir = testdir.mkdir('fixtures')
|
|
|
|
fixfile = fixdir.join("fix.py")
|
|
|
|
fixfile.write(_pytest._code.Source("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(params=[0, 1, 2])
|
|
|
|
def fix_with_param(request):
|
|
|
|
return request.param
|
|
|
|
"""))
|
|
|
|
|
|
|
|
testfile = tests_dir.join("test_foos.py")
|
|
|
|
testfile.write(_pytest._code.Source("""
|
|
|
|
from fix import fix_with_param
|
|
|
|
|
|
|
|
def test_foo(request):
|
2016-06-21 18:09:55 +08:00
|
|
|
request.getfixturevalue('fix_with_param')
|
2016-06-20 23:20:24 +08:00
|
|
|
"""))
|
|
|
|
|
|
|
|
tests_dir.chdir()
|
|
|
|
testdir.syspathinsert(fixdir)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
E*Failed: The requested fixture has no parameter defined for the current test.
|
|
|
|
E*
|
|
|
|
E*Requested fixture 'fix_with_param' defined in:
|
|
|
|
E*{0}:5
|
|
|
|
E*Requested here:
|
|
|
|
E*{1}:5
|
|
|
|
*1 failed*
|
|
|
|
""".format(fixfile.strpath, testfile.basename))
|
2016-06-21 18:09:55 +08:00
|
|
|
|
|
|
|
|
2017-11-12 21:14:55 +08:00
|
|
|
def test_pytest_fixture_setup_and_post_finalizer_hook(testdir):
|
2016-12-10 18:45:40 +08:00
|
|
|
testdir.makeconftest("""
|
2017-11-12 21:14:55 +08:00
|
|
|
from __future__ import print_function
|
|
|
|
def pytest_fixture_setup(fixturedef, request):
|
|
|
|
print('ROOT setup hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
|
|
|
|
def pytest_fixture_post_finalizer(fixturedef, request):
|
|
|
|
print('ROOT finalizer hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
|
2016-12-10 18:45:40 +08:00
|
|
|
""")
|
2017-11-12 21:14:55 +08:00
|
|
|
testdir.makepyfile(**{
|
|
|
|
'tests/conftest.py': """
|
|
|
|
from __future__ import print_function
|
|
|
|
def pytest_fixture_setup(fixturedef, request):
|
|
|
|
print('TESTS setup hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
|
|
|
|
def pytest_fixture_post_finalizer(fixturedef, request):
|
|
|
|
print('TESTS finalizer hook called for {0} from {1}'.format(fixturedef.argname, request.node.name))
|
|
|
|
""",
|
|
|
|
'tests/test_hooks.py': """
|
|
|
|
from __future__ import print_function
|
|
|
|
import pytest
|
2016-12-10 18:45:40 +08:00
|
|
|
|
2017-11-12 21:14:55 +08:00
|
|
|
@pytest.fixture()
|
|
|
|
def my_fixture():
|
|
|
|
return 'some'
|
2016-12-10 18:45:40 +08:00
|
|
|
|
2017-11-12 21:14:55 +08:00
|
|
|
def test_func(my_fixture):
|
|
|
|
print('TEST test_func')
|
|
|
|
assert my_fixture == 'some'
|
|
|
|
"""
|
|
|
|
})
|
2016-12-10 18:45:40 +08:00
|
|
|
result = testdir.runpytest("-s")
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines([
|
2017-11-12 21:14:55 +08:00
|
|
|
"*TESTS setup hook called for my_fixture from test_func*",
|
|
|
|
"*ROOT setup hook called for my_fixture from test_func*",
|
|
|
|
"*TEST test_func*",
|
|
|
|
"*TESTS finalizer hook called for my_fixture from test_func*",
|
|
|
|
"*ROOT finalizer hook called for my_fixture from test_func*",
|
2016-12-10 18:45:40 +08:00
|
|
|
])
|