2009-12-29 23:29:48 +08:00
|
|
|
import sys, py
|
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):
|
|
|
|
raise config.Error("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
|
|
|
|
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="""
|
|
|
|
import py
|
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("""
|
|
|
|
import py
|
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)
|
|
|
|
assert result.ret != 0
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stderr.fnmatch_lines([
|
2010-09-15 16:30:50 +08:00
|
|
|
"*ERROR: can't collect:*%s" %(p2.basename,)
|
2009-08-06 21:26:45 +08:00
|
|
|
])
|
2009-12-29 23:29:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_earlyinit(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import py
|
|
|
|
assert hasattr(py.test, 'mark')
|
|
|
|
""")
|
2010-01-11 03:45:37 +08:00
|
|
|
result = testdir.runpython(p)
|
2009-12-29 23:29:48 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
def test_pydoc(self, testdir):
|
2010-04-21 18:50:03 +08:00
|
|
|
result = testdir.runpython_c("import py;help(py.test)")
|
2009-12-29 23:29:48 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
s = result.stdout.str()
|
|
|
|
assert 'MarkGenerator' in s
|
2010-05-12 01:56:22 +08:00
|
|
|
|
|
|
|
def test_double_pytestcmdline(self, testdir):
|
|
|
|
p = testdir.makepyfile(run="""
|
|
|
|
import py
|
|
|
|
py.test.cmdline.main()
|
|
|
|
py.test.cmdline.main()
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_hello():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpython(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 passed*",
|
|
|
|
"*1 passed*",
|
|
|
|
])
|
2010-05-14 18:02:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
@py.test.mark.xfail
|
|
|
|
def test_early_skip(self, testdir):
|
|
|
|
testdir.mkdir("xyz")
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import py
|
|
|
|
def pytest_collect_directory():
|
|
|
|
py.test.skip("early")
|
|
|
|
""")
|
|
|
|
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("""
|
|
|
|
import py
|
|
|
|
class MyFile(py.test.collect.File):
|
|
|
|
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
|
|
|
])
|
|
|
|
|
|
|
|
@py.test.mark.xfail
|
|
|
|
def test_issue93_initialnode_importing_capturing(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
print "should not be seen"
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
assert "should not be seen" not in result.stdout.str()
|
2010-08-02 02:43:02 +08:00
|
|
|
|
|
|
|
@py.test.mark.skipif("not hasattr(os, 'symlink')")
|
|
|
|
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()))
|
|
|
|
print (py.log.Producer)
|
|
|
|
"""))
|
|
|
|
result = testdir.runpython(p, prepend=False)
|
|
|
|
assert not result.ret
|
2010-09-15 16:30:50 +08:00
|
|
|
|
|
|
|
@py.test.mark.xfail(reason="http://bitbucket.org/hpk42/py-trunk/issue/109")
|
|
|
|
def test_sibling_conftest_issue109(self, testdir):
|
|
|
|
"""
|
|
|
|
This test is to make sure that the conftest.py of sibling directories is not loaded
|
|
|
|
if py.test is run for/in one of the siblings directory and those sibling directories
|
|
|
|
are not packaged together with an __init__.py. See bitbucket issue #109.
|
|
|
|
"""
|
|
|
|
for dirname in ['a', 'b']:
|
|
|
|
testdir.tmpdir.ensure(dirname, dir=True)
|
|
|
|
testdir.tmpdir.ensure(dirname, '__init__.py')
|
|
|
|
|
|
|
|
# To create the conftest.py I would like to use testdir.make*-methods
|
|
|
|
# but as far as I have seen they can only create files in testdir.tempdir
|
|
|
|
# Maybe there is a way to explicitly specifiy the directory on which those
|
|
|
|
# methods work or a completely better way to do that?
|
|
|
|
backupTmpDir = testdir.tmpdir
|
|
|
|
testdir.tmpdir = testdir.tmpdir.join(dirname)
|
|
|
|
testdir.makeconftest("""
|
|
|
|
_DIR_NAME = '%s'
|
|
|
|
def pytest_configure(config):
|
|
|
|
if config.args and config.args[0] != _DIR_NAME:
|
|
|
|
raise Exception("py.test run for '" + config.args[0] + "', but '" + _DIR_NAME + "/conftest.py' loaded.")
|
|
|
|
""" % dirname)
|
|
|
|
testdir.tmpdir = backupTmpDir
|
|
|
|
|
|
|
|
for dirname, other_dirname in [('a', 'b'), ('b', 'a')]:
|
|
|
|
result = testdir.runpytest(dirname)
|
|
|
|
assert result.ret == 0, "test_sibling_conftest: py.test run for '%s', but '%s/conftest.py' loaded." % (dirname, other_dirname)
|