2020-05-09 18:57:17 +08:00
|
|
|
import sys
|
|
|
|
|
2016-06-22 22:45:36 +08:00
|
|
|
import pytest
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2016-06-22 22:45:36 +08:00
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.fixture(params=["--setup-only", "--setup-plan", "--setup-show"], scope="module")
|
2016-06-22 22:45:36 +08:00
|
|
|
def mode(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
2019-09-28 22:52:09 +08:00
|
|
|
def test_show_only_active_fixtures(testdir, mode, dummy_yaml_custom_test):
|
|
|
|
testdir.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
2016-06-22 18:00:45 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def _arg0():
|
|
|
|
"""hidden arg0 fixture"""
|
|
|
|
@pytest.fixture
|
|
|
|
def arg1():
|
|
|
|
"""arg1 docstring"""
|
|
|
|
def test_arg1(arg1):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
|
|
|
)
|
2016-06-22 18:00:45 +08:00
|
|
|
|
2019-09-28 22:52:09 +08:00
|
|
|
result = testdir.runpytest(mode)
|
2016-06-22 18:00:45 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*SETUP F arg1*", "*test_arg1 (fixtures used: arg1)*", "*TEARDOWN F arg1*"]
|
|
|
|
)
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*_arg0*")
|
2016-06-22 18:00:45 +08:00
|
|
|
|
|
|
|
|
2016-06-22 22:45:36 +08:00
|
|
|
def test_show_different_scopes(testdir, mode):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
'''
|
2016-06-22 18:00:45 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg_function():
|
|
|
|
"""function scoped fixture"""
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def arg_session():
|
|
|
|
"""session scoped fixture"""
|
|
|
|
def test_arg1(arg_session, arg_function):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
|
|
|
)
|
2016-06-22 18:00:45 +08:00
|
|
|
|
2016-06-22 22:45:36 +08:00
|
|
|
result = testdir.runpytest(mode, p)
|
2016-06-22 18:00:45 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"SETUP S arg_session*",
|
|
|
|
"*SETUP F arg_function*",
|
|
|
|
"*test_arg1 (fixtures used: arg_function, arg_session)*",
|
|
|
|
"*TEARDOWN F arg_function*",
|
|
|
|
"TEARDOWN S arg_session*",
|
|
|
|
]
|
|
|
|
)
|
2016-06-22 18:00:45 +08:00
|
|
|
|
|
|
|
|
2016-06-22 22:45:36 +08:00
|
|
|
def test_show_nested_fixtures(testdir, mode):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
'''
|
2016-06-22 18:00:45 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def arg_same():
|
|
|
|
"""session scoped fixture"""
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
|
|
|
)
|
|
|
|
p = testdir.makepyfile(
|
|
|
|
'''
|
2016-06-22 18:00:45 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def arg_same(arg_same):
|
|
|
|
"""function scoped fixture"""
|
|
|
|
def test_arg1(arg_same):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
|
|
|
)
|
2016-06-22 18:00:45 +08:00
|
|
|
|
2016-06-22 22:45:36 +08:00
|
|
|
result = testdir.runpytest(mode, p)
|
2016-06-22 18:00:45 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"SETUP S arg_same*",
|
|
|
|
"*SETUP F arg_same (fixtures used: arg_same)*",
|
|
|
|
"*test_arg1 (fixtures used: arg_same)*",
|
|
|
|
"*TEARDOWN F arg_same*",
|
|
|
|
"TEARDOWN S arg_same*",
|
|
|
|
]
|
|
|
|
)
|
2016-06-22 18:00:45 +08:00
|
|
|
|
|
|
|
|
2016-06-22 22:45:36 +08:00
|
|
|
def test_show_fixtures_with_autouse(testdir, mode):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
'''
|
2016-06-22 18:00:45 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg_function():
|
|
|
|
"""function scoped fixture"""
|
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def arg_session():
|
|
|
|
"""session scoped fixture"""
|
|
|
|
def test_arg1(arg_function):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
|
|
|
)
|
2016-06-22 18:00:45 +08:00
|
|
|
|
2016-06-22 22:45:36 +08:00
|
|
|
result = testdir.runpytest(mode, p)
|
2016-06-22 18:00:45 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"SETUP S arg_session*",
|
|
|
|
"*SETUP F arg_function*",
|
|
|
|
"*test_arg1 (fixtures used: arg_function, arg_session)*",
|
|
|
|
]
|
|
|
|
)
|
2016-06-22 18:00:45 +08:00
|
|
|
|
|
|
|
|
2016-06-22 22:45:36 +08:00
|
|
|
def test_show_fixtures_with_parameters(testdir, mode):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
'''
|
2016-06-22 18:00:45 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='session', params=['foo', 'bar'])
|
|
|
|
def arg_same():
|
|
|
|
"""session scoped fixture"""
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
|
|
|
)
|
|
|
|
p = testdir.makepyfile(
|
|
|
|
'''
|
2016-06-22 18:00:45 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def arg_other(arg_same):
|
|
|
|
"""function scoped fixture"""
|
|
|
|
def test_arg1(arg_other):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
|
|
|
)
|
2016-06-22 18:00:45 +08:00
|
|
|
|
2016-06-22 22:45:36 +08:00
|
|
|
result = testdir.runpytest(mode, p)
|
2016-06-22 18:00:45 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2020-05-10 18:06:36 +08:00
|
|
|
"SETUP S arg_same?'foo'?",
|
|
|
|
"TEARDOWN S arg_same?'foo'?",
|
|
|
|
"SETUP S arg_same?'bar'?",
|
|
|
|
"TEARDOWN S arg_same?'bar'?",
|
2018-05-23 22:48:46 +08:00
|
|
|
]
|
|
|
|
)
|
2016-06-22 18:00:45 +08:00
|
|
|
|
|
|
|
|
2016-06-22 22:45:36 +08:00
|
|
|
def test_show_fixtures_with_parameter_ids(testdir, mode):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
'''
|
2016-06-22 18:00:45 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(
|
|
|
|
scope='session', params=['foo', 'bar'], ids=['spam', 'ham'])
|
|
|
|
def arg_same():
|
|
|
|
"""session scoped fixture"""
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
|
|
|
)
|
|
|
|
p = testdir.makepyfile(
|
|
|
|
'''
|
2016-06-22 18:00:45 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(scope='function')
|
|
|
|
def arg_other(arg_same):
|
|
|
|
"""function scoped fixture"""
|
|
|
|
def test_arg1(arg_other):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
|
|
|
)
|
2016-06-22 18:00:45 +08:00
|
|
|
|
2016-06-22 22:45:36 +08:00
|
|
|
result = testdir.runpytest(mode, p)
|
2016-06-22 18:00:45 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2020-05-10 18:06:36 +08:00
|
|
|
["SETUP S arg_same?'spam'?", "SETUP S arg_same?'ham'?"]
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-06-22 23:24:55 +08:00
|
|
|
|
|
|
|
|
2016-06-23 16:54:22 +08:00
|
|
|
def test_show_fixtures_with_parameter_ids_function(testdir, mode):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2016-06-23 16:54:22 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture(params=['foo', 'bar'], ids=lambda p: p.upper())
|
|
|
|
def foobar():
|
|
|
|
pass
|
|
|
|
def test_foobar(foobar):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-06-23 16:54:22 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest(mode, p)
|
|
|
|
assert result.ret == 0
|
|
|
|
|
2020-05-10 18:06:36 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*SETUP F foobar?'FOO'?", "*SETUP F foobar?'BAR'?"]
|
|
|
|
)
|
2016-06-23 16:54:22 +08:00
|
|
|
|
|
|
|
|
2016-06-22 23:24:55 +08:00
|
|
|
def test_dynamic_fixture_request(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2016-06-22 23:24:55 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture()
|
|
|
|
def dynamically_requested_fixture():
|
|
|
|
pass
|
|
|
|
@pytest.fixture()
|
|
|
|
def dependent_fixture(request):
|
2017-07-21 10:11:14 +08:00
|
|
|
request.getfixturevalue('dynamically_requested_fixture')
|
2016-06-22 23:24:55 +08:00
|
|
|
def test_dyn(dependent_fixture):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-06-22 23:24:55 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest("--setup-only", p)
|
2016-06-22 23:24:55 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*SETUP F dynamically_requested_fixture",
|
|
|
|
"*TEARDOWN F dynamically_requested_fixture",
|
|
|
|
]
|
|
|
|
)
|
2016-06-23 16:23:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_capturing(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2016-06-23 16:23:04 +08:00
|
|
|
import pytest, sys
|
|
|
|
@pytest.fixture()
|
|
|
|
def one():
|
|
|
|
sys.stdout.write('this should be captured')
|
|
|
|
sys.stderr.write('this should also be captured')
|
|
|
|
@pytest.fixture()
|
|
|
|
def two(one):
|
|
|
|
assert 0
|
|
|
|
def test_capturing(two):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-06-23 16:23:04 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest("--setup-only", p)
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["this should be captured", "this should also be captured"]
|
|
|
|
)
|
2016-07-04 04:30:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_show_fixtures_and_execute_test(testdir):
|
|
|
|
""" Verifies that setups are shown and tests are executed. """
|
2018-05-23 22:48:46 +08:00
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
2016-07-04 04:30:51 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg():
|
|
|
|
assert True
|
|
|
|
def test_arg(arg):
|
|
|
|
assert False
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-07-04 04:30:51 +08:00
|
|
|
|
|
|
|
result = testdir.runpytest("--setup-show", p)
|
|
|
|
assert result.ret == 1
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*SETUP F arg*", "*test_arg (fixtures used: arg)F*", "*TEARDOWN F arg*"]
|
|
|
|
)
|
2019-10-20 03:28:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_setup_show_with_KeyboardInterrupt_in_test(testdir):
|
|
|
|
p = testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def arg():
|
2019-10-21 02:39:56 +08:00
|
|
|
pass
|
2019-10-20 03:28:06 +08:00
|
|
|
def test_arg(arg):
|
|
|
|
raise KeyboardInterrupt()
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("--setup-show", p, no_reraise_ctrlc=True)
|
|
|
|
result.stdout.fnmatch_lines(
|
2019-10-20 14:54:23 +08:00
|
|
|
[
|
|
|
|
"*SETUP F arg*",
|
|
|
|
"*test_arg (fixtures used: arg)*",
|
|
|
|
"*TEARDOWN F arg*",
|
|
|
|
"*! KeyboardInterrupt !*",
|
|
|
|
"*= no tests ran in *",
|
|
|
|
]
|
2019-10-20 03:28:06 +08:00
|
|
|
)
|
2019-10-21 02:39:56 +08:00
|
|
|
assert result.ret == ExitCode.INTERRUPTED
|
2020-05-09 18:57:17 +08:00
|
|
|
|
|
|
|
|
2020-05-10 21:59:20 +08:00
|
|
|
def test_show_fixture_action_with_bytes(testdir):
|
2020-05-10 17:19:52 +08:00
|
|
|
# Issue 7126, BytesWarning when using --setup-show with bytes parameter
|
2020-05-09 18:57:17 +08:00
|
|
|
test_file = testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('data', [b'Hello World'])
|
|
|
|
def test_data(data):
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.run(
|
|
|
|
sys.executable, "-bb", "-m", "pytest", "--setup-show", str(test_file)
|
|
|
|
)
|
|
|
|
assert result.ret == 0
|