Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" run test suites written for nose. """ 

2from _pytest import python 

3from _pytest import unittest 

4from _pytest.config import hookimpl 

5from _pytest.nodes import Item 

6 

7 

8@hookimpl(trylast=True) 

9def pytest_runtest_setup(item): 

10 if is_potential_nosetest(item): 

11 if not call_optional(item.obj, "setup"): 

12 # call module level setup if there is no object level one 

13 call_optional(item.parent.obj, "setup") 

14 # XXX this implies we only call teardown when setup worked 

15 item.session._setupstate.addfinalizer((lambda: teardown_nose(item)), item) 

16 

17 

18def teardown_nose(item): 

19 if is_potential_nosetest(item): 

20 if not call_optional(item.obj, "teardown"): 

21 call_optional(item.parent.obj, "teardown") 

22 

23 

24def is_potential_nosetest(item: Item) -> bool: 

25 # extra check needed since we do not do nose style setup/teardown 

26 # on direct unittest style classes 

27 return isinstance(item, python.Function) and not isinstance( 

28 item, unittest.TestCaseFunction 

29 ) 

30 

31 

32def call_optional(obj, name): 

33 method = getattr(obj, name, None) 

34 isfixture = hasattr(method, "_pytestfixturefunction") 

35 if method is not None and not isfixture and callable(method): 

36 # If there's any problems allow the exception to raise rather than 

37 # silently ignoring them 

38 method() 

39 return True