test_ok1/testing/acceptance_test.py

1113 lines
36 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import sys
import textwrap
2017-12-27 11:47:26 +08:00
import types
import py
2017-12-07 05:55:04 +08:00
import six
import pytest
from _pytest.main import EXIT_NOTESTSCOLLECTED
from _pytest.main import EXIT_USAGEERROR
from _pytest.warnings import SHOW_PYTEST_WARNINGS_ARG
def prepend_pythonpath(*dirs):
cur = os.getenv("PYTHONPATH")
if cur:
dirs += (cur,)
return os.pathsep.join(str(p) for p in dirs)
class TestGeneralUsage(object):
def test_config_error(self, testdir):
testdir.copy_example("conftest_usageerror/conftest.py")
result = testdir.runpytest(testdir.tmpdir)
2018-11-09 08:31:53 +08:00
assert result.ret == EXIT_USAGEERROR
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(["*ERROR: hello"])
2018-11-09 08:31:53 +08:00
result.stdout.fnmatch_lines(["*pytest_unconfigure_called"])
def test_root_conftest_syntax_error(self, testdir):
testdir.makepyfile(conftest="raise SyntaxError\n")
result = testdir.runpytest()
result.stderr.fnmatch_lines(["*raise SyntaxError*"])
assert result.ret != 0
def test_early_hook_error_issue38_1(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_sessionstart():
0 / 0
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest(testdir.tmpdir)
assert result.ret != 0
# tracestyle is native by default for hook failures
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(
["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"]
)
result = testdir.runpytest(testdir.tmpdir, "--fulltrace")
assert result.ret != 0
# tracestyle is native by default for hook failures
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(
["*INTERNALERROR*def pytest_sessionstart():*", "*INTERNALERROR*0 / 0*"]
)
def test_early_hook_configure_error_issue38(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_configure():
0 / 0
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest(testdir.tmpdir)
assert result.ret != 0
# here we get it on stderr
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(
["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"]
)
2010-11-01 15:16:10 +08:00
def test_file_not_found(self, testdir):
result = testdir.runpytest("asd")
assert result.ret != 0
result.stderr.fnmatch_lines(["ERROR: file not found*asd"])
def test_file_not_found_unconfigure_issue143(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_configure():
print("---configure")
def pytest_unconfigure():
print("---unconfigure")
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest("-s", "asd")
assert result.ret == 4 # EXIT_USAGEERROR
result.stderr.fnmatch_lines(["ERROR: file not found*asd"])
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*---configure", "*---unconfigure"])
2012-10-01 16:14:54 +08:00
def test_config_preparse_plugin_option(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
pytest_xyz="""
def pytest_addoption(parser):
parser.addoption("--xyz", dest="xyz", action="store")
2018-05-23 22:48:46 +08:00
"""
)
testdir.makepyfile(
test_one="""
def test_option(pytestconfig):
assert pytestconfig.option.xyz == "123"
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest("-p", "pytest_xyz", "--xyz=123", syspathinsert=True)
assert result.ret == 0
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*1 passed*"])
def test_assertion_magic(self, testdir):
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
"""
def test_this():
x = 0
assert x
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest(p)
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["> assert x", "E assert 0"])
assert result.ret == 1
def test_nested_import_error(self, testdir):
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
"""
import import_fails
def test_this():
assert import_fails.a == 1
2018-05-23 22:48:46 +08:00
"""
)
testdir.makepyfile(import_fails="import does_not_work")
result = testdir.runpytest(p)
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(
[
# XXX on jython this fails: "> import import_fails",
"ImportError while importing test module*",
"*No module named *does_not_work*",
]
)
assert result.ret == 2
def test_not_collectable_arguments(self, testdir):
p1 = testdir.makepyfile("")
p2 = testdir.makefile(".pyc", "123")
result = testdir.runpytest(p1, p2)
assert result.ret
2018-08-23 09:30:42 +08:00
result.stderr.fnmatch_lines(["*ERROR: not found:*{}".format(p2.basename)])
@pytest.mark.filterwarnings("default")
def test_better_reporting_on_conftest_load_failure(self, testdir, request):
"""Show a user-friendly traceback on conftest import failures (#486, #3332)"""
testdir.makepyfile("")
testdir.makeconftest(
"""
def foo():
import qwerty
foo()
"""
)
result = testdir.runpytest("--help")
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(
"""
*--version*
*warning*conftest.py*
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
dirname = request.node.name + "0"
exc_name = (
"ModuleNotFoundError" if sys.version_info >= (3, 6) else "ImportError"
)
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(
[
"ImportError while loading conftest '*{sep}{dirname}{sep}conftest.py'.".format(
dirname=dirname, sep=os.sep
),
"conftest.py:3: in <module>",
" foo()",
"conftest.py:2: in foo",
" import qwerty",
"E {}: No module named {q}qwerty{q}".format(
exc_name, q="'" if six.PY3 else ""
),
]
2018-05-23 22:48:46 +08:00
)
def test_early_skip(self, testdir):
testdir.mkdir("xyz")
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
import pytest
def pytest_collect_directory():
pytest.skip("early")
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
assert result.ret == EXIT_NOTESTSCOLLECTED
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*1 skip*"])
def test_issue88_initial_file_multinodes(self, testdir):
testdir.copy_example("issue88_initial_file_multinodes")
p = testdir.makepyfile("def test_hello(): pass")
result = testdir.runpytest(p, "--collect-only")
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*MyFile*test_issue88*", "*Module*test_issue88*"])
def test_issue93_initialnode_importing_capturing(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
import sys
2018-11-22 16:15:14 +08:00
print("should not be seen")
sys.stderr.write("stder42\\n")
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
assert result.ret == EXIT_NOTESTSCOLLECTED
assert "should not be seen" not in result.stdout.str()
assert "stderr42" not in result.stderr.str()
def test_conftest_printing_shows_if_error(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
2018-11-22 16:15:14 +08:00
print("should be seen")
assert 0
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
assert result.ret != 0
assert "should be seen" in result.stdout.str()
2018-05-23 22:48:46 +08:00
@pytest.mark.skipif(
not hasattr(py.path.local, "mksymlinkto"),
reason="symlink not available on this platform",
)
def test_chdir(self, testdir):
testdir.tmpdir.join("py").mksymlinkto(py._pydir)
p = testdir.tmpdir.join("main.py")
2018-05-23 22:48:46 +08:00
p.write(
textwrap.dedent(
"""\
import sys, os
sys.path.insert(0, '')
import py
print(py.__file__)
print(py.__path__)
os.chdir(os.path.dirname(os.getcwd()))
print(py.log)
2018-05-23 22:48:46 +08:00
"""
)
)
result = testdir.runpython(p)
assert not result.ret
def test_issue109_sibling_conftests_not_loaded(self, testdir):
2017-12-01 20:28:39 +08:00
sub1 = testdir.mkdir("sub1")
sub2 = testdir.mkdir("sub2")
sub1.join("conftest.py").write("assert 0")
result = testdir.runpytest(sub2)
assert result.ret == EXIT_NOTESTSCOLLECTED
sub2.ensure("__init__.py")
p = sub2.ensure("test_hello.py")
result = testdir.runpytest(p)
assert result.ret == EXIT_NOTESTSCOLLECTED
result = testdir.runpytest(sub1)
assert result.ret == EXIT_USAGEERROR
def test_directory_skipped(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
import pytest
def pytest_ignore_collect():
pytest.skip("intentional")
2018-05-23 22:48:46 +08:00
"""
)
testdir.makepyfile("def test_hello(): pass")
result = testdir.runpytest()
assert result.ret == EXIT_NOTESTSCOLLECTED
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*1 skipped*"])
def test_multiple_items_per_collector_byid(self, testdir):
2018-05-23 22:48:46 +08:00
c = testdir.makeconftest(
"""
import pytest
class MyItem(pytest.Item):
def runtest(self):
pass
class MyCollector(pytest.File):
def collect(self):
return [MyItem(name="xyz", parent=self)]
def pytest_collect_file(path, parent):
if path.basename.startswith("conftest"):
return MyCollector(path, parent)
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest(c.basename + "::" + "xyz")
assert result.ret == 0
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*1 pass*"])
def test_skip_on_generated_funcarg_id(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
import pytest
def pytest_generate_tests(metafunc):
2018-12-02 02:41:59 +08:00
metafunc.parametrize('x', [3], ids=['hello-123'])
def pytest_runtest_setup(item):
2018-11-22 16:15:14 +08:00
print(item.keywords)
if 'hello-123' in item.keywords:
pytest.skip("hello")
assert 0
2018-05-23 22:48:46 +08:00
"""
)
p = testdir.makepyfile("""def test_func(x): pass""")
res = testdir.runpytest(p, SHOW_PYTEST_WARNINGS_ARG)
assert res.ret == 0
res.stdout.fnmatch_lines(["*1 skipped*"])
2011-09-12 14:57:35 +08:00
def test_direct_addressing_selects(self, testdir):
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
"""
def pytest_generate_tests(metafunc):
2018-12-02 02:41:59 +08:00
metafunc.parametrize('i', [1, 2], ids=["1", "2"])
def test_func(i):
pass
2018-05-23 22:48:46 +08:00
"""
)
res = testdir.runpytest(
p.basename + "::" + "test_func[1]", SHOW_PYTEST_WARNINGS_ARG
)
assert res.ret == 0
res.stdout.fnmatch_lines(["*1 passed*"])
def test_direct_addressing_notfound(self, testdir):
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
"""
def test_func():
pass
2018-05-23 22:48:46 +08:00
"""
)
res = testdir.runpytest(p.basename + "::" + "test_notfound")
assert res.ret
res.stderr.fnmatch_lines(["*ERROR*not found*"])
def test_docstring_on_hookspec(self):
from _pytest import hookspec
2018-05-23 22:48:46 +08:00
for name, value in vars(hookspec).items():
if name.startswith("pytest_"):
assert value.__doc__, "no docstring for %s" % name
def test_initialization_error_issue49(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
def pytest_configure():
x
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
assert result.ret == 3 # internal error
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(["INTERNAL*pytest_configure*", "INTERNAL*x*"])
assert "sessionstarttime" not in result.stderr.str()
2018-05-23 22:48:46 +08:00
@pytest.mark.parametrize("lookfor", ["test_fun.py::test_a"])
def test_issue134_report_error_when_collecting_member(self, testdir, lookfor):
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
test_fun="""
def test_a():
pass
2018-05-23 22:48:46 +08:00
def"""
)
result = testdir.runpytest(lookfor)
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*SyntaxError*"])
if "::" in lookfor:
result.stderr.fnmatch_lines(["*ERROR*"])
assert result.ret == 4 # usage error only if item not found
def test_report_all_failed_collections_initargs(self, testdir):
testdir.makepyfile(test_a="def", test_b="def")
result = testdir.runpytest("test_a.py::a", "test_b.py::b")
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(["*ERROR*test_a.py::a*", "*ERROR*test_b.py::b*"])
2018-05-23 22:48:46 +08:00
@pytest.mark.usefixtures("recwarn")
def test_namespace_import_doesnt_confuse_import_hook(self, testdir):
"""
Ref #383. Python 3.3's namespace package messed with our import hooks
Importing a module that didn't exist, even if the ImportError was
gracefully handled, would make our test crash.
Use recwarn here to silence this warning in Python 2.7:
ImportWarning: Not importing directory '...\not_a_package': missing __init__.py
"""
2018-05-23 22:48:46 +08:00
testdir.mkdir("not_a_package")
p = testdir.makepyfile(
"""
try:
from not_a_package import doesnt_exist
except ImportError:
# We handle the import error gracefully here
pass
2014-01-18 17:40:20 +08:00
def test_whatever():
pass
2018-05-23 22:48:46 +08:00
"""
)
res = testdir.runpytest(p.basename)
assert res.ret == 0
def test_unknown_option(self, testdir):
result = testdir.runpytest("--qwlkej")
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(
"""
*unrecognized*
2018-05-23 22:48:46 +08:00
"""
)
def test_getsourcelines_error_issue553(self, testdir, monkeypatch):
monkeypatch.setattr("inspect.getsourcelines", None)
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
"""
def raise_error(obj):
raise IOError('source code not available')
import inspect
inspect.getsourcelines = raise_error
def test_foo(invalid_fixture):
pass
2018-05-23 22:48:46 +08:00
"""
)
res = testdir.runpytest(p)
2018-05-23 22:48:46 +08:00
res.stdout.fnmatch_lines(
["*source code not available*", "E*fixture 'invalid_fixture' not found"]
)
def test_plugins_given_as_strings(self, tmpdir, monkeypatch):
"""test that str values passed to main() as `plugins` arg
are interpreted as module names to be imported and registered.
#855.
"""
with pytest.raises(ImportError) as excinfo:
2018-05-23 22:48:46 +08:00
pytest.main([str(tmpdir)], plugins=["invalid.module"])
assert "invalid" in str(excinfo.value)
2018-05-23 22:48:46 +08:00
p = tmpdir.join("test_test_plugins_given_as_strings.py")
p.write("def test_foo(): pass")
2017-12-27 11:47:26 +08:00
mod = types.ModuleType("myplugin")
2018-05-23 22:48:46 +08:00
monkeypatch.setitem(sys.modules, "myplugin", mod)
assert pytest.main(args=[str(tmpdir)], plugins=["myplugin"]) == 0
def test_parametrized_with_bytes_regex(self, testdir):
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
"""
import re
import pytest
@pytest.mark.parametrize('r', [re.compile(b'foo')])
def test_stuff(r):
pass
"""
2018-05-23 22:48:46 +08:00
)
res = testdir.runpytest(p)
2018-05-23 22:48:46 +08:00
res.stdout.fnmatch_lines(["*1 passed*"])
def test_parametrized_with_null_bytes(self, testdir):
"""Test parametrization with values that contain null bytes and unicode characters (#2644, #2957)"""
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
u"""
# encoding: UTF-8
import pytest
@pytest.mark.parametrize("data", [b"\\x00", "\\x00", u'ação'])
def test_foo(data):
assert data
2018-05-23 22:48:46 +08:00
"""
)
res = testdir.runpytest(p)
res.assert_outcomes(passed=3)
class TestInvocationVariants(object):
def test_earlyinit(self, testdir):
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
"""
import pytest
assert hasattr(pytest, 'mark')
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpython(p)
assert result.ret == 0
@pytest.mark.xfail("sys.platform.startswith('java')")
def test_pydoc(self, testdir):
2018-05-23 22:48:46 +08:00
for name in ("py.test", "pytest"):
2018-08-23 09:30:42 +08:00
result = testdir.runpython_c("import {};help({})".format(name, name))
assert result.ret == 0
s = result.stdout.str()
2018-05-23 22:48:46 +08:00
assert "MarkGenerator" in s
def test_import_star_py_dot_test(self, testdir):
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
"""
from py.test import *
#collect
#cmdline
#Item
# assert collect.Item is Item
# assert collect.Collector is Collector
main
skip
xfail
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpython(p)
assert result.ret == 0
def test_import_star_pytest(self, testdir):
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
"""
from pytest import *
#Item
#File
main
skip
xfail
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpython(p)
assert result.ret == 0
def test_double_pytestcmdline(self, testdir):
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
run="""
import pytest
pytest.main()
pytest.main()
2018-05-23 22:48:46 +08:00
"""
)
testdir.makepyfile(
"""
def test_hello():
pass
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpython(p)
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*1 passed*", "*1 passed*"])
def test_python_minus_m_invocation_ok(self, testdir):
p1 = testdir.makepyfile("def test_hello(): pass")
2017-12-27 11:47:26 +08:00
res = testdir.run(sys.executable, "-m", "pytest", str(p1))
assert res.ret == 0
def test_python_minus_m_invocation_fail(self, testdir):
p1 = testdir.makepyfile("def test_fail(): 0/0")
2017-12-27 11:47:26 +08:00
res = testdir.run(sys.executable, "-m", "pytest", str(p1))
assert res.ret == 1
def test_python_pytest_package(self, testdir):
p1 = testdir.makepyfile("def test_pass(): pass")
2017-12-27 11:47:26 +08:00
res = testdir.run(sys.executable, "-m", "pytest", str(p1))
assert res.ret == 0
res.stdout.fnmatch_lines(["*1 passed*"])
def test_equivalence_pytest_pytest(self):
assert pytest.main == py.test.cmdline.main
def test_invoke_with_invalid_type(self, capsys):
with pytest.raises(
TypeError, match="expected to be a list or tuple of strings, got: '-h'"
):
pytest.main("-h")
def test_invoke_with_path(self, tmpdir, capsys):
retcode = pytest.main(tmpdir)
assert retcode == EXIT_NOTESTSCOLLECTED
out, err = capsys.readouterr()
2011-09-12 14:57:35 +08:00
def test_invoke_plugin_api(self, testdir, capsys):
class MyPlugin(object):
def pytest_addoption(self, parser):
parser.addoption("--myopt")
pytest.main(["-h"], plugins=[MyPlugin()])
out, err = capsys.readouterr()
assert "--myopt" in out
def test_pyargs_importerror(self, testdir, monkeypatch):
2018-05-23 22:48:46 +08:00
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False)
path = testdir.mkpydir("tpkg")
2018-05-23 22:48:46 +08:00
path.join("test_hello.py").write("raise ImportError")
result = testdir.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True)
assert result.ret != 0
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["collected*0*items*/*1*errors"])
def test_cmdline_python_package(self, testdir, monkeypatch):
import warnings
2018-05-23 22:48:46 +08:00
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False)
path = testdir.mkpydir("tpkg")
path.join("test_hello.py").write("def test_hello(): pass")
path.join("test_world.py").write("def test_world(): pass")
result = testdir.runpytest("--pyargs", "tpkg")
assert result.ret == 0
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*2 passed*"])
result = testdir.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True)
assert result.ret == 0
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*1 passed*"])
empty_package = testdir.mkpydir("empty_package")
monkeypatch.setenv("PYTHONPATH", str(empty_package), prepend=os.pathsep)
# the path which is not a package raises a warning on pypy;
# no idea why only pypy and not normal python warn about it here
with warnings.catch_warnings():
2018-05-23 22:48:46 +08:00
warnings.simplefilter("ignore", ImportWarning)
result = testdir.runpytest("--pyargs", ".")
assert result.ret == 0
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*2 passed*"])
monkeypatch.setenv("PYTHONPATH", str(testdir), prepend=os.pathsep)
result = testdir.runpytest("--pyargs", "tpkg.test_missing", syspathinsert=True)
assert result.ret != 0
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(["*not*found*test_missing*"])
def test_cmdline_python_namespace_package(self, testdir, monkeypatch):
"""
test --pyargs option with namespace packages (#1567)
"""
2018-05-23 22:48:46 +08:00
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
search_path = []
for dirname in "hello", "world":
d = testdir.mkdir(dirname)
search_path.append(d)
ns = d.mkdir("ns_pkg")
ns.join("__init__.py").write(
2018-05-23 22:48:46 +08:00
"__import__('pkg_resources').declare_namespace(__name__)"
)
lib = ns.mkdir(dirname)
lib.ensure("__init__.py")
2018-05-23 22:48:46 +08:00
lib.join("test_{}.py".format(dirname)).write(
"def test_{}(): pass\ndef test_other():pass".format(dirname)
2018-05-23 22:48:46 +08:00
)
# The structure of the test directory is now:
# .
# ├── hello
# │ └── ns_pkg
# │ ├── __init__.py
# │ └── hello
# │ ├── __init__.py
# │ └── test_hello.py
# └── world
# └── ns_pkg
# ├── __init__.py
# └── world
# ├── __init__.py
# └── test_world.py
# NOTE: the different/reversed ordering is intentional here.
monkeypatch.setenv("PYTHONPATH", prepend_pythonpath(*search_path))
for p in search_path:
monkeypatch.syspath_prepend(p)
# mixed module and filenames:
monkeypatch.chdir("world")
result = testdir.runpytest("--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world")
assert result.ret == 0
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(
[
"test_hello.py::test_hello*PASSED*",
"test_hello.py::test_other*PASSED*",
"ns_pkg/world/test_world.py::test_world*PASSED*",
"ns_pkg/world/test_world.py::test_other*PASSED*",
"*4 passed in*",
2018-05-23 22:48:46 +08:00
]
)
# specify tests within a module
testdir.chdir()
2018-05-23 22:48:46 +08:00
result = testdir.runpytest(
"--pyargs", "-v", "ns_pkg.world.test_world::test_other"
)
assert result.ret == 0
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(
["*test_world.py::test_other*PASSED*", "*1 passed*"]
)
def test_invoke_test_and_doctestmodules(self, testdir):
p = testdir.makepyfile(
"""
def test():
pass
"""
)
result = testdir.runpytest(str(p) + "::test", "--doctest-modules")
result.stdout.fnmatch_lines(["*1 passed*"])
2017-12-07 05:55:04 +08:00
@pytest.mark.skipif(not hasattr(os, "symlink"), reason="requires symlinks")
def test_cmdline_python_package_symlink(self, testdir, monkeypatch):
"""
test --pyargs option with packages with path containing symlink can
have conftest.py in their package (#2985)
"""
# dummy check that we can actually create symlinks: on Windows `os.symlink` is available,
# but normal users require special admin privileges to create symlinks.
2018-05-23 22:48:46 +08:00
if sys.platform == "win32":
try:
2018-05-23 22:48:46 +08:00
os.symlink(
str(testdir.tmpdir.ensure("tmpfile")),
str(testdir.tmpdir.join("tmpfile2")),
)
except OSError as e:
pytest.skip(six.text_type(e.args[0]))
2018-05-23 22:48:46 +08:00
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False)
2017-12-07 05:55:04 +08:00
dirname = "lib"
d = testdir.mkdir(dirname)
foo = d.mkdir("foo")
foo.ensure("__init__.py")
2018-05-23 22:48:46 +08:00
lib = foo.mkdir("bar")
2017-12-07 05:55:04 +08:00
lib.ensure("__init__.py")
2018-05-23 22:48:46 +08:00
lib.join("test_bar.py").write(
"def test_bar(): pass\ndef test_other(a_fixture):pass"
2018-05-23 22:48:46 +08:00
)
lib.join("conftest.py").write(
"import pytest\n@pytest.fixture\ndef a_fixture():pass"
2018-05-23 22:48:46 +08:00
)
2017-12-07 05:55:04 +08:00
d_local = testdir.mkdir("local")
symlink_location = os.path.join(str(d_local), "lib")
if six.PY2:
os.symlink(str(d), symlink_location)
else:
os.symlink(str(d), symlink_location, target_is_directory=True)
# The structure of the test directory is now:
# .
# ├── local
# │ └── lib -> ../lib
2017-12-07 05:55:04 +08:00
# └── lib
# └── foo
# ├── __init__.py
# └── bar
# ├── __init__.py
# ├── conftest.py
# └── test_bar.py
2017-12-07 05:55:04 +08:00
# NOTE: the different/reversed ordering is intentional here.
search_path = ["lib", os.path.join("local", "lib")]
monkeypatch.setenv("PYTHONPATH", prepend_pythonpath(*search_path))
2017-12-07 05:55:04 +08:00
for p in search_path:
monkeypatch.syspath_prepend(p)
# module picked up in symlink-ed directory:
# It picks up local/lib/foo/bar (symlink) via sys.path.
2017-12-07 05:55:04 +08:00
result = testdir.runpytest("--pyargs", "-v", "foo.bar")
testdir.chdir()
assert result.ret == 0
if hasattr(py.path.local, "mksymlinkto"):
result.stdout.fnmatch_lines(
[
"lib/foo/bar/test_bar.py::test_bar PASSED*",
"lib/foo/bar/test_bar.py::test_other PASSED*",
"*2 passed*",
]
)
else:
result.stdout.fnmatch_lines(
[
"*lib/foo/bar/test_bar.py::test_bar PASSED*",
"*lib/foo/bar/test_bar.py::test_other PASSED*",
"*2 passed*",
]
)
2017-12-07 05:55:04 +08:00
def test_cmdline_python_package_not_exists(self, testdir):
result = testdir.runpytest("--pyargs", "tpkgwhatv")
assert result.ret
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(["ERROR*file*or*package*not*found*"])
@pytest.mark.xfail(reason="decide: feature or bug")
def test_noclass_discovery_if_not_testcase(self, testdir):
2018-05-23 22:48:46 +08:00
testpath = testdir.makepyfile(
"""
import unittest
class TestHello(object):
def test_hello(self):
assert self.attr
2010-11-07 08:10:15 +08:00
class RealTest(unittest.TestCase, TestHello):
attr = 42
2018-05-23 22:48:46 +08:00
"""
)
reprec = testdir.inline_run(testpath)
reprec.assertoutcome(passed=1)
def test_doctest_id(self, testdir):
2018-05-23 22:48:46 +08:00
testdir.makefile(
".txt",
"""
>>> x=3
>>> x
4
2018-05-23 22:48:46 +08:00
""",
)
result = testdir.runpytest("-rf")
lines = result.stdout.str().splitlines()
for line in lines:
if line.startswith(("FAIL ", "FAILED ")):
_fail, _sep, testid = line.partition(" ")
break
2018-05-23 22:48:46 +08:00
result = testdir.runpytest(testid, "-rf")
result.stdout.fnmatch_lines([line, "*1 failed*"])
def test_core_backward_compatibility(self):
"""Test backward compatibility for get_plugin_manager function. See #787."""
import _pytest.config
2018-05-23 22:48:46 +08:00
assert (
type(_pytest.config.get_plugin_manager())
is _pytest.config.PytestPluginManager
)
def test_has_plugin(self, request):
"""Test hasplugin function of the plugin manager (#932)."""
2018-05-23 22:48:46 +08:00
assert request.config.pluginmanager.hasplugin("python")
class TestDurations(object):
2011-11-09 02:12:16 +08:00
source = """
import time
frag = 0.002
2013-03-28 17:21:03 +08:00
def test_something():
pass
def test_2():
2012-05-18 19:56:49 +08:00
time.sleep(frag*5)
def test_1():
time.sleep(frag)
def test_3():
2012-05-18 19:56:49 +08:00
time.sleep(frag*10)
2011-11-09 02:12:16 +08:00
"""
def test_calls(self, testdir):
testdir.makepyfile(self.source)
result = testdir.runpytest("--durations=10")
assert result.ret == 0
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines_random(
["*durations*", "*call*test_3*", "*call*test_2*"]
2018-05-23 22:48:46 +08:00
)
2018-10-14 05:36:20 +08:00
result.stdout.fnmatch_lines(
["(0.00 durations hidden. Use -vv to show these durations.)"]
2018-05-23 22:48:46 +08:00
)
2011-11-09 02:12:16 +08:00
def test_calls_show_2(self, testdir):
testdir.makepyfile(self.source)
result = testdir.runpytest("--durations=2")
assert result.ret == 0
lines = result.stdout.get_lines_after("*slowest*durations*")
assert "4 passed" in lines[2]
2011-11-09 02:12:16 +08:00
def test_calls_showall(self, testdir):
testdir.makepyfile(self.source)
result = testdir.runpytest("--durations=0")
assert result.ret == 0
for x in "23":
for y in ("call",): # 'setup', 'call', 'teardown':
for line in result.stdout.lines:
if ("test_%s" % x) in line and y in line:
break
else:
raise AssertionError("not found {} {}".format(x, y))
def test_calls_showall_verbose(self, testdir):
testdir.makepyfile(self.source)
result = testdir.runpytest("--durations=0", "-vv")
assert result.ret == 0
2011-11-09 02:12:16 +08:00
for x in "123":
2018-05-23 22:48:46 +08:00
for y in ("call",): # 'setup', 'call', 'teardown':
2011-11-09 02:12:16 +08:00
for line in result.stdout.lines:
if ("test_%s" % x) in line and y in line:
break
else:
2018-08-23 09:30:42 +08:00
raise AssertionError("not found {} {}".format(x, y))
2011-11-09 02:12:16 +08:00
def test_with_deselected(self, testdir):
testdir.makepyfile(self.source)
result = testdir.runpytest("--durations=2", "-k test_2")
2011-11-09 02:12:16 +08:00
assert result.ret == 0
result.stdout.fnmatch_lines(["*durations*", "*call*test_2*"])
2011-11-09 02:12:16 +08:00
def test_with_failing_collection(self, testdir):
testdir.makepyfile(self.source)
testdir.makepyfile(test_collecterror="""xyz""")
result = testdir.runpytest("--durations=2", "-k test_1")
assert result.ret == 2
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*Interrupted: 1 errors during collection*"])
# Collection errors abort test execution, therefore no duration is
# output
assert "duration" not in result.stdout.str()
2016-04-01 13:20:22 +08:00
def test_with_not(self, testdir):
testdir.makepyfile(self.source)
result = testdir.runpytest("-k not 1")
assert result.ret == 0
2011-11-09 02:12:16 +08:00
class TestDurationWithFixture(object):
2011-11-09 02:12:16 +08:00
source = """
import pytest
import time
frag = 0.01
@pytest.fixture
def setup_fixt():
time.sleep(frag)
def test_1(setup_fixt):
time.sleep(frag)
2011-11-09 02:12:16 +08:00
"""
2011-11-09 02:12:16 +08:00
def test_setup_function(self, testdir):
testdir.makepyfile(self.source)
result = testdir.runpytest("--durations=10")
assert result.ret == 0
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines_random(
"""
*durations*
* setup *test_1*
* call *test_1*
2018-05-23 22:48:46 +08:00
"""
)
def test_zipimport_hook(testdir, tmpdir):
"""Test package loader is being used correctly (see #1837)."""
2018-05-23 22:48:46 +08:00
zipapp = pytest.importorskip("zipapp")
testdir.tmpdir.join("app").ensure(dir=1)
testdir.makepyfile(
**{
"app/foo.py": """
import pytest
def main():
pytest.main(['--pyarg', 'foo'])
2018-05-23 22:48:46 +08:00
"""
}
)
target = tmpdir.join("foo.zip")
zipapp.create_archive(str(testdir.tmpdir.join("app")), str(target), main="foo:main")
result = testdir.runpython(target)
assert result.ret == 0
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(["*not found*foo*"])
assert "INTERNALERROR>" not in result.stdout.str()
def test_import_plugin_unicode_name(testdir):
2018-05-23 22:48:46 +08:00
testdir.makepyfile(myplugin="")
testdir.makepyfile(
2018-05-23 22:48:46 +08:00
"""
def test(): pass
2018-05-23 22:48:46 +08:00
"""
)
testdir.makeconftest(
"""
pytest_plugins = [u'myplugin']
2018-05-23 22:48:46 +08:00
"""
)
r = testdir.runpytest()
assert r.ret == 0
def test_deferred_hook_checking(testdir):
"""
Check hooks as late as possible (#1821).
"""
testdir.syspathinsert()
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
**{
"plugin.py": """
class Hooks(object):
def pytest_my_hook(self, config):
pass
def pytest_configure(config):
config.pluginmanager.add_hookspecs(Hooks)
""",
2018-05-23 22:48:46 +08:00
"conftest.py": """
pytest_plugins = ['plugin']
def pytest_my_hook(config):
return 40
""",
2018-05-23 22:48:46 +08:00
"test_foo.py": """
def test(request):
assert request.config.hook.pytest_my_hook(config=request.config) == [40]
2018-05-23 22:48:46 +08:00
""",
}
)
result = testdir.runpytest()
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["* 1 passed *"])
def test_fixture_values_leak(testdir):
"""Ensure that fixture objects are properly destroyed by the garbage collector at the end of their expected
life-times (#2981).
"""
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
"""
import attr
import gc
import pytest
import weakref
@attr.s
class SomeObj(object):
name = attr.ib()
fix_of_test1_ref = None
session_ref = None
@pytest.fixture(scope='session')
def session_fix():
global session_ref
obj = SomeObj(name='session-fixture')
session_ref = weakref.ref(obj)
return obj
@pytest.fixture
def fix(session_fix):
global fix_of_test1_ref
obj = SomeObj(name='local-fixture')
fix_of_test1_ref = weakref.ref(obj)
return obj
def test1(fix):
assert fix_of_test1_ref() is fix
def test2():
gc.collect()
# fixture "fix" created during test1 must have been destroyed by now
assert fix_of_test1_ref() is None
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["* 2 passed *"])
def test_fixture_order_respects_scope(testdir):
"""Ensure that fixtures are created according to scope order, regression test for #2405
"""
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
"""
import pytest
data = {}
@pytest.fixture(scope='module')
def clean_data():
data.clear()
@pytest.fixture(autouse=True)
def add_data():
data.update(value=True)
@pytest.mark.usefixtures('clean_data')
def test_value():
assert data.get('value')
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
assert result.ret == 0
2018-04-10 07:43:14 +08:00
def test_frame_leak_on_failing_test(testdir):
"""pytest would leak garbage referencing the frames of tests that failed that could never be reclaimed (#2798)
Unfortunately it was not possible to remove the actual circles because most of them
are made of traceback objects which cannot be weakly referenced. Those objects at least
can be eventually claimed by the garbage collector.
"""
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
"""
2018-04-10 07:43:14 +08:00
import gc
import weakref
class Obj:
pass
ref = None
def test1():
obj = Obj()
global ref
ref = weakref.ref(obj)
assert 0
def test2():
gc.collect()
assert ref() is None
2018-05-23 22:48:46 +08:00
"""
)
2018-04-10 07:43:14 +08:00
result = testdir.runpytest_subprocess()
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*1 failed, 1 passed in*"])
def test_fixture_mock_integration(testdir):
"""Test that decorators applied to fixture are left working (#3774)"""
p = testdir.copy_example("acceptance/fixture_mock_integration.py")
result = testdir.runpytest(p)
result.stdout.fnmatch_lines("*1 passed*")
def test_usage_error_code(testdir):
result = testdir.runpytest("-unknown-option-")
assert result.ret == EXIT_USAGEERROR