req_unit_test.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """Test availability of required packages."""
  2. import unittest
  3. from pathlib import Path
  4. import pkg_resources
  5. import pytest
  6. _REQUIREMENTS_PATH = Path(__file__).parent.with_name("requirements.txt")
  7. _EXTRA_REQUIREMENTS_PATH = Path(__file__).parent.parent / "requirements"
  8. class TestRequirements(unittest.TestCase):
  9. """Test availability of required packages."""
  10. def test_requirements(self):
  11. """Test that each required package is available."""
  12. requirements = pkg_resources.parse_requirements(_REQUIREMENTS_PATH.open())
  13. for requirement in requirements:
  14. requirement = str(requirement)
  15. with self.subTest(requirement=requirement):
  16. pkg_resources.require(requirement)
  17. @pytest.mark.xfail(
  18. strict=False
  19. ) # DO not successfully parse recursing of reqs using -r
  20. def test_extra_requirements(self):
  21. """Test that each required package is available."""
  22. if _EXTRA_REQUIREMENTS_PATH.exists():
  23. for extra_req_file in _EXTRA_REQUIREMENTS_PATH.iterdir():
  24. if extra_req_file.is_file() and extra_req_file.suffix == ".txt":
  25. requirements = pkg_resources.parse_requirements(
  26. extra_req_file.open()
  27. )
  28. for requirement in requirements:
  29. requirement = str(requirement)
  30. with self.subTest(requirement=requirement):
  31. pkg_resources.require(requirement)