conftest.py 2.8 KB

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