2014-01-29 17:20:13 +08:00
|
|
|
import pytest
|
2009-08-10 17:27:13 +08:00
|
|
|
|
2010-10-03 02:49:24 +08:00
|
|
|
def setup_module(mod):
|
2014-01-18 19:31:33 +08:00
|
|
|
mod.nose = pytest.importorskip("nose")
|
2010-09-03 17:07:17 +08:00
|
|
|
|
2009-08-10 17:27:13 +08:00
|
|
|
def test_nose_setup(testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
l = []
|
2011-01-12 00:27:34 +08:00
|
|
|
from nose.tools import with_setup
|
2009-08-10 17:27:13 +08:00
|
|
|
|
2011-01-12 00:27:34 +08:00
|
|
|
@with_setup(lambda: l.append(1), lambda: l.append(2))
|
2009-08-10 17:27:13 +08:00
|
|
|
def test_hello():
|
|
|
|
assert l == [1]
|
2010-09-03 17:09:41 +08:00
|
|
|
|
2009-08-10 17:27:13 +08:00
|
|
|
def test_world():
|
|
|
|
assert l == [1,2]
|
2010-09-03 17:09:41 +08:00
|
|
|
|
2009-08-10 17:27:13 +08:00
|
|
|
test_hello.setup = lambda: l.append(1)
|
|
|
|
test_hello.teardown = lambda: l.append(2)
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p, '-p', 'nose')
|
|
|
|
result.stdout.fnmatch_lines([
|
2010-09-03 17:04:45 +08:00
|
|
|
"*2 passed*"
|
|
|
|
])
|
|
|
|
|
2010-09-03 17:07:17 +08:00
|
|
|
|
2012-09-22 06:23:36 +08:00
|
|
|
def test_setup_func_with_setup_decorator():
|
|
|
|
from _pytest.nose import call_optional
|
|
|
|
l = []
|
|
|
|
class A:
|
2012-10-17 15:21:04 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2012-09-22 06:23:36 +08:00
|
|
|
def f(self):
|
|
|
|
l.append(1)
|
|
|
|
call_optional(A(), "f")
|
|
|
|
assert not l
|
|
|
|
|
|
|
|
|
2012-11-06 04:17:58 +08:00
|
|
|
def test_setup_func_not_callable():
|
|
|
|
from _pytest.nose import call_optional
|
|
|
|
class A:
|
|
|
|
f = 1
|
|
|
|
call_optional(A(), "f")
|
|
|
|
|
2010-09-03 17:04:45 +08:00
|
|
|
def test_nose_setup_func(testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2011-01-12 00:27:34 +08:00
|
|
|
from nose.tools import with_setup
|
|
|
|
|
2010-09-03 17:04:45 +08:00
|
|
|
l = []
|
|
|
|
|
|
|
|
def my_setup():
|
|
|
|
a = 1
|
|
|
|
l.append(a)
|
|
|
|
|
|
|
|
def my_teardown():
|
|
|
|
b = 2
|
|
|
|
l.append(b)
|
|
|
|
|
2011-01-12 00:27:34 +08:00
|
|
|
@with_setup(my_setup, my_teardown)
|
2010-09-03 17:04:45 +08:00
|
|
|
def test_hello():
|
2011-01-12 00:27:34 +08:00
|
|
|
print (l)
|
2010-09-03 17:04:45 +08:00
|
|
|
assert l == [1]
|
2010-09-03 17:09:41 +08:00
|
|
|
|
2010-09-03 17:04:45 +08:00
|
|
|
def test_world():
|
2011-01-12 00:27:34 +08:00
|
|
|
print (l)
|
2010-09-03 17:04:45 +08:00
|
|
|
assert l == [1,2]
|
2010-09-03 17:09:41 +08:00
|
|
|
|
2010-09-03 17:04:45 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p, '-p', 'nose')
|
|
|
|
result.stdout.fnmatch_lines([
|
2010-09-03 18:27:47 +08:00
|
|
|
"*2 passed*"
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
def test_nose_setup_func_failure(testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2011-01-12 00:27:34 +08:00
|
|
|
from nose.tools import with_setup
|
2010-09-03 18:27:47 +08:00
|
|
|
|
2011-01-12 00:27:34 +08:00
|
|
|
l = []
|
2010-09-03 18:27:47 +08:00
|
|
|
my_setup = lambda x: 1
|
|
|
|
my_teardown = lambda x: 2
|
|
|
|
|
2011-01-12 00:27:34 +08:00
|
|
|
@with_setup(my_setup, my_teardown)
|
2010-09-03 18:27:47 +08:00
|
|
|
def test_hello():
|
2011-01-12 00:27:34 +08:00
|
|
|
print (l)
|
2010-09-03 18:27:47 +08:00
|
|
|
assert l == [1]
|
|
|
|
|
|
|
|
def test_world():
|
2011-01-12 00:27:34 +08:00
|
|
|
print (l)
|
2010-09-03 18:27:47 +08:00
|
|
|
assert l == [1,2]
|
|
|
|
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p, '-p', 'nose')
|
|
|
|
result.stdout.fnmatch_lines([
|
2012-10-22 16:55:59 +08:00
|
|
|
"*TypeError: <lambda>()*"
|
2010-09-03 18:27:47 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
|
2010-09-03 18:32:12 +08:00
|
|
|
def test_nose_setup_func_failure_2(testdir):
|
2013-10-12 21:39:22 +08:00
|
|
|
testdir.makepyfile("""
|
2010-09-03 18:32:12 +08:00
|
|
|
l = []
|
|
|
|
|
|
|
|
my_setup = 1
|
|
|
|
my_teardown = 2
|
|
|
|
|
|
|
|
def test_hello():
|
2012-11-06 18:04:11 +08:00
|
|
|
assert l == []
|
2010-09-03 18:32:12 +08:00
|
|
|
|
|
|
|
test_hello.setup = my_setup
|
|
|
|
test_hello.teardown = my_teardown
|
|
|
|
""")
|
2012-11-06 18:04:11 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1)
|
2010-09-03 18:32:12 +08:00
|
|
|
|
2010-09-03 18:27:47 +08:00
|
|
|
def test_nose_setup_partial(testdir):
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.importorskip("functools")
|
2010-09-03 18:27:47 +08:00
|
|
|
p = testdir.makepyfile("""
|
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
l = []
|
|
|
|
|
|
|
|
def my_setup(x):
|
|
|
|
a = x
|
|
|
|
l.append(a)
|
|
|
|
|
|
|
|
def my_teardown(x):
|
|
|
|
b = x
|
|
|
|
l.append(b)
|
|
|
|
|
|
|
|
my_setup_partial = partial(my_setup, 1)
|
|
|
|
my_teardown_partial = partial(my_teardown, 2)
|
|
|
|
|
|
|
|
def test_hello():
|
2011-01-12 00:27:34 +08:00
|
|
|
print (l)
|
2010-09-03 18:27:47 +08:00
|
|
|
assert l == [1]
|
|
|
|
|
|
|
|
def test_world():
|
2011-01-12 00:27:34 +08:00
|
|
|
print (l)
|
2010-09-03 18:27:47 +08:00
|
|
|
assert l == [1,2]
|
|
|
|
|
|
|
|
test_hello.setup = my_setup_partial
|
|
|
|
test_hello.teardown = my_teardown_partial
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p, '-p', 'nose')
|
|
|
|
result.stdout.fnmatch_lines([
|
2009-08-10 17:27:13 +08:00
|
|
|
"*2 passed*"
|
|
|
|
])
|
|
|
|
|
2010-09-03 17:07:17 +08:00
|
|
|
|
2009-08-10 17:27:13 +08:00
|
|
|
def test_nose_test_generator_fixtures(testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
# taken from nose-0.11.1 unit_tests/test_generator_fixtures.py
|
|
|
|
from nose.tools import eq_
|
|
|
|
called = []
|
|
|
|
|
|
|
|
def outer_setup():
|
|
|
|
called.append('outer_setup')
|
|
|
|
|
|
|
|
def outer_teardown():
|
|
|
|
called.append('outer_teardown')
|
|
|
|
|
|
|
|
def inner_setup():
|
|
|
|
called.append('inner_setup')
|
|
|
|
|
|
|
|
def inner_teardown():
|
|
|
|
called.append('inner_teardown')
|
|
|
|
|
|
|
|
def test_gen():
|
|
|
|
called[:] = []
|
|
|
|
for i in range(0, 5):
|
|
|
|
yield check, i
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-08-10 17:27:13 +08:00
|
|
|
def check(i):
|
|
|
|
expect = ['outer_setup']
|
|
|
|
for x in range(0, i):
|
|
|
|
expect.append('inner_setup')
|
|
|
|
expect.append('inner_teardown')
|
|
|
|
expect.append('inner_setup')
|
|
|
|
eq_(called, expect)
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-08-10 17:27:13 +08:00
|
|
|
test_gen.setup = outer_setup
|
|
|
|
test_gen.teardown = outer_teardown
|
|
|
|
check.setup = inner_setup
|
|
|
|
check.teardown = inner_teardown
|
|
|
|
|
|
|
|
class TestClass(object):
|
|
|
|
def setup(self):
|
2011-01-12 00:27:34 +08:00
|
|
|
print ("setup called in %s" % self)
|
2009-08-10 17:27:13 +08:00
|
|
|
self.called = ['setup']
|
|
|
|
|
|
|
|
def teardown(self):
|
2011-01-12 00:27:34 +08:00
|
|
|
print ("teardown called in %s" % self)
|
2009-08-10 17:27:13 +08:00
|
|
|
eq_(self.called, ['setup'])
|
|
|
|
self.called.append('teardown')
|
|
|
|
|
|
|
|
def test(self):
|
2011-01-12 00:27:34 +08:00
|
|
|
print ("test called in %s" % self)
|
2009-08-10 17:27:13 +08:00
|
|
|
for i in range(0, 5):
|
|
|
|
yield self.check, i
|
|
|
|
|
|
|
|
def check(self, i):
|
2011-01-12 00:27:34 +08:00
|
|
|
print ("check called in %s" % self)
|
2009-08-10 17:27:13 +08:00
|
|
|
expect = ['setup']
|
|
|
|
#for x in range(0, i):
|
|
|
|
# expect.append('setup')
|
|
|
|
# expect.append('teardown')
|
|
|
|
#expect.append('setup')
|
|
|
|
eq_(self.called, expect)
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p, '-p', 'nose')
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*10 passed*"
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2009-10-23 21:11:53 +08:00
|
|
|
def test_module_level_setup(testdir):
|
|
|
|
testdir.makepyfile("""
|
2009-10-23 21:27:59 +08:00
|
|
|
from nose.tools import with_setup
|
2009-10-23 21:11:53 +08:00
|
|
|
items = {}
|
2010-09-03 17:09:41 +08:00
|
|
|
|
2009-10-23 21:11:53 +08:00
|
|
|
def setup():
|
|
|
|
items[1]=1
|
|
|
|
|
2009-10-23 21:27:59 +08:00
|
|
|
def teardown():
|
|
|
|
del items[1]
|
|
|
|
|
|
|
|
def setup2():
|
|
|
|
items[2] = 2
|
|
|
|
|
|
|
|
def teardown2():
|
|
|
|
del items[2]
|
|
|
|
|
|
|
|
def test_setup_module_setup():
|
|
|
|
assert items[1] == 1
|
|
|
|
|
|
|
|
@with_setup(setup2, teardown2)
|
|
|
|
def test_local_setup():
|
|
|
|
assert items[2] == 2
|
|
|
|
assert 1 not in items
|
2009-10-23 21:11:53 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest('-p', 'nose')
|
|
|
|
result.stdout.fnmatch_lines([
|
2009-10-23 21:27:59 +08:00
|
|
|
"*2 passed*",
|
2009-10-23 21:11:53 +08:00
|
|
|
])
|
2009-10-23 22:17:06 +08:00
|
|
|
|
2010-09-03 17:07:17 +08:00
|
|
|
|
2009-10-23 22:17:06 +08:00
|
|
|
def test_nose_style_setup_teardown(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
l = []
|
2010-09-03 17:09:41 +08:00
|
|
|
|
2009-10-23 22:17:06 +08:00
|
|
|
def setup_module():
|
|
|
|
l.append(1)
|
|
|
|
|
|
|
|
def teardown_module():
|
|
|
|
del l[0]
|
|
|
|
|
|
|
|
def test_hello():
|
|
|
|
assert l == [1]
|
|
|
|
|
|
|
|
def test_world():
|
|
|
|
assert l == [1]
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest('-p', 'nose')
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*2 passed*",
|
|
|
|
])
|
2011-07-06 03:23:59 +08:00
|
|
|
|
|
|
|
def test_nose_setup_ordering(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def setup_module(mod):
|
|
|
|
mod.visited = True
|
|
|
|
|
|
|
|
class TestClass:
|
|
|
|
def setup(self):
|
|
|
|
assert visited
|
|
|
|
def test_first(self):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 passed*",
|
|
|
|
])
|
2013-02-08 00:53:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_apiwrapper_problem_issue260(testdir):
|
|
|
|
# this would end up trying a call a optional teardown on the class
|
|
|
|
# for plain unittests we dont want nose behaviour
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import unittest
|
|
|
|
class TestCase(unittest.TestCase):
|
|
|
|
def setup(self):
|
|
|
|
#should not be called in unittest testcases
|
|
|
|
assert 0, 'setup'
|
|
|
|
def teardown(self):
|
|
|
|
#should not be called in unittest testcases
|
|
|
|
assert 0, 'teardown'
|
|
|
|
def setUp(self):
|
|
|
|
print('setup')
|
|
|
|
def tearDown(self):
|
|
|
|
print('teardown')
|
|
|
|
def test_fun(self):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines("*1 passed*")
|
|
|
|
|
2013-04-16 15:04:05 +08:00
|
|
|
@pytest.mark.skipif("sys.version_info < (2,6)")
|
2013-03-25 17:52:02 +08:00
|
|
|
def test_setup_teardown_linking_issue265(testdir):
|
2013-04-16 15:04:05 +08:00
|
|
|
# we accidentally didnt integrate nose setupstate with normal setupstate
|
2013-03-25 17:52:02 +08:00
|
|
|
# this test ensures that won't happen again
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import pytest
|
2013-02-08 00:53:13 +08:00
|
|
|
|
2013-03-25 17:52:02 +08:00
|
|
|
class TestGeneric(object):
|
|
|
|
def test_nothing(self):
|
|
|
|
"""Tests the API of the implementation (for generic and specialized)."""
|
|
|
|
|
2013-04-16 15:04:05 +08:00
|
|
|
@pytest.mark.skipif("True", reason=
|
|
|
|
"Skip tests to check if teardown is skipped as well.")
|
2013-03-25 17:52:02 +08:00
|
|
|
class TestSkipTeardown(TestGeneric):
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""Sets up my specialized implementation for $COOL_PLATFORM."""
|
|
|
|
raise Exception("should not call setup for skipped tests")
|
|
|
|
|
|
|
|
def teardown(self):
|
|
|
|
"""Undoes the setup."""
|
|
|
|
raise Exception("should not call teardown for skipped tests")
|
|
|
|
''')
|
2013-08-16 17:33:58 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(passed=1, skipped=1)
|
2013-03-25 17:52:02 +08:00
|
|
|
|
2014-03-14 21:04:54 +08:00
|
|
|
|
2013-03-14 23:10:33 +08:00
|
|
|
def test_SkipTest_during_collection(testdir):
|
2014-03-14 21:04:54 +08:00
|
|
|
p = testdir.makepyfile("""
|
2013-03-14 23:10:33 +08:00
|
|
|
import nose
|
|
|
|
raise nose.SkipTest("during collection")
|
|
|
|
def test_failing():
|
|
|
|
assert False
|
|
|
|
""")
|
2014-03-14 21:04:54 +08:00
|
|
|
result = testdir.runpytest(p)
|
|
|
|
outcome = result.parseoutcomes()
|
|
|
|
outcome.pop('seconds')
|
|
|
|
assert outcome == dict(skipped=1)
|
|
|
|
|
|
|
|
|
|
|
|
def test_SkipTest_in_test(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import nose
|
|
|
|
|
|
|
|
def test_skipping():
|
|
|
|
raise nose.SkipTest("in test")
|
|
|
|
""")
|
2013-08-16 17:33:58 +08:00
|
|
|
reprec = testdir.inline_run()
|
|
|
|
reprec.assertoutcome(skipped=1)
|