conftest.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. # The purpose of this class is to create fixtures or methods that can be shared among all tests.
  4. from unittest.mock import MagicMock, patch
  5. import pytest
  6. from UM.Application import Application
  7. from UM.Qt.QtApplication import QtApplication # QtApplication import is required, even though it isn't used.
  8. from cura.CuraApplication import CuraApplication
  9. from cura.Settings.ExtruderManager import ExtruderManager
  10. from cura.Settings.MachineManager import MachineManager
  11. from cura.UI.MachineActionManager import MachineActionManager
  12. from UM.Settings.ContainerRegistry import ContainerRegistry
  13. # Create a CuraApplication object that will be shared among all tests. It needs to be initialized.
  14. # Since we need to use it more that once, we create the application the first time and use its instance afterwards.
  15. @pytest.fixture()
  16. def application() -> CuraApplication:
  17. app = MagicMock()
  18. return app
  19. @pytest.fixture()
  20. def um_application() -> Application:
  21. app = MagicMock()
  22. app.getInstance = MagicMock(return_value=app)
  23. return app
  24. # Returns a MachineActionManager instance.
  25. @pytest.fixture()
  26. def machine_action_manager(application) -> MachineActionManager:
  27. return MachineActionManager(application)
  28. @pytest.fixture()
  29. def global_stack():
  30. return MagicMock(name="Global Stack")
  31. @pytest.fixture()
  32. def container_registry(application, global_stack) -> ContainerRegistry:
  33. result = MagicMock()
  34. result.findContainerStacks = MagicMock(return_value = [global_stack])
  35. application.getContainerRegistry = MagicMock(return_value = result)
  36. return result
  37. @pytest.fixture()
  38. def extruder_manager(application, um_application, container_registry) -> ExtruderManager:
  39. if ExtruderManager.getInstance() is not None:
  40. # Reset the data
  41. ExtruderManager._ExtruderManager__instance = None
  42. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=application)):
  43. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  44. with patch("UM.Application.Application.getInstance", um_application):
  45. manager = ExtruderManager()
  46. return manager
  47. @pytest.fixture()
  48. def machine_manager(application, extruder_manager, container_registry, global_stack) -> MachineManager:
  49. application.getExtruderManager = MagicMock(return_value = extruder_manager)
  50. application.getGlobalContainerStack = MagicMock(return_value = global_stack)
  51. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  52. manager = MachineManager(application)
  53. return manager