2008-08-16 23:26:59 +08:00
|
|
|
#
|
|
|
|
# test correct setup/teardowns at
|
|
|
|
# module, class, and instance level
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_module_and_function_setup(testdir):
|
2010-07-27 03:15:15 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2008-08-16 23:26:59 +08:00
|
|
|
modlevel = []
|
|
|
|
def setup_module(module):
|
|
|
|
assert not modlevel
|
|
|
|
module.modlevel.append(42)
|
|
|
|
|
|
|
|
def teardown_module(module):
|
|
|
|
modlevel.pop()
|
|
|
|
|
|
|
|
def setup_function(function):
|
|
|
|
function.answer = 17
|
|
|
|
|
|
|
|
def teardown_function(function):
|
|
|
|
del function.answer
|
|
|
|
|
|
|
|
def test_modlevel():
|
|
|
|
assert modlevel[0] == 42
|
|
|
|
assert test_modlevel.answer == 17
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
class TestFromClass:
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_module(self):
|
|
|
|
assert modlevel[0] == 42
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not hasattr(test_modlevel, 'answer')
|
2008-08-16 23:26:59 +08:00
|
|
|
""")
|
2010-07-27 03:15:15 +08:00
|
|
|
rep = reprec.matchreport("test_modlevel")
|
|
|
|
assert rep.passed
|
|
|
|
rep = reprec.matchreport("test_module")
|
|
|
|
assert rep.passed
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_class_setup(testdir):
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2008-08-16 23:26:59 +08:00
|
|
|
class TestSimpleClassSetup:
|
|
|
|
clslevel = []
|
|
|
|
def setup_class(cls):
|
|
|
|
cls.clslevel.append(23)
|
|
|
|
|
|
|
|
def teardown_class(cls):
|
|
|
|
cls.clslevel.pop()
|
|
|
|
|
|
|
|
def test_classlevel(self):
|
|
|
|
assert self.clslevel[0] == 23
|
|
|
|
|
|
|
|
class TestInheritedClassSetupStillWorks(TestSimpleClassSetup):
|
|
|
|
def test_classlevel_anothertime(self):
|
|
|
|
assert self.clslevel == [23]
|
|
|
|
|
|
|
|
def test_cleanup():
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not TestSimpleClassSetup.clslevel
|
2008-08-16 23:26:59 +08:00
|
|
|
assert not TestInheritedClassSetupStillWorks.clslevel
|
|
|
|
""")
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec.assertoutcome(passed=1+2+1)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-07-08 22:41:30 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_method_setup(testdir):
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2008-08-16 23:26:59 +08:00
|
|
|
class TestSetupMethod:
|
|
|
|
def setup_method(self, meth):
|
2010-07-27 03:15:15 +08:00
|
|
|
self.methsetup = meth
|
2008-08-16 23:26:59 +08:00
|
|
|
def teardown_method(self, meth):
|
2010-07-27 03:15:15 +08:00
|
|
|
del self.methsetup
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def test_some(self):
|
2010-07-27 03:15:15 +08:00
|
|
|
assert self.methsetup == self.test_some
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def test_other(self):
|
|
|
|
assert self.methsetup == self.test_other
|
|
|
|
""")
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec.assertoutcome(passed=2)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_method_generator_setup(testdir):
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2010-07-27 03:15:15 +08:00
|
|
|
class TestSetupTeardownOnInstance:
|
2008-08-16 23:26:59 +08:00
|
|
|
def setup_class(cls):
|
2010-07-27 03:15:15 +08:00
|
|
|
cls.classsetup = True
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def setup_method(self, method):
|
2010-07-27 03:15:15 +08:00
|
|
|
self.methsetup = method
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
def test_generate(self):
|
2010-07-27 03:15:15 +08:00
|
|
|
assert self.classsetup
|
|
|
|
assert self.methsetup == self.test_generate
|
2008-08-16 23:26:59 +08:00
|
|
|
yield self.generated, 5
|
|
|
|
yield self.generated, 2
|
|
|
|
|
|
|
|
def generated(self, value):
|
2010-07-27 03:15:15 +08:00
|
|
|
assert self.classsetup
|
|
|
|
assert self.methsetup == self.test_generate
|
2008-08-16 23:26:59 +08:00
|
|
|
assert value == 5
|
|
|
|
""")
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec.assertoutcome(passed=1, failed=1)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_func_generator_setup(testdir):
|
2010-07-27 03:15:15 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2008-08-16 23:26:59 +08:00
|
|
|
import sys
|
|
|
|
|
|
|
|
def setup_module(mod):
|
2009-09-05 03:47:49 +08:00
|
|
|
print ("setup_module")
|
2008-08-16 23:26:59 +08:00
|
|
|
mod.x = []
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
def setup_function(fun):
|
2009-09-05 03:47:49 +08:00
|
|
|
print ("setup_function")
|
2008-08-16 23:26:59 +08:00
|
|
|
x.append(1)
|
|
|
|
|
|
|
|
def teardown_function(fun):
|
2009-09-05 03:47:49 +08:00
|
|
|
print ("teardown_function")
|
2008-08-16 23:26:59 +08:00
|
|
|
x.pop()
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_one():
|
|
|
|
assert x == [1]
|
|
|
|
def check():
|
2009-09-05 03:47:49 +08:00
|
|
|
print ("check")
|
|
|
|
sys.stderr.write("e\\n")
|
2008-08-16 23:26:59 +08:00
|
|
|
assert x == [1]
|
2010-07-27 03:15:15 +08:00
|
|
|
yield check
|
2008-08-16 23:26:59 +08:00
|
|
|
assert x == [1]
|
|
|
|
""")
|
2010-07-27 03:15:15 +08:00
|
|
|
rep = reprec.matchreport("test_one", names="pytest_runtest_logreport")
|
|
|
|
assert rep.passed
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def test_method_setup_uses_fresh_instances(testdir):
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
2010-07-27 03:15:15 +08:00
|
|
|
class TestSelfState1:
|
2009-08-07 00:15:21 +08:00
|
|
|
memory = []
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_hello(self):
|
2009-08-07 00:15:21 +08:00
|
|
|
self.memory.append(self)
|
|
|
|
|
2008-08-16 23:26:59 +08:00
|
|
|
def test_afterhello(self):
|
2009-08-07 00:15:21 +08:00
|
|
|
assert self != self.memory[0]
|
2008-08-16 23:26:59 +08:00
|
|
|
""")
|
2009-08-07 00:15:21 +08:00
|
|
|
reprec.assertoutcome(passed=2, failed=0)
|
2009-04-05 03:06:20 +08:00
|
|
|
|
2010-01-27 19:09:30 +08:00
|
|
|
def test_failing_setup_calls_teardown(testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def setup_module(mod):
|
|
|
|
raise ValueError(42)
|
|
|
|
def test_function():
|
|
|
|
assert 0
|
|
|
|
def teardown_module(mod):
|
|
|
|
raise ValueError(43)
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*42*",
|
|
|
|
"*43*",
|
|
|
|
"*2 error*"
|
|
|
|
])
|
|
|
|
|
2010-01-28 21:20:58 +08:00
|
|
|
def test_setup_that_skips_calledagain_and_teardown(testdir):
|
2010-01-27 19:09:30 +08:00
|
|
|
p = testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-01-27 19:09:30 +08:00
|
|
|
def setup_module(mod):
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("x")
|
2010-01-27 19:09:30 +08:00
|
|
|
def test_function1():
|
|
|
|
pass
|
|
|
|
def test_function2():
|
|
|
|
pass
|
|
|
|
def teardown_module(mod):
|
|
|
|
raise ValueError(43)
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
2010-01-28 21:20:58 +08:00
|
|
|
"*ValueError*43*",
|
|
|
|
"*2 skipped*1 error*",
|
2010-01-27 19:09:30 +08:00
|
|
|
])
|
2010-01-28 21:20:58 +08:00
|
|
|
|
|
|
|
def test_setup_fails_again_on_all_tests(testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-01-28 21:20:58 +08:00
|
|
|
def setup_module(mod):
|
|
|
|
raise ValueError(42)
|
|
|
|
def test_function1():
|
|
|
|
pass
|
|
|
|
def test_function2():
|
|
|
|
pass
|
|
|
|
def teardown_module(mod):
|
|
|
|
raise ValueError(43)
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*3 error*"
|
|
|
|
])
|
|
|
|
assert "passed" not in result.stdout.str()
|
|
|
|
|
2011-07-07 22:43:39 +08:00
|
|
|
def test_setup_funcarg_setup_when_outer_scope_fails(testdir):
|
2010-01-28 22:36:27 +08:00
|
|
|
p = testdir.makepyfile("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2010-01-28 22:36:27 +08:00
|
|
|
def setup_module(mod):
|
|
|
|
raise ValueError(42)
|
|
|
|
def pytest_funcarg__hello(request):
|
2010-04-21 20:46:41 +08:00
|
|
|
raise ValueError("xyz43")
|
2010-01-28 22:36:27 +08:00
|
|
|
def test_function1(hello):
|
|
|
|
pass
|
|
|
|
def test_function2(hello):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*function1*",
|
|
|
|
"*ValueError*42*",
|
|
|
|
"*function2*",
|
|
|
|
"*ValueError*42*",
|
|
|
|
"*2 error*"
|
|
|
|
])
|
2010-04-21 20:46:41 +08:00
|
|
|
assert "xyz43" not in result.stdout.str()
|
2010-01-28 22:36:27 +08:00
|
|
|
|
2010-01-27 19:09:30 +08:00
|
|
|
|
|
|
|
|