1
0

conftest.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # -*- coding: utf-8 -*-
  2. """
  3. tests.pytests.conftest
  4. ~~~~~~~~~~~~~~~~~~~~~~
  5. """
  6. import pathlib
  7. import pytest
  8. from tests.support.runtests import RUNTIME_VARS
  9. from tests.support.unit import TestCase
  10. PYTESTS_SUITE_PATH = pathlib.Path(__file__).parent
  11. @pytest.hookimpl(hookwrapper=True, trylast=True)
  12. def pytest_collection_modifyitems(config, items):
  13. """
  14. called after collection has been performed, may filter or re-order
  15. the items in-place.
  16. :param _pytest.main.Session session: the pytest session object
  17. :param _pytest.config.Config config: pytest config object
  18. :param List[_pytest.nodes.Item] items: list of item objects
  19. """
  20. # Let PyTest or other plugins handle the initial collection
  21. yield
  22. # Check each collected item that's under this package to ensure that none is using TestCase as the base class
  23. for item in items:
  24. if not str(item.fspath).startswith(str(PYTESTS_SUITE_PATH)):
  25. continue
  26. if not item.cls:
  27. # The test item is not part of a class
  28. continue
  29. if issubclass(item.cls, TestCase):
  30. raise RuntimeError(
  31. "The tests under {} MUST NOT use unittest's TestCase class or a subclass of it.".format(
  32. pathlib.Path(str(item.fspath)).relative_to(RUNTIME_VARS.CODE_DIR)
  33. )
  34. )