You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.3KB

  1. """Run testsuites written for nose."""
  2. from _pytest import python
  3. from _pytest import unittest
  4. from _pytest.config import hookimpl
  5. from _pytest.nodes import Item
  6. @hookimpl(trylast=True)
  7. def pytest_runtest_setup(item):
  8. if is_potential_nosetest(item):
  9. if not call_optional(item.obj, "setup"):
  10. # Call module level setup if there is no object level one.
  11. call_optional(item.parent.obj, "setup")
  12. # XXX This implies we only call teardown when setup worked.
  13. item.session._setupstate.addfinalizer((lambda: teardown_nose(item)), item)
  14. def teardown_nose(item):
  15. if is_potential_nosetest(item):
  16. if not call_optional(item.obj, "teardown"):
  17. call_optional(item.parent.obj, "teardown")
  18. def is_potential_nosetest(item: Item) -> bool:
  19. # Extra check needed since we do not do nose style setup/teardown
  20. # on direct unittest style classes.
  21. return isinstance(item, python.Function) and not isinstance(
  22. item, unittest.TestCaseFunction
  23. )
  24. def call_optional(obj, name):
  25. method = getattr(obj, name, None)
  26. isfixture = hasattr(method, "_pytestfixturefunction")
  27. if method is not None and not isfixture and callable(method):
  28. # If there's any problems allow the exception to raise rather than
  29. # silently ignoring them.
  30. method()
  31. return True