2010-11-06 06:37:31 +08:00
|
|
|
import sys, py, pytest
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-03-23 01:41:36 +08:00
|
|
|
class TestGeneralUsage:
|
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
|
|
|
|
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"])
|
|
|
|
|
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
|
|
|
""")
|
|
|
|
result = testdir.runpytest("-p", "xyz", "--xyz=123")
|
|
|
|
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-03-17 18:29:45 +08:00
|
|
|
def test_basetemp(self, testdir):
|
|
|
|
mytemp = testdir.tmpdir.mkdir("mytemp")
|
|
|
|
p = testdir.makepyfile("""
|
2009-12-30 05:26:03 +08:00
|
|
|
def test_1(pytestconfig):
|
|
|
|
pytestconfig.getbasetemp().ensure("hello")
|
2009-03-17 18:29:45 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p, '--basetemp=%s' %mytemp)
|
|
|
|
assert result.ret == 0
|
2009-12-30 05:26:03 +08:00
|
|
|
assert mytemp.join('hello').check()
|
2010-07-27 03:15:15 +08:00
|
|
|
|
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([
|
2010-01-03 21:19:31 +08:00
|
|
|
#XXX on jython this fails: "> import import_fails",
|
2008-08-16 23:26:59 +08:00
|
|
|
"E ImportError: No module named does_not_work",
|
|
|
|
])
|
|
|
|
assert result.ret == 1
|
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([
|
2010-11-06 16:58:04 +08:00
|
|
|
"*ERROR: not found:*%s" %(p2.basename,)
|
2009-08-06 21:26:45 +08:00
|
|
|
])
|
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()
|
|
|
|
assert result.ret == 0
|
|
|
|
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):
|
|
|
|
return
|
|
|
|
def pytest_collect_file(path, parent):
|
|
|
|
return MyFile(path, parent)
|
|
|
|
""")
|
|
|
|
p = testdir.makepyfile("def test_hello(): pass")
|
|
|
|
result = testdir.runpytest(p, "--collectonly")
|
|
|
|
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()
|
|
|
|
assert result.ret == 0
|
|
|
|
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()
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.skipif("not hasattr(os, 'symlink')")
|
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")
|
|
|
|
p.write(py.code.Source("""
|
|
|
|
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
|
|
|
"""))
|
|
|
|
result = testdir.runpython(p, prepend=False)
|
|
|
|
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)
|
|
|
|
assert result.ret == 0
|
|
|
|
sub2.ensure("__init__.py")
|
|
|
|
p = sub2.ensure("test_hello.py")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
assert result.ret == 0
|
|
|
|
result = testdir.runpytest(sub1)
|
|
|
|
assert result.ret != 0
|
|
|
|
|
|
|
|
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()
|
|
|
|
assert result.ret == 0
|
|
|
|
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)
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(c.basename+"::"+"xyz")
|
|
|
|
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*"])
|
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
|
|
|
|
|
|
|
class TestInvocationVariants:
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_pydoc(self, testdir):
|
2010-11-13 16:05:11 +08:00
|
|
|
for name in ('py.test', 'pytest'):
|
|
|
|
result = testdir.runpython_c("import %s;help(%s)" % (name,name))
|
|
|
|
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
|
|
|
|
#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*",
|
|
|
|
])
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.skipif("sys.version_info < (2,5)")
|
2010-11-06 06:37:31 +08:00
|
|
|
def test_python_minus_m_invocation_ok(self, testdir):
|
|
|
|
p1 = testdir.makepyfile("def test_hello(): pass")
|
|
|
|
res = testdir.run(py.std.sys.executable, "-m", "py.test", str(p1))
|
|
|
|
assert res.ret == 0
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.skipif("sys.version_info < (2,5)")
|
2010-11-06 06:37:31 +08:00
|
|
|
def test_python_minus_m_invocation_fail(self, testdir):
|
|
|
|
p1 = testdir.makepyfile("def test_fail(): 0/0")
|
|
|
|
res = testdir.run(py.std.sys.executable, "-m", "py.test", str(p1))
|
|
|
|
assert res.ret == 1
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.skipif("sys.version_info < (2,5)")
|
2010-11-06 06:37:31 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
def test_invoke_with_string(self, capsys):
|
|
|
|
retcode = pytest.main("-h")
|
|
|
|
assert not retcode
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert "--help" in out
|
2010-11-07 06:45:48 +08:00
|
|
|
pytest.raises(ValueError, lambda: pytest.main(retcode))
|
2010-11-06 06:37:31 +08:00
|
|
|
|
|
|
|
def test_invoke_with_path(self, testdir, capsys):
|
|
|
|
retcode = testdir.pytestmain(testdir.tmpdir)
|
|
|
|
assert not retcode
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
|
|
|
|
def test_invoke_plugin_api(self, capsys):
|
|
|
|
class MyPlugin:
|
|
|
|
def pytest_addoption(self, parser):
|
|
|
|
parser.addoption("--myopt")
|
|
|
|
|
|
|
|
pytest.main(["-h"], plugins=[MyPlugin()])
|
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert "--myopt" in out
|
|
|
|
|
2010-11-07 07:22:16 +08:00
|
|
|
def test_cmdline_python_package(self, testdir, monkeypatch):
|
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*"
|
|
|
|
])
|
2010-11-07 08:10:15 +08:00
|
|
|
result = testdir.runpytest("--pyargs", ".")
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*2 passed*"
|
|
|
|
])
|
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*",
|
|
|
|
])
|
|
|
|
|