conftest.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. # Prevents error: "PyCapsule_GetPointer called with incorrect name" with conflicting SIP configurations between Arcus and PyQt: Import Arcus and Savitar first!
  7. import Savitar # Dont remove this line
  8. import Arcus # No really. Don't. It needs to be there!
  9. from UM.Qt.QtApplication import QtApplication # QtApplication import is required, even though it isn't used.
  10. # Even though your IDE says these files are not used, don't believe it. It's lying. They need to be there.
  11. from cura.CuraApplication import CuraApplication
  12. from cura.Settings.ExtruderManager import ExtruderManager
  13. from cura.Settings.MachineManager import MachineManager
  14. from cura.UI.MachineActionManager import MachineActionManager
  15. from UM.Settings.ContainerRegistry import ContainerRegistry
  16. # Create a CuraApplication object that will be shared among all tests. It needs to be initialized.
  17. # Since we need to use it more that once, we create the application the first time and use its instance afterwards.
  18. @pytest.fixture()
  19. def application() -> CuraApplication:
  20. app = MagicMock()
  21. return app
  22. # Returns a MachineActionManager instance.
  23. @pytest.fixture()
  24. def machine_action_manager(application) -> MachineActionManager:
  25. return MachineActionManager(application)
  26. @pytest.fixture()
  27. def global_stack():
  28. return MagicMock(name="Global Stack")
  29. @pytest.fixture()
  30. def container_registry(application, global_stack) -> ContainerRegistry:
  31. result = MagicMock()
  32. result.findContainerStacks = MagicMock(return_value = [global_stack])
  33. application.getContainerRegistry = MagicMock(return_value = result)
  34. return result
  35. @pytest.fixture()
  36. def extruder_manager(application, container_registry) -> ExtruderManager:
  37. if ExtruderManager.getInstance() is not None:
  38. # Reset the data
  39. ExtruderManager._ExtruderManager__instance = None
  40. with patch("cura.CuraApplication.CuraApplication.getInstance", MagicMock(return_value=application)):
  41. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  42. manager = ExtruderManager()
  43. return manager
  44. @pytest.fixture()
  45. def machine_manager(application, extruder_manager, container_registry, global_stack) -> MachineManager:
  46. application.getExtruderManager = MagicMock(return_value = extruder_manager)
  47. application.getGlobalContainerStack = MagicMock(return_value = global_stack)
  48. with patch("UM.Settings.ContainerRegistry.ContainerRegistry.getInstance", MagicMock(return_value=container_registry)):
  49. manager = MachineManager(application)
  50. return manager