2016-06-08 21:17:55 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2016-06-09 14:22:11 +08:00
|
|
|
import os
|
2015-07-19 01:40:00 +08:00
|
|
|
import sys
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
import _pytest._code
|
|
|
|
import py
|
|
|
|
import pytest
|
2015-07-05 01:42:22 +08:00
|
|
|
from _pytest.main import EXIT_NOTESTSCOLLECTED, EXIT_USAGEERROR
|
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestGeneralUsage(object):
|
2009-03-17 15:03:49 +08:00
|
|
|
def test_config_error(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_configure(config):
|
2010-10-28 01:35:27 +08:00
|
|
|
import pytest
|
|
|
|
raise pytest.UsageError("hello")
|
2009-03-17 15:03:49 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest(testdir.tmpdir)
|
|
|
|
assert result.ret != 0
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stderr.fnmatch_lines([
|
2009-03-22 03:28:35 +08:00
|
|
|
'*ERROR: hello'
|
2009-03-17 15:03:49 +08:00
|
|
|
])
|
2009-03-17 18:29:45 +08:00
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
def test_root_conftest_syntax_error(self, testdir):
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makepyfile(conftest="raise SyntaxError\n")
|
2011-11-08 02:08:41 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stderr.fnmatch_lines(["*raise SyntaxError*"])
|
|
|
|
assert result.ret != 0
|
|
|
|
|
2011-04-17 18:20:11 +08:00
|
|
|
def test_early_hook_error_issue38_1(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_sessionstart():
|
|
|
|
0 / 0
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(testdir.tmpdir)
|
|
|
|
assert result.ret != 0
|
|
|
|
# tracestyle is native by default for hook failures
|
|
|
|
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
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
'*INTERNALERROR*def pytest_sessionstart():*',
|
|
|
|
'*INTERNALERROR*0 / 0*',
|
|
|
|
])
|
|
|
|
|
2011-04-17 18:20:13 +08:00
|
|
|
def test_early_hook_configure_error_issue38(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_configure():
|
|
|
|
0 / 0
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(testdir.tmpdir)
|
|
|
|
assert result.ret != 0
|
|
|
|
# here we get it on stderr
|
|
|
|
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"])
|
|
|
|
|
2012-05-18 05:11:23 +08:00
|
|
|
def test_file_not_found_unconfigure_issue143(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_configure():
|
|
|
|
print("---configure")
|
|
|
|
def pytest_unconfigure():
|
|
|
|
print("---unconfigure")
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("-s", "asd")
|
2017-07-17 07:25:09 +08:00
|
|
|
assert result.ret == 4 # EXIT_USAGEERROR
|
2012-05-18 05:11:23 +08:00
|
|
|
result.stderr.fnmatch_lines(["ERROR: file not found*asd"])
|
2013-10-12 21:39:22 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2012-05-18 05:11:23 +08:00
|
|
|
"*---configure",
|
|
|
|
"*---unconfigure",
|
|
|
|
])
|
2012-10-01 16:14:54 +08:00
|
|
|
|
2009-04-18 02:09:29 +08:00
|
|
|
def test_config_preparse_plugin_option(self, testdir):
|
|
|
|
testdir.makepyfile(pytest_xyz="""
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--xyz", dest="xyz", action="store")
|
2009-04-18 02:09:29 +08:00
|
|
|
""")
|
|
|
|
testdir.makepyfile(test_one="""
|
2010-01-14 01:04:58 +08:00
|
|
|
def test_option(pytestconfig):
|
|
|
|
assert pytestconfig.option.xyz == "123"
|
2009-04-18 02:09:29 +08:00
|
|
|
""")
|
2015-04-28 17:54:46 +08:00
|
|
|
result = testdir.runpytest("-p", "pytest_xyz", "--xyz=123", syspathinsert=True)
|
2009-04-18 02:09:29 +08:00
|
|
|
assert result.ret == 0
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2009-04-18 02:09:29 +08:00
|
|
|
'*1 passed*',
|
|
|
|
])
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_assertion_magic(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_this():
|
|
|
|
x = 0
|
|
|
|
assert x
|
|
|
|
""")
|
2009-02-27 18:18:27 +08:00
|
|
|
result = testdir.runpytest(p)
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2010-07-27 03:15:15 +08:00
|
|
|
"> assert x",
|
2008-08-16 23:26:59 +08:00
|
|
|
"E assert 0",
|
|
|
|
])
|
|
|
|
assert result.ret == 1
|
2009-03-17 15:03:49 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_nested_import_error(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2008-08-16 23:26:59 +08:00
|
|
|
import import_fails
|
|
|
|
def test_this():
|
|
|
|
assert import_fails.a == 1
|
2009-02-27 18:18:27 +08:00
|
|
|
""")
|
|
|
|
testdir.makepyfile(import_fails="import does_not_work")
|
|
|
|
result = testdir.runpytest(p)
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2017-07-17 07:25:09 +08:00
|
|
|
# XXX on jython this fails: "> import import_fails",
|
2016-04-17 18:43:04 +08:00
|
|
|
"ImportError while importing test module*",
|
2016-10-04 07:47:44 +08:00
|
|
|
"*No module named *does_not_work*",
|
2008-08-16 23:26:59 +08:00
|
|
|
])
|
2016-06-20 21:05:50 +08:00
|
|
|
assert result.ret == 2
|
2009-08-06 21:26:45 +08:00
|
|
|
|
|
|
|
def test_not_collectable_arguments(self, testdir):
|
|
|
|
p1 = testdir.makepyfile("")
|
|
|
|
p2 = testdir.makefile(".pyc", "123")
|
|
|
|
result = testdir.runpytest(p1, p2)
|
2010-11-06 16:58:04 +08:00
|
|
|
assert result.ret
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stderr.fnmatch_lines([
|
2017-07-17 07:25:08 +08:00
|
|
|
"*ERROR: not found:*%s" % (p2.basename,)
|
2009-08-06 21:26:45 +08:00
|
|
|
])
|
2009-12-29 23:29:48 +08:00
|
|
|
|
2014-04-03 02:42:41 +08:00
|
|
|
def test_issue486_better_reporting_on_conftest_load_failure(self, testdir):
|
|
|
|
testdir.makepyfile("")
|
|
|
|
testdir.makeconftest("import qwerty")
|
|
|
|
result = testdir.runpytest("--help")
|
|
|
|
result.stdout.fnmatch_lines("""
|
|
|
|
*--version*
|
|
|
|
*warning*conftest.py*
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stderr.fnmatch_lines("""
|
|
|
|
*ERROR*could not load*conftest.py*
|
|
|
|
""")
|
2009-12-29 23:29:48 +08:00
|
|
|
|
2010-05-14 18:02:43 +08:00
|
|
|
def test_early_skip(self, testdir):
|
|
|
|
testdir.mkdir("xyz")
|
|
|
|
testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2010-05-14 18:02:43 +08:00
|
|
|
def pytest_collect_directory():
|
2010-11-13 16:05:11 +08:00
|
|
|
pytest.skip("early")
|
2010-05-14 18:02:43 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
2015-07-05 01:42:22 +08:00
|
|
|
assert result.ret == EXIT_NOTESTSCOLLECTED
|
2010-05-14 18:02:43 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 skip*"
|
|
|
|
])
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2010-05-14 18:02:43 +08:00
|
|
|
def test_issue88_initial_file_multinodes(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
class MyFile(pytest.File):
|
2010-05-14 18:02:43 +08:00
|
|
|
def collect(self):
|
2011-03-07 01:32:00 +08:00
|
|
|
return [MyItem("hello", parent=self)]
|
2010-05-14 18:02:43 +08:00
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
return MyFile(path, parent)
|
2011-03-07 01:32:00 +08:00
|
|
|
class MyItem(pytest.Item):
|
|
|
|
pass
|
2010-05-14 18:02:43 +08:00
|
|
|
""")
|
|
|
|
p = testdir.makepyfile("def test_hello(): pass")
|
2013-08-01 23:32:19 +08:00
|
|
|
result = testdir.runpytest(p, "--collect-only")
|
2010-05-14 18:02:43 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2010-07-27 03:15:15 +08:00
|
|
|
"*MyFile*test_issue88*",
|
|
|
|
"*Module*test_issue88*",
|
2010-05-14 18:02:43 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
def test_issue93_initialnode_importing_capturing(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
2010-10-25 05:43:35 +08:00
|
|
|
import sys
|
2010-10-25 05:26:14 +08:00
|
|
|
print ("should not be seen")
|
2010-10-25 05:43:35 +08:00
|
|
|
sys.stderr.write("stder42\\n")
|
2010-05-14 18:02:43 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
2015-07-05 01:42:22 +08:00
|
|
|
assert result.ret == EXIT_NOTESTSCOLLECTED
|
2010-05-14 18:02:43 +08:00
|
|
|
assert "should not be seen" not in result.stdout.str()
|
2010-10-25 05:43:35 +08:00
|
|
|
assert "stderr42" not in result.stderr.str()
|
2010-08-02 02:43:02 +08:00
|
|
|
|
2010-10-25 05:26:14 +08:00
|
|
|
def test_conftest_printing_shows_if_error(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
print ("should be seen")
|
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret != 0
|
|
|
|
assert "should be seen" in result.stdout.str()
|
|
|
|
|
2014-01-29 18:46:36 +08:00
|
|
|
@pytest.mark.skipif(not hasattr(py.path.local, 'mksymlinkto'),
|
|
|
|
reason="symlink not available on this platform")
|
2010-08-02 02:43:02 +08:00
|
|
|
def test_chdir(self, testdir):
|
|
|
|
testdir.tmpdir.join("py").mksymlinkto(py._pydir)
|
|
|
|
p = testdir.tmpdir.join("main.py")
|
2015-11-27 22:43:01 +08:00
|
|
|
p.write(_pytest._code.Source("""
|
2010-08-02 02:43:02 +08:00
|
|
|
import sys, os
|
|
|
|
sys.path.insert(0, '')
|
|
|
|
import py
|
|
|
|
print (py.__file__)
|
|
|
|
print (py.__path__)
|
|
|
|
os.chdir(os.path.dirname(os.getcwd()))
|
2010-11-06 16:58:04 +08:00
|
|
|
print (py.log)
|
2010-08-02 02:43:02 +08:00
|
|
|
"""))
|
2015-04-28 17:54:53 +08:00
|
|
|
result = testdir.runpython(p)
|
2010-08-02 02:43:02 +08:00
|
|
|
assert not result.ret
|
2010-09-15 16:30:50 +08:00
|
|
|
|
2010-10-04 22:19:01 +08:00
|
|
|
def test_issue109_sibling_conftests_not_loaded(self, testdir):
|
|
|
|
sub1 = testdir.tmpdir.mkdir("sub1")
|
|
|
|
sub2 = testdir.tmpdir.mkdir("sub2")
|
|
|
|
sub1.join("conftest.py").write("assert 0")
|
|
|
|
result = testdir.runpytest(sub2)
|
2015-07-05 01:42:22 +08:00
|
|
|
assert result.ret == EXIT_NOTESTSCOLLECTED
|
2010-10-04 22:19:01 +08:00
|
|
|
sub2.ensure("__init__.py")
|
|
|
|
p = sub2.ensure("test_hello.py")
|
|
|
|
result = testdir.runpytest(p)
|
2015-07-05 01:42:22 +08:00
|
|
|
assert result.ret == EXIT_NOTESTSCOLLECTED
|
2010-10-04 22:19:01 +08:00
|
|
|
result = testdir.runpytest(sub1)
|
2015-07-05 01:42:22 +08:00
|
|
|
assert result.ret == EXIT_USAGEERROR
|
2010-10-04 22:19:01 +08:00
|
|
|
|
|
|
|
def test_directory_skipped(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2010-10-04 22:19:01 +08:00
|
|
|
def pytest_ignore_collect():
|
2010-11-13 16:05:11 +08:00
|
|
|
pytest.skip("intentional")
|
2010-10-04 22:19:01 +08:00
|
|
|
""")
|
|
|
|
testdir.makepyfile("def test_hello(): pass")
|
|
|
|
result = testdir.runpytest()
|
2015-07-05 01:42:22 +08:00
|
|
|
assert result.ret == EXIT_NOTESTSCOLLECTED
|
2010-10-04 22:19:01 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 skipped*"
|
|
|
|
])
|
2010-09-28 21:24:36 +08:00
|
|
|
|
|
|
|
def test_multiple_items_per_collector_byid(self, testdir):
|
|
|
|
c = testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
class MyItem(pytest.Item):
|
2010-09-28 21:24:36 +08:00
|
|
|
def runtest(self):
|
|
|
|
pass
|
2010-11-13 16:05:11 +08:00
|
|
|
class MyCollector(pytest.File):
|
2010-09-28 21:24:36 +08:00
|
|
|
def collect(self):
|
|
|
|
return [MyItem(name="xyz", parent=self)]
|
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
if path.basename.startswith("conftest"):
|
|
|
|
return MyCollector(path, parent)
|
|
|
|
""")
|
2017-07-17 07:25:08 +08:00
|
|
|
result = testdir.runpytest(c.basename + "::" + "xyz")
|
2010-09-28 21:24:36 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 pass*",
|
|
|
|
])
|
|
|
|
|
2010-10-13 18:26:14 +08:00
|
|
|
def test_skip_on_generated_funcarg_id(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2010-10-13 18:26:14 +08:00
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.addcall({'x': 3}, id='hello-123')
|
|
|
|
def pytest_runtest_setup(item):
|
|
|
|
print (item.keywords)
|
|
|
|
if 'hello-123' in item.keywords:
|
2010-11-13 16:05:11 +08:00
|
|
|
pytest.skip("hello")
|
2010-10-13 18:26:14 +08:00
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
p = testdir.makepyfile("""def test_func(x): pass""")
|
|
|
|
res = testdir.runpytest(p)
|
|
|
|
assert res.ret == 0
|
|
|
|
res.stdout.fnmatch_lines(["*1 skipped*"])
|
2011-09-12 14:57:35 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def test_direct_addressing_selects(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
metafunc.addcall({'i': 1}, id="1")
|
|
|
|
metafunc.addcall({'i': 2}, id="2")
|
|
|
|
def test_func(i):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
res = testdir.runpytest(p.basename + "::" + "test_func[1]")
|
|
|
|
assert res.ret == 0
|
|
|
|
res.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
|
|
|
def test_direct_addressing_notfound(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
res = testdir.runpytest(p.basename + "::" + "test_notfound")
|
|
|
|
assert res.ret
|
|
|
|
res.stderr.fnmatch_lines(["*ERROR*not found*"])
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2010-12-09 18:00:31 +08:00
|
|
|
def test_docstring_on_hookspec(self):
|
|
|
|
from _pytest import hookspec
|
|
|
|
for name, value in vars(hookspec).items():
|
|
|
|
if name.startswith("pytest_"):
|
|
|
|
assert value.__doc__, "no docstring for %s" % name
|
|
|
|
|
2011-06-01 20:54:34 +08:00
|
|
|
def test_initialization_error_issue49(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_configure():
|
|
|
|
x
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
2017-07-17 07:25:09 +08:00
|
|
|
assert result.ret == 3 # internal error
|
2011-06-01 20:54:34 +08:00
|
|
|
result.stderr.fnmatch_lines([
|
|
|
|
"INTERNAL*pytest_configure*",
|
|
|
|
"INTERNAL*x*",
|
|
|
|
])
|
|
|
|
assert 'sessionstarttime' not in result.stderr.str()
|
|
|
|
|
2017-06-04 05:26:34 +08:00
|
|
|
@pytest.mark.parametrize('lookfor', ['test_fun.py::test_a'])
|
|
|
|
def test_issue134_report_error_when_collecting_member(self, testdir, lookfor):
|
2013-02-14 19:21:42 +08:00
|
|
|
testdir.makepyfile(test_fun="""
|
|
|
|
def test_a():
|
|
|
|
pass
|
|
|
|
def""")
|
|
|
|
result = testdir.runpytest(lookfor)
|
|
|
|
result.stdout.fnmatch_lines(['*SyntaxError*'])
|
|
|
|
if '::' in lookfor:
|
|
|
|
result.stderr.fnmatch_lines([
|
|
|
|
'*ERROR*',
|
|
|
|
])
|
|
|
|
assert result.ret == 4 # usage error only if item not found
|
|
|
|
|
2013-09-09 04:26:51 +08:00
|
|
|
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")
|
|
|
|
result.stderr.fnmatch_lines([
|
|
|
|
"*ERROR*test_a.py::a*",
|
|
|
|
"*ERROR*test_b.py::b*",
|
|
|
|
])
|
|
|
|
|
2017-03-21 09:01:22 +08:00
|
|
|
@pytest.mark.usefixtures('recwarn')
|
2013-11-16 03:03:57 +08:00
|
|
|
def test_namespace_import_doesnt_confuse_import_hook(self, testdir):
|
2017-03-21 09:01:22 +08:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
|
2017-10-10 13:54:56 +08:00
|
|
|
Use recwarn here to silence this warning in Python 2.7:
|
2017-03-21 09:01:22 +08:00
|
|
|
ImportWarning: Not importing directory '...\not_a_package': missing __init__.py
|
|
|
|
"""
|
2013-11-16 03:03:57 +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
|
|
|
|
2013-11-16 03:03:57 +08:00
|
|
|
def test_whatever():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
res = testdir.runpytest(p.basename)
|
|
|
|
assert res.ret == 0
|
2013-02-14 19:21:42 +08:00
|
|
|
|
2014-04-01 20:19:58 +08:00
|
|
|
def test_unknown_option(self, testdir):
|
|
|
|
result = testdir.runpytest("--qwlkej")
|
|
|
|
result.stderr.fnmatch_lines("""
|
|
|
|
*unrecognized*
|
|
|
|
""")
|
|
|
|
|
2015-04-28 17:54:46 +08:00
|
|
|
def test_getsourcelines_error_issue553(self, testdir, monkeypatch):
|
|
|
|
monkeypatch.setattr("inspect.getsourcelines", None)
|
2015-04-16 06:31:22 +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
|
|
|
|
""")
|
|
|
|
res = testdir.runpytest(p)
|
|
|
|
res.stdout.fnmatch_lines([
|
|
|
|
"*source code not available*",
|
2016-07-24 05:45:07 +08:00
|
|
|
"E*fixture 'invalid_fixture' not found",
|
2015-04-16 06:31:22 +08:00
|
|
|
])
|
|
|
|
|
2015-07-19 01:40:00 +08:00
|
|
|
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:
|
|
|
|
pytest.main([str(tmpdir)], plugins=['invalid.module'])
|
|
|
|
assert 'invalid' in str(excinfo.value)
|
|
|
|
|
|
|
|
p = tmpdir.join('test_test_plugins_given_as_strings.py')
|
|
|
|
p.write('def test_foo(): pass')
|
|
|
|
mod = py.std.types.ModuleType("myplugin")
|
|
|
|
monkeypatch.setitem(sys.modules, 'myplugin', mod)
|
|
|
|
assert pytest.main(args=[str(tmpdir)], plugins=['myplugin']) == 0
|
|
|
|
|
2017-08-02 07:49:07 +08:00
|
|
|
def test_parametrized_with_bytes_regex(self, testdir):
|
2016-03-06 01:23:13 +08:00
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import re
|
|
|
|
import pytest
|
|
|
|
@pytest.mark.parametrize('r', [re.compile(b'foo')])
|
|
|
|
def test_stuff(r):
|
|
|
|
pass
|
|
|
|
"""
|
2017-07-17 07:25:07 +08:00
|
|
|
)
|
2016-03-06 01:23:13 +08:00
|
|
|
res = testdir.runpytest(p)
|
|
|
|
res.stdout.fnmatch_lines([
|
|
|
|
'*1 passed*'
|
|
|
|
])
|
|
|
|
|
2017-08-02 07:49:07 +08:00
|
|
|
def test_parametrized_with_null_bytes(self, testdir):
|
|
|
|
"""Test parametrization with values that contain null bytes and unicode characters (#2644)"""
|
|
|
|
p = testdir.makepyfile(u"""
|
|
|
|
# encoding: UTF-8
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("data", ["\\x00", u'ação'])
|
|
|
|
def test_foo(data):
|
|
|
|
assert data
|
|
|
|
""")
|
|
|
|
res = testdir.runpytest(p)
|
|
|
|
res.assert_outcomes(passed=2)
|
|
|
|
|
2013-02-14 19:21:42 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestInvocationVariants(object):
|
2010-11-06 06:37:31 +08:00
|
|
|
def test_earlyinit(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
assert hasattr(pytest, 'mark')
|
2010-11-06 06:37:31 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpython(p)
|
|
|
|
assert result.ret == 0
|
|
|
|
|
2011-09-21 14:12:37 +08:00
|
|
|
@pytest.mark.xfail("sys.platform.startswith('java')")
|
2010-11-06 06:37:31 +08:00
|
|
|
def test_pydoc(self, testdir):
|
2010-11-13 16:05:11 +08:00
|
|
|
for name in ('py.test', 'pytest'):
|
2010-11-18 21:56:16 +08:00
|
|
|
result = testdir.runpython_c("import %s;help(%s)" % (name, name))
|
2010-11-13 16:05:11 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
s = result.stdout.str()
|
|
|
|
assert 'MarkGenerator' in s
|
|
|
|
|
2010-11-13 18:10:45 +08:00
|
|
|
def test_import_star_py_dot_test(self, testdir):
|
2010-11-13 16:05:11 +08:00
|
|
|
p = testdir.makepyfile("""
|
2010-11-13 18:10:45 +08:00
|
|
|
from py.test import *
|
|
|
|
#collect
|
|
|
|
#cmdline
|
|
|
|
#Item
|
2017-07-17 07:25:09 +08:00
|
|
|
# assert collect.Item is Item
|
|
|
|
# assert collect.Collector is Collector
|
2010-11-13 16:05:11 +08:00
|
|
|
main
|
|
|
|
skip
|
|
|
|
xfail
|
2010-11-13 18:10:45 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpython(p)
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
def test_import_star_pytest(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
from pytest import *
|
|
|
|
#Item
|
|
|
|
#File
|
|
|
|
main
|
|
|
|
skip
|
|
|
|
xfail
|
|
|
|
""")
|
2010-11-13 16:05:11 +08:00
|
|
|
result = testdir.runpython(p)
|
2010-11-06 06:37:31 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
def test_double_pytestcmdline(self, testdir):
|
|
|
|
p = testdir.makepyfile(run="""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-11-13 16:05:11 +08:00
|
|
|
pytest.main()
|
|
|
|
pytest.main()
|
2010-11-06 06:37:31 +08:00
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_hello():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpython(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 passed*",
|
|
|
|
"*1 passed*",
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_python_minus_m_invocation_ok(self, testdir):
|
|
|
|
p1 = testdir.makepyfile("def test_hello(): pass")
|
2012-10-01 16:14:54 +08:00
|
|
|
res = testdir.run(py.std.sys.executable, "-m", "pytest", str(p1))
|
2010-11-06 06:37:31 +08:00
|
|
|
assert res.ret == 0
|
|
|
|
|
|
|
|
def test_python_minus_m_invocation_fail(self, testdir):
|
|
|
|
p1 = testdir.makepyfile("def test_fail(): 0/0")
|
2012-10-01 16:14:54 +08:00
|
|
|
res = testdir.run(py.std.sys.executable, "-m", "pytest", str(p1))
|
2010-11-06 06:37:31 +08:00
|
|
|
assert res.ret == 1
|
|
|
|
|
|
|
|
def test_python_pytest_package(self, testdir):
|
|
|
|
p1 = testdir.makepyfile("def test_pass(): pass")
|
|
|
|
res = testdir.run(py.std.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
|
|
|
|
|
2013-09-29 04:23:00 +08:00
|
|
|
def test_invoke_with_string(self, capsys):
|
|
|
|
retcode = pytest.main("-h")
|
2010-11-06 06:37:31 +08:00
|
|
|
assert not retcode
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert "--help" in out
|
2011-11-08 02:08:41 +08:00
|
|
|
pytest.raises(ValueError, lambda: pytest.main(0))
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2013-09-29 04:23:00 +08:00
|
|
|
def test_invoke_with_path(self, tmpdir, capsys):
|
|
|
|
retcode = pytest.main(tmpdir)
|
2015-07-05 01:42:22 +08:00
|
|
|
assert retcode == EXIT_NOTESTSCOLLECTED
|
2010-11-06 06:37:31 +08:00
|
|
|
out, err = capsys.readouterr()
|
2011-09-12 14:57:35 +08:00
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
def test_invoke_plugin_api(self, testdir, capsys):
|
2017-02-17 02:41:51 +08:00
|
|
|
class MyPlugin(object):
|
2010-11-06 06:37:31 +08:00
|
|
|
def pytest_addoption(self, parser):
|
|
|
|
parser.addoption("--myopt")
|
2010-11-18 21:56:16 +08:00
|
|
|
|
2013-09-29 04:23:00 +08:00
|
|
|
pytest.main(["-h"], plugins=[MyPlugin()])
|
2010-11-06 06:37:31 +08:00
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert "--myopt" in out
|
|
|
|
|
2011-09-05 23:38:22 +08:00
|
|
|
def test_pyargs_importerror(self, testdir, monkeypatch):
|
|
|
|
monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', False)
|
|
|
|
path = testdir.mkpydir("tpkg")
|
|
|
|
path.join("test_hello.py").write('raise ImportError')
|
|
|
|
|
2016-06-09 14:22:11 +08:00
|
|
|
result = testdir.runpytest_subprocess("--pyargs", "tpkg.test_hello")
|
2011-09-05 23:38:22 +08:00
|
|
|
assert result.ret != 0
|
2016-06-08 21:17:55 +08:00
|
|
|
|
2011-09-05 23:38:22 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2016-06-09 14:22:11 +08:00
|
|
|
"collected*0*items*/*1*errors"
|
2011-09-05 23:38:22 +08:00
|
|
|
])
|
|
|
|
|
2010-11-07 07:22:16 +08:00
|
|
|
def test_cmdline_python_package(self, testdir, monkeypatch):
|
2017-03-22 09:32:41 +08:00
|
|
|
import warnings
|
2010-11-07 07:36:24 +08:00
|
|
|
monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', False)
|
2010-11-07 05:17:33 +08:00
|
|
|
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
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*2 passed*"
|
|
|
|
])
|
|
|
|
result = testdir.runpytest("--pyargs", "tpkg.test_hello")
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 passed*"
|
|
|
|
])
|
2011-10-27 04:40:08 +08:00
|
|
|
|
2011-12-20 01:02:07 +08:00
|
|
|
def join_pythonpath(what):
|
|
|
|
cur = py.std.os.environ.get('PYTHONPATH')
|
|
|
|
if cur:
|
2016-06-09 14:22:11 +08:00
|
|
|
return str(what) + os.pathsep + cur
|
2011-12-20 01:02:07 +08:00
|
|
|
return what
|
2011-10-27 05:59:22 +08:00
|
|
|
empty_package = testdir.mkpydir("empty_package")
|
2011-12-20 01:02:07 +08:00
|
|
|
monkeypatch.setenv('PYTHONPATH', join_pythonpath(empty_package))
|
2017-03-22 09:32:41 +08:00
|
|
|
# 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():
|
|
|
|
warnings.simplefilter('ignore', ImportWarning)
|
|
|
|
result = testdir.runpytest("--pyargs", ".")
|
2011-10-27 04:40:08 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*2 passed*"
|
|
|
|
])
|
|
|
|
|
2011-12-20 01:02:07 +08:00
|
|
|
monkeypatch.setenv('PYTHONPATH', join_pythonpath(testdir))
|
2016-06-09 14:22:11 +08:00
|
|
|
result = testdir.runpytest("--pyargs", "tpkg.test_missing")
|
2011-01-28 04:11:21 +08:00
|
|
|
assert result.ret != 0
|
|
|
|
result.stderr.fnmatch_lines([
|
2016-06-09 14:22:11 +08:00
|
|
|
"*not*found*test_missing*",
|
2011-01-28 04:11:21 +08:00
|
|
|
])
|
2010-11-07 05:17:33 +08:00
|
|
|
|
2016-06-08 21:17:55 +08:00
|
|
|
def test_cmdline_python_namespace_package(self, testdir, monkeypatch):
|
|
|
|
"""
|
|
|
|
test --pyargs option with namespace packages (#1567)
|
|
|
|
"""
|
|
|
|
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(
|
|
|
|
"__import__('pkg_resources').declare_namespace(__name__)")
|
|
|
|
lib = ns.mkdir(dirname)
|
|
|
|
lib.ensure("__init__.py")
|
|
|
|
lib.join("test_{0}.py".format(dirname)). \
|
|
|
|
write("def test_{0}(): pass\n"
|
|
|
|
"def test_other():pass".format(dirname))
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def join_pythonpath(*dirs):
|
|
|
|
cur = py.std.os.environ.get('PYTHONPATH')
|
|
|
|
if cur:
|
|
|
|
dirs += (cur,)
|
2016-06-09 14:22:11 +08:00
|
|
|
return os.pathsep.join(str(p) for p in dirs)
|
2016-06-08 21:17:55 +08:00
|
|
|
monkeypatch.setenv('PYTHONPATH', join_pythonpath(*search_path))
|
|
|
|
for p in search_path:
|
|
|
|
monkeypatch.syspath_prepend(p)
|
|
|
|
|
2017-09-14 00:14:24 +08:00
|
|
|
os.chdir('world')
|
2016-06-08 21:17:55 +08:00
|
|
|
# mixed module and filenames:
|
2017-09-14 00:14:24 +08:00
|
|
|
result = testdir.runpytest("--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world")
|
|
|
|
testdir.chdir()
|
2016-06-08 21:17:55 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines([
|
2017-09-28 01:42:55 +08:00
|
|
|
"*test_hello.py::test_hello*PASSED*",
|
|
|
|
"*test_hello.py::test_other*PASSED*",
|
|
|
|
"*test_world.py::test_world*PASSED*",
|
|
|
|
"*test_world.py::test_other*PASSED*",
|
2016-06-08 21:17:55 +08:00
|
|
|
"*4 passed*"
|
|
|
|
])
|
|
|
|
|
|
|
|
# specify tests within a module
|
|
|
|
result = testdir.runpytest("--pyargs", "-v", "ns_pkg.world.test_world::test_other")
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines([
|
2017-09-28 01:42:55 +08:00
|
|
|
"*test_world.py::test_other*PASSED*",
|
2016-06-08 21:17:55 +08:00
|
|
|
"*1 passed*"
|
2011-01-28 04:11:21 +08:00
|
|
|
])
|
2010-11-07 05:17:33 +08:00
|
|
|
|
|
|
|
def test_cmdline_python_package_not_exists(self, testdir):
|
|
|
|
result = testdir.runpytest("--pyargs", "tpkgwhatv")
|
|
|
|
assert result.ret
|
|
|
|
result.stderr.fnmatch_lines([
|
|
|
|
"ERROR*file*or*package*not*found*",
|
|
|
|
])
|
2010-11-07 06:45:48 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.xfail(reason="decide: feature or bug")
|
2010-11-07 06:45:48 +08:00
|
|
|
def test_noclass_discovery_if_not_testcase(self, testdir):
|
|
|
|
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):
|
2010-11-07 06:45:48 +08:00
|
|
|
attr = 42
|
|
|
|
""")
|
|
|
|
reprec = testdir.inline_run(testpath)
|
|
|
|
reprec.assertoutcome(passed=1)
|
2010-11-18 01:24:28 +08:00
|
|
|
|
|
|
|
def test_doctest_id(self, testdir):
|
|
|
|
testdir.makefile('.txt', """
|
|
|
|
>>> x=3
|
|
|
|
>>> x
|
|
|
|
4
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("-rf")
|
|
|
|
lines = result.stdout.str().splitlines()
|
|
|
|
for line in lines:
|
|
|
|
if line.startswith("FAIL "):
|
|
|
|
testid = line[5:].strip()
|
|
|
|
break
|
|
|
|
result = testdir.runpytest(testid, '-rf')
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
line,
|
|
|
|
"*1 failed*",
|
|
|
|
])
|
|
|
|
|
2015-06-18 05:57:45 +08:00
|
|
|
def test_core_backward_compatibility(self):
|
|
|
|
"""Test backward compatibility for get_plugin_manager function. See #787."""
|
|
|
|
import _pytest.config
|
|
|
|
assert type(_pytest.config.get_plugin_manager()) is _pytest.config.PytestPluginManager
|
|
|
|
|
2015-08-10 06:30:49 +08:00
|
|
|
def test_has_plugin(self, request):
|
|
|
|
"""Test hasplugin function of the plugin manager (#932)."""
|
|
|
|
assert request.config.pluginmanager.hasplugin('python')
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestDurations(object):
|
2011-11-09 02:12:16 +08:00
|
|
|
source = """
|
2011-11-09 01:20:56 +08:00
|
|
|
import time
|
2013-10-01 18:37:11 +08:00
|
|
|
frag = 0.002
|
2013-03-28 17:21:03 +08:00
|
|
|
def test_something():
|
|
|
|
pass
|
2011-11-09 01:20:56 +08:00
|
|
|
def test_2():
|
2012-05-18 19:56:49 +08:00
|
|
|
time.sleep(frag*5)
|
2011-11-09 01:20:56 +08:00
|
|
|
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
|
2013-10-01 18:37:11 +08:00
|
|
|
result.stdout.fnmatch_lines_random([
|
2011-11-09 02:12:16 +08:00
|
|
|
"*durations*",
|
|
|
|
"*call*test_3*",
|
|
|
|
"*call*test_2*",
|
|
|
|
"*call*test_1*",
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_calls_show_2(self, testdir):
|
|
|
|
testdir.makepyfile(self.source)
|
|
|
|
result = testdir.runpytest("--durations=2")
|
|
|
|
assert result.ret == 0
|
2013-10-01 18:37:11 +08:00
|
|
|
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 "123":
|
2017-07-17 07:25:09 +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:
|
2017-07-17 07:25:08 +08:00
|
|
|
raise AssertionError("not found %s %s" % (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_1")
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*durations*",
|
|
|
|
"*call*test_1*",
|
|
|
|
])
|
|
|
|
|
2011-11-09 03:00:25 +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")
|
2016-06-20 21:05:50 +08:00
|
|
|
assert result.ret == 2
|
2011-11-09 03:00:25 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2016-06-20 21:05:50 +08:00
|
|
|
"*Interrupted: 1 errors during collection*",
|
2011-11-09 03:00:25 +08:00
|
|
|
])
|
2016-06-20 21:05:50 +08:00
|
|
|
# Collection errors abort test execution, therefore no duration is
|
|
|
|
# output
|
|
|
|
assert "duration" not in result.stdout.str()
|
2011-11-09 03:00:25 +08:00
|
|
|
|
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
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestDurationWithFixture(object):
|
2011-11-09 02:12:16 +08:00
|
|
|
source = """
|
2011-11-09 01:53:46 +08:00
|
|
|
import time
|
2013-10-01 18:37:11 +08:00
|
|
|
frag = 0.001
|
2011-11-09 01:53:46 +08:00
|
|
|
def setup_function(func):
|
|
|
|
time.sleep(frag * 3)
|
|
|
|
def test_1():
|
|
|
|
time.sleep(frag*2)
|
|
|
|
def test_2():
|
|
|
|
time.sleep(frag)
|
2011-11-09 02:12:16 +08:00
|
|
|
"""
|
2017-07-17 07:25:09 +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
|
|
|
|
|
2013-09-30 22:09:26 +08:00
|
|
|
result.stdout.fnmatch_lines_random("""
|
|
|
|
*durations*
|
|
|
|
* setup *test_1*
|
|
|
|
* call *test_1*
|
|
|
|
""")
|
2016-07-12 08:06:35 +08:00
|
|
|
|
2016-08-23 04:54:17 +08:00
|
|
|
|
|
|
|
def test_zipimport_hook(testdir, tmpdir):
|
|
|
|
"""Test package loader is being used correctly (see #1837)."""
|
|
|
|
zipapp = pytest.importorskip('zipapp')
|
|
|
|
testdir.tmpdir.join('app').ensure(dir=1)
|
|
|
|
testdir.makepyfile(**{
|
|
|
|
'app/foo.py': """
|
|
|
|
import pytest
|
|
|
|
def main():
|
|
|
|
pytest.main(['--pyarg', 'foo'])
|
|
|
|
""",
|
|
|
|
})
|
|
|
|
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
|
|
|
|
result.stderr.fnmatch_lines(['*not found*foo*'])
|
|
|
|
assert 'INTERNALERROR>' not in result.stdout.str()
|
2017-04-06 18:54:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_import_plugin_unicode_name(testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
myplugin='',
|
|
|
|
)
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test(): pass
|
|
|
|
""")
|
|
|
|
testdir.makeconftest("""
|
|
|
|
pytest_plugins = [u'myplugin']
|
|
|
|
""")
|
|
|
|
r = testdir.runpytest()
|
|
|
|
assert r.ret == 0
|
2017-03-08 08:02:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_deferred_hook_checking(testdir):
|
|
|
|
"""
|
|
|
|
Check hooks as late as possible (#1821).
|
|
|
|
"""
|
|
|
|
testdir.syspathinsert()
|
|
|
|
testdir.makepyfile(**{
|
|
|
|
'plugin.py': """
|
|
|
|
class Hooks:
|
|
|
|
def pytest_my_hook(self, config):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
config.pluginmanager.add_hookspecs(Hooks)
|
|
|
|
""",
|
|
|
|
'conftest.py': """
|
|
|
|
pytest_plugins = ['plugin']
|
|
|
|
def pytest_my_hook(config):
|
|
|
|
return 40
|
|
|
|
""",
|
|
|
|
'test_foo.py': """
|
|
|
|
def test(request):
|
|
|
|
assert request.config.hook.pytest_my_hook(config=request.config) == [40]
|
|
|
|
"""
|
|
|
|
})
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(['* 1 passed *'])
|