conftest.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright 2016 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import sys
  16. import mock
  17. import pytest
  18. def pytest_configure():
  19. """Load public certificate and private key."""
  20. import __res
  21. pytest.private_key_bytes = __res.find("data/privatekey.pem")
  22. pytest.public_cert_bytes = __res.find("data/public_cert.pem")
  23. @pytest.fixture
  24. def mock_non_existent_module(monkeypatch):
  25. """Mocks a non-existing module in sys.modules.
  26. Additionally mocks any non-existing modules specified in the dotted path.
  27. """
  28. def _mock_non_existent_module(path):
  29. parts = path.split(".")
  30. partial = []
  31. for part in parts:
  32. partial.append(part)
  33. current_module = ".".join(partial)
  34. if current_module not in sys.modules:
  35. monkeypatch.setitem(sys.modules, current_module, mock.MagicMock())
  36. return _mock_non_existent_module