2014-01-29 17:20:13 +08:00
|
|
|
import pytest
|
2020-12-01 06:07:26 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2009-08-10 17:27:13 +08:00
|
|
|
|
2017-07-17 07:25:09 +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
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_nose_setup(pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2011-01-12 00:27:34 +08:00
|
|
|
from nose.tools import with_setup
|
2009-08-10 17:27:13 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
@with_setup(lambda: values.append(1), lambda: values.append(2))
|
2009-08-10 17:27:13 +08:00
|
|
|
def test_hello():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1]
|
2010-09-03 17:09:41 +08:00
|
|
|
|
2009-08-10 17:27:13 +08:00
|
|
|
def test_world():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1,2]
|
2010-09-03 17:09:41 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
test_hello.setup = lambda: values.append(1)
|
|
|
|
test_hello.teardown = lambda: values.append(2)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest(p, "-p", "nose")
|
2015-04-28 17:54:45 +08:00
|
|
|
result.assert_outcomes(passed=2)
|
2010-09-03 17:04:45 +08:00
|
|
|
|
2010-09-03 17:07:17 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_setup_func_with_setup_decorator() -> None:
|
2012-09-22 06:23:36 +08:00
|
|
|
from _pytest.nose import call_optional
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
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):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2022-10-10 04:16:33 +08:00
|
|
|
call_optional(A(), "f", "A.f")
|
2017-11-04 23:17:20 +08:00
|
|
|
assert not values
|
2012-09-22 06:23:36 +08:00
|
|
|
|
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_setup_func_not_callable() -> None:
|
2012-11-06 04:17:58 +08:00
|
|
|
from _pytest.nose import call_optional
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class A:
|
2012-11-06 04:17:58 +08:00
|
|
|
f = 1
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2022-10-10 04:16:33 +08:00
|
|
|
call_optional(A(), "f", "A.f")
|
2012-11-06 04:17:58 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_nose_setup_func(pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2011-01-12 00:27:34 +08:00
|
|
|
from nose.tools import with_setup
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2010-09-03 17:04:45 +08:00
|
|
|
|
|
|
|
def my_setup():
|
|
|
|
a = 1
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(a)
|
2010-09-03 17:04:45 +08:00
|
|
|
|
|
|
|
def my_teardown():
|
|
|
|
b = 2
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(b)
|
2010-09-03 17:04:45 +08:00
|
|
|
|
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():
|
2018-11-22 16:15:14 +08:00
|
|
|
print(values)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1]
|
2010-09-03 17:09:41 +08:00
|
|
|
|
2010-09-03 17:04:45 +08:00
|
|
|
def test_world():
|
2018-11-22 16:15:14 +08:00
|
|
|
print(values)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1,2]
|
2010-09-03 17:09:41 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest(p, "-p", "nose")
|
2015-04-28 17:54:45 +08:00
|
|
|
result.assert_outcomes(passed=2)
|
2010-09-03 18:27:47 +08:00
|
|
|
|
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_nose_setup_func_failure(pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2011-01-12 00:27:34 +08:00
|
|
|
from nose.tools import with_setup
|
2010-09-03 18:27:47 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
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():
|
2018-11-22 16:15:14 +08:00
|
|
|
print(values)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1]
|
2010-09-03 18:27:47 +08:00
|
|
|
|
|
|
|
def test_world():
|
2018-11-22 16:15:14 +08:00
|
|
|
print(values)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1,2]
|
2010-09-03 18:27:47 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest(p, "-p", "nose")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*TypeError: <lambda>()*"])
|
2010-09-03 18:27:47 +08:00
|
|
|
|
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_nose_setup_func_failure_2(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2010-09-03 18:32:12 +08:00
|
|
|
|
|
|
|
my_setup = 1
|
|
|
|
my_teardown = 2
|
|
|
|
|
|
|
|
def test_hello():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == []
|
2010-09-03 18:32:12 +08:00
|
|
|
|
|
|
|
test_hello.setup = my_setup
|
|
|
|
test_hello.teardown = my_teardown
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
reprec = pytester.inline_run()
|
2012-11-06 18:04:11 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
2010-09-03 18:32:12 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_nose_setup_partial(pytester: Pytester) -> None:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.importorskip("functools")
|
2020-12-01 06:07:26 +08:00
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-09-03 18:27:47 +08:00
|
|
|
from functools import partial
|
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2010-09-03 18:27:47 +08:00
|
|
|
|
|
|
|
def my_setup(x):
|
|
|
|
a = x
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(a)
|
2010-09-03 18:27:47 +08:00
|
|
|
|
|
|
|
def my_teardown(x):
|
|
|
|
b = x
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(b)
|
2010-09-03 18:27:47 +08:00
|
|
|
|
|
|
|
my_setup_partial = partial(my_setup, 1)
|
|
|
|
my_teardown_partial = partial(my_teardown, 2)
|
|
|
|
|
|
|
|
def test_hello():
|
2018-11-22 16:15:14 +08:00
|
|
|
print(values)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1]
|
2010-09-03 18:27:47 +08:00
|
|
|
|
|
|
|
def test_world():
|
2018-11-22 16:15:14 +08:00
|
|
|
print(values)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1,2]
|
2010-09-03 18:27:47 +08:00
|
|
|
|
|
|
|
test_hello.setup = my_setup_partial
|
|
|
|
test_hello.teardown = my_teardown_partial
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest(p, "-p", "nose")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2009-08-10 17:27:13 +08:00
|
|
|
|
2010-09-03 17:07:17 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_module_level_setup(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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():
|
2021-11-06 17:41:09 +08:00
|
|
|
items.setdefault("setup", []).append("up")
|
2009-10-23 21:11:53 +08:00
|
|
|
|
2009-10-23 21:27:59 +08:00
|
|
|
def teardown():
|
2021-11-06 17:41:09 +08:00
|
|
|
items.setdefault("setup", []).append("down")
|
2009-10-23 21:27:59 +08:00
|
|
|
|
|
|
|
def setup2():
|
2021-11-06 17:41:09 +08:00
|
|
|
items.setdefault("setup2", []).append("up")
|
2009-10-23 21:27:59 +08:00
|
|
|
|
|
|
|
def teardown2():
|
2021-11-06 17:41:09 +08:00
|
|
|
items.setdefault("setup2", []).append("down")
|
2009-10-23 21:27:59 +08:00
|
|
|
|
|
|
|
def test_setup_module_setup():
|
2021-11-06 17:41:09 +08:00
|
|
|
assert items["setup"] == ["up"]
|
|
|
|
|
|
|
|
def test_setup_module_setup_again():
|
|
|
|
assert items["setup"] == ["up"]
|
2009-10-23 21:27:59 +08:00
|
|
|
|
|
|
|
@with_setup(setup2, teardown2)
|
|
|
|
def test_local_setup():
|
2021-11-06 17:41:09 +08:00
|
|
|
assert items["setup"] == ["up"]
|
|
|
|
assert items["setup2"] == ["up"]
|
|
|
|
|
|
|
|
@with_setup(setup2, teardown2)
|
|
|
|
def test_local_setup_again():
|
|
|
|
assert items["setup"] == ["up"]
|
|
|
|
assert items["setup2"] == ["up", "down", "up"]
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest("-p", "nose")
|
2021-11-06 17:41:09 +08:00
|
|
|
result.stdout.fnmatch_lines(["*4 passed*"])
|
2009-10-23 22:17:06 +08:00
|
|
|
|
2010-09-03 17:07:17 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_nose_style_setup_teardown(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2010-09-03 17:09:41 +08:00
|
|
|
|
2009-10-23 22:17:06 +08:00
|
|
|
def setup_module():
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(1)
|
2009-10-23 22:17:06 +08:00
|
|
|
|
|
|
|
def teardown_module():
|
2017-11-04 23:17:20 +08:00
|
|
|
del values[0]
|
2009-10-23 22:17:06 +08:00
|
|
|
|
|
|
|
def test_hello():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1]
|
2009-10-23 22:17:06 +08:00
|
|
|
|
|
|
|
def test_world():
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values == [1]
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest("-p", "nose")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*2 passed*"])
|
2011-07-06 03:23:59 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2021-03-05 04:45:57 +08:00
|
|
|
def test_fixtures_nose_setup_issue8394(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
|
|
|
def setup_module():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def teardown_module():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setup_function(func):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def teardown_function(func):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_world():
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Test(object):
|
|
|
|
def setup_class(cls):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def teardown_class(cls):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setup_method(self, meth):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def teardown_method(self, meth):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_method(self): pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
match = "*no docstring available*"
|
|
|
|
result = pytester.runpytest("--fixtures")
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.no_fnmatch_line(match)
|
|
|
|
|
|
|
|
result = pytester.runpytest("--fixtures", "-v")
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines([match, match, match, match])
|
|
|
|
|
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_nose_setup_ordering(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2011-07-06 03:23:59 +08:00
|
|
|
def setup_module(mod):
|
|
|
|
mod.visited = True
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2011-07-06 03:23:59 +08:00
|
|
|
def setup(self):
|
|
|
|
assert visited
|
2021-11-13 20:03:44 +08:00
|
|
|
self.visited_cls = True
|
2011-07-06 03:23:59 +08:00
|
|
|
def test_first(self):
|
2021-11-13 20:03:44 +08:00
|
|
|
assert visited
|
|
|
|
assert self.visited_cls
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2013-02-08 00:53:13 +08:00
|
|
|
|
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_apiwrapper_problem_issue260(pytester: Pytester) -> None:
|
2018-05-13 18:06:09 +08:00
|
|
|
# this would end up trying a call an optional teardown on the class
|
2019-11-01 11:28:25 +08:00
|
|
|
# for plain unittests we don't want nose behaviour
|
2020-12-01 06:07:26 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-02-08 00:53:13 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest()
|
2015-04-28 17:54:45 +08:00
|
|
|
result.assert_outcomes(passed=1)
|
2013-02-08 00:53:13 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_setup_teardown_linking_issue265(pytester: Pytester) -> None:
|
2019-08-01 21:10:39 +08:00
|
|
|
# we accidentally didn't integrate nose setupstate with normal setupstate
|
2013-03-25 17:52:02 +08:00
|
|
|
# this test ensures that won't happen again
|
2020-12-01 06:07:26 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
2013-03-25 17:52:02 +08:00
|
|
|
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")
|
2018-05-23 22:48:46 +08:00
|
|
|
'''
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
reprec = pytester.runpytest()
|
2015-04-28 17:54:45 +08:00
|
|
|
reprec.assert_outcomes(passed=1, skipped=1)
|
2013-03-25 17:52:02 +08:00
|
|
|
|
2014-03-14 21:04:54 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_SkipTest_during_collection(pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-03-14 23:10:33 +08:00
|
|
|
import nose
|
|
|
|
raise nose.SkipTest("during collection")
|
|
|
|
def test_failing():
|
|
|
|
assert False
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest(p)
|
2022-02-23 21:59:54 +08:00
|
|
|
result.assert_outcomes(skipped=1, warnings=0)
|
2014-03-14 21:04:54 +08:00
|
|
|
|
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_SkipTest_in_test(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2014-03-14 21:04:54 +08:00
|
|
|
import nose
|
|
|
|
|
|
|
|
def test_skipping():
|
|
|
|
raise nose.SkipTest("in test")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
reprec = pytester.inline_run()
|
2013-08-16 17:33:58 +08:00
|
|
|
reprec.assertoutcome(skipped=1)
|
2015-08-06 16:51:18 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_istest_function_decorator(pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-08-06 16:51:18 +08:00
|
|
|
import nose.tools
|
|
|
|
@nose.tools.istest
|
|
|
|
def not_test_prefix():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest(p)
|
2015-08-06 16:51:18 +08:00
|
|
|
result.assert_outcomes(passed=1)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_nottest_function_decorator(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-08-06 16:51:18 +08:00
|
|
|
import nose.tools
|
|
|
|
@nose.tools.nottest
|
|
|
|
def test_prefix():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
reprec = pytester.inline_run()
|
2015-08-06 16:51:18 +08:00
|
|
|
assert not reprec.getfailedcollections()
|
|
|
|
calls = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
assert not calls
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_istest_class_decorator(pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-08-06 16:51:18 +08:00
|
|
|
import nose.tools
|
|
|
|
@nose.tools.istest
|
2017-02-17 02:41:51 +08:00
|
|
|
class NotTestPrefix(object):
|
2015-08-06 16:51:18 +08:00
|
|
|
def test_method(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest(p)
|
2015-08-06 16:51:18 +08:00
|
|
|
result.assert_outcomes(passed=1)
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_nottest_class_decorator(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-08-06 16:51:18 +08:00
|
|
|
import nose.tools
|
|
|
|
@nose.tools.nottest
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestPrefix(object):
|
2015-08-06 16:51:18 +08:00
|
|
|
def test_method(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
reprec = pytester.inline_run()
|
2015-08-06 16:51:18 +08:00
|
|
|
assert not reprec.getfailedcollections()
|
|
|
|
calls = reprec.getreports("pytest_runtest_logreport")
|
|
|
|
assert not calls
|
2019-01-28 22:50:04 +08:00
|
|
|
|
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_skip_test_with_unicode(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-06-03 06:40:34 +08:00
|
|
|
"""\
|
2019-01-28 22:50:04 +08:00
|
|
|
import unittest
|
|
|
|
class TestClass():
|
|
|
|
def test_io(self):
|
2019-06-05 08:48:06 +08:00
|
|
|
raise unittest.SkipTest('😊')
|
2019-06-03 06:40:34 +08:00
|
|
|
"""
|
2019-01-28 22:50:04 +08:00
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 skipped *"])
|
2020-01-21 05:08:09 +08:00
|
|
|
|
|
|
|
|
2020-12-01 06:07:26 +08:00
|
|
|
def test_raises(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2020-01-21 05:08:09 +08:00
|
|
|
"""
|
|
|
|
from nose.tools import raises
|
|
|
|
|
|
|
|
@raises(RuntimeError)
|
2020-01-21 18:30:32 +08:00
|
|
|
def test_raises_runtimeerror():
|
2020-01-21 05:08:09 +08:00
|
|
|
raise RuntimeError
|
2020-01-21 18:30:32 +08:00
|
|
|
|
|
|
|
@raises(Exception)
|
|
|
|
def test_raises_baseexception_not_caught():
|
|
|
|
raise BaseException
|
|
|
|
|
|
|
|
@raises(BaseException)
|
|
|
|
def test_raises_baseexception_caught():
|
|
|
|
raise BaseException
|
2020-01-21 05:08:09 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-01 06:07:26 +08:00
|
|
|
result = pytester.runpytest("-vv")
|
2020-01-21 18:30:32 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2020-01-21 19:26:29 +08:00
|
|
|
"test_raises.py::test_raises_runtimeerror PASSED*",
|
|
|
|
"test_raises.py::test_raises_baseexception_not_caught FAILED*",
|
|
|
|
"test_raises.py::test_raises_baseexception_caught PASSED*",
|
2020-01-21 18:30:32 +08:00
|
|
|
"*= FAILURES =*",
|
|
|
|
"*_ test_raises_baseexception_not_caught _*",
|
|
|
|
"",
|
|
|
|
"arg = (), kw = {}",
|
|
|
|
"",
|
|
|
|
" def newfunc(*arg, **kw):",
|
|
|
|
" try:",
|
|
|
|
"> func(*arg, **kw)",
|
|
|
|
"",
|
|
|
|
"*/nose/*: ",
|
|
|
|
"_ _ *",
|
|
|
|
"",
|
|
|
|
" @raises(Exception)",
|
|
|
|
" def test_raises_baseexception_not_caught():",
|
|
|
|
"> raise BaseException",
|
|
|
|
"E BaseException",
|
|
|
|
"",
|
|
|
|
"test_raises.py:9: BaseException",
|
|
|
|
"* 1 failed, 2 passed *",
|
|
|
|
]
|
|
|
|
)
|
2021-12-25 18:03:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_nose_setup_skipped_if_non_callable(pytester: Pytester) -> None:
|
|
|
|
"""Regression test for #9391."""
|
|
|
|
p = pytester.makepyfile(
|
|
|
|
__init__="",
|
|
|
|
setup="""
|
|
|
|
""",
|
|
|
|
teardown="""
|
|
|
|
""",
|
|
|
|
test_it="""
|
|
|
|
from . import setup, teardown
|
|
|
|
|
|
|
|
def test_it():
|
|
|
|
pass
|
|
|
|
""",
|
|
|
|
)
|
|
|
|
result = pytester.runpytest(p, "-p", "nose")
|
|
|
|
assert result.ret == 0
|
2023-01-28 01:33:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("fixture_name", ("teardown", "teardown_class"))
|
|
|
|
def test_teardown_fixture_not_called_directly(fixture_name, pytester: Pytester) -> None:
|
|
|
|
"""Regression test for #10597."""
|
|
|
|
p = pytester.makepyfile(
|
|
|
|
f"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
class TestHello:
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def {fixture_name}(self):
|
|
|
|
yield
|
|
|
|
|
|
|
|
def test_hello(self, {fixture_name}):
|
|
|
|
assert True
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest(p, "-p", "nose")
|
|
|
|
assert result.ret == 0
|