test_failed_rerun_command.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. Unit tests for ly_test_tools._internal.pytest_plugin.failed_test_rerun_command
  6. """
  7. import os
  8. import pytest
  9. import unittest.mock as mock
  10. import ly_test_tools._internal.pytest_plugin.failed_test_rerun_command as failed_test_rerun_command
  11. pytestmark = pytest.mark.SUITE_smoke
  12. class TestRerunCommand(object):
  13. MOCK_FOO_EXE = os.path.join('foo', 'path')
  14. @mock.patch('os.path.join')
  15. @mock.patch('sys.argv', mock.MagicMock())
  16. @mock.patch('os.path.exists', mock.MagicMock(return_value=True))
  17. def test_GetLauncherCommand_PythonScriptFound_CmdReturned(self, mock_join):
  18. mock_python = 'python'
  19. mock_join.return_value = mock_python
  20. expected = f"{mock_python} -m pytest "
  21. under_test = failed_test_rerun_command._get_test_launcher_cmd()
  22. assert under_test == expected
  23. @mock.patch('sys.argv', mock.MagicMock())
  24. @mock.patch('os.path.abspath', mock.MagicMock())
  25. @mock.patch('os.path.join', mock.MagicMock())
  26. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.sys.executable', MOCK_FOO_EXE)
  27. @mock.patch('os.path.exists', mock.MagicMock(return_value=False))
  28. def test_GetLauncherCommand_PythonScriptNotFound_ExeReturned(self):
  29. expected = f"{TestRerunCommand.MOCK_FOO_EXE} -m pytest "
  30. under_test = failed_test_rerun_command._get_test_launcher_cmd()
  31. assert under_test == expected
  32. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.sys.executable', MOCK_FOO_EXE)
  33. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.WINDOWS', True)
  34. @mock.patch('sys.argv', mock.MagicMock())
  35. @mock.patch('os.path.exists', mock.MagicMock(return_value=True))
  36. def test_GetLauncherCommand_WindowsPythonInterpreter_WindowsPythonEntrypointReturned(self):
  37. python_script = 'python.cmd'
  38. expected = f"{os.path.join(TestRerunCommand.MOCK_FOO_EXE, python_script)} -m pytest "
  39. under_test = failed_test_rerun_command._get_test_launcher_cmd()
  40. assert under_test == expected
  41. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.sys.executable', MOCK_FOO_EXE)
  42. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.WINDOWS', False)
  43. @mock.patch('sys.argv', mock.MagicMock())
  44. @mock.patch('os.path.exists', mock.MagicMock(return_value=True))
  45. def test_GetLauncherCommand_NonWindowsPythonInterpreter_NonWindowsPythonEntrypointReturned(self):
  46. python_script = 'python.sh'
  47. expected = f"{os.path.join(TestRerunCommand.MOCK_FOO_EXE, python_script)} -m pytest "
  48. under_test = failed_test_rerun_command._get_test_launcher_cmd()
  49. assert under_test == expected
  50. @mock.patch('sys.argv', ['mock_python_exe', '-mock_arg1', '--mock_arg2', 'mock_arg3', 'mock_python_module'])
  51. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.sys.executable', MOCK_FOO_EXE)
  52. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.WINDOWS', True)
  53. @mock.patch('os.path.exists', mock.MagicMock(return_value=True))
  54. def test_GetLauncherCommand_WindowsPythonInterpreter_SysArgsAppended(self):
  55. under_test = failed_test_rerun_command._get_test_launcher_cmd()
  56. assert 'mock_python_exe' not in under_test
  57. assert 'mock_python_module' not in under_test
  58. assert '-mock_arg1' in under_test
  59. assert '--mock_arg2' in under_test
  60. assert 'mock_arg3' in under_test
  61. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.os.path.abspath')
  62. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.os.path.dirname')
  63. def test_FormatCommand_FileAsPathAndFullNodeId_CorrectCommand(self, mock_dirname, mock_abspath):
  64. launcher_cmd = 'python -m pytest '
  65. test_path = os.path.join('dirA', 'dirB', 'test_stuff.py')
  66. nodeid = 'test_stuff.py::test_Stuff_Something_Else[a]'
  67. mock_dirname.return_value = test_path
  68. mock_abspath.return_value = os.path.join(test_path, nodeid)
  69. expected = f'{launcher_cmd}{os.path.join(test_path, nodeid)}'
  70. actual = failed_test_rerun_command._format_cmd(launcher_cmd, test_path, nodeid)
  71. assert actual == expected
  72. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.os.path.abspath')
  73. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.os.path.dirname')
  74. def test_FormatCommand_FileAsPathAndFileAsNodeId_CorrectCommand(self, mock_dirname, mock_abspath):
  75. launcher_cmd = 'python -m pytest '
  76. test_path = os.path.join('dirA', 'dirB', 'test_stuff.py')
  77. nodeid = 'test_stuff.py'
  78. mock_dirname.return_value = test_path
  79. mock_abspath.return_value = os.path.join(test_path, nodeid)
  80. expected = f'{launcher_cmd}{test_path}'
  81. actual = failed_test_rerun_command._format_cmd(launcher_cmd, test_path, nodeid)
  82. assert actual == expected
  83. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.os.path.abspath')
  84. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.os.path.dirname')
  85. def test_FormatCommand_DirectoryAsPathAndFullNodeId_CorrectCommand(self, mock_dirname, mock_abspath):
  86. launcher_cmd = 'python -m pytest '
  87. test_path = os.path.join('dirA', 'dirB')
  88. nodeid = 'test_Stuff_Something_Else[a]'
  89. mock_dirname.return_value = test_path
  90. mock_abspath.return_value = os.path.join(test_path, os.path.normpath(nodeid))
  91. expected = f'{launcher_cmd}{os.path.join(test_path, nodeid)}'
  92. actual = failed_test_rerun_command._format_cmd(launcher_cmd, test_path, nodeid)
  93. assert actual == expected
  94. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.os.path.abspath')
  95. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.os.path.dirname')
  96. def test_FormatCommand_DirectoryAsPathAndFileAsNodeId_CorrectCommand(self, mock_dirname, mock_abspath):
  97. launcher_cmd = 'python -m pytest '
  98. test_path = os.path.join('dirA', 'dirB')
  99. nodeid = 'test_stuff.py'
  100. mock_dirname.return_value = test_path
  101. mock_abspath.return_value = os.path.join(test_path, os.path.normpath(nodeid))
  102. expected = f'{launcher_cmd}{os.path.join(test_path, nodeid)}'
  103. actual = failed_test_rerun_command._format_cmd(launcher_cmd, test_path, nodeid)
  104. assert actual == expected
  105. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command._get_test_launcher_cmd')
  106. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.os.path.abspath')
  107. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.os.path.dirname')
  108. @mock.patch('os.path.exists', mock.MagicMock(return_value=False))
  109. @mock.patch('ly_test_tools._internal.pytest_plugin.failed_test_rerun_command.os.environ', {})
  110. def test_BuildCommands_TwoNodeIds_CorrectCommands(self, mock_dirname, mock_abspath, mock_get_test_launcher_cmd):
  111. test_path = os.path.join('dirA', 'dirB')
  112. nodeids = ['test_stuff.py', 'test_something::test_Something_Somewhere_Somehow[a]']
  113. python_cmd = 'python -m pytest '
  114. mock_get_test_launcher_cmd.return_value = python_cmd
  115. mock_dirname.side_effect = [test_path, test_path]
  116. mock_abspath.side_effect = [os.path.join(test_path, os.path.normpath(nodeids[0])),
  117. os.path.join(test_path, os.path.normpath(nodeids[1]))]
  118. expected = [f'{python_cmd}{os.path.join(test_path, nodeids[0])}',
  119. f'{python_cmd}{os.path.join(test_path, nodeids[1])}']
  120. actual = failed_test_rerun_command.build_rerun_commands(test_path, nodeids)
  121. assert actual == expected