test_pytest_metrics_xml_to_csv.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 metrics_xml_to_csv.py
  6. """
  7. import unittest
  8. import unittest.mock as mock
  9. import pytest_metrics_xml_to_csv
  10. class TestMetricsXMLtoCSV(unittest.TestCase):
  11. def test_DetermineTestResult_FailedResult_ReturnsCorrectly(self):
  12. mock_node = mock.MagicMock()
  13. mock_node.find.return_value = True
  14. under_test = pytest_metrics_xml_to_csv._determine_test_result(mock_node)
  15. assert under_test == 'failed'
  16. def test_DetermineTestResult_PassedResult_ReturnsCorrectly(self):
  17. mock_node = mock.MagicMock()
  18. mock_node.find.return_value = None
  19. under_test = pytest_metrics_xml_to_csv._determine_test_result(mock_node)
  20. assert under_test == 'passed'
  21. @mock.patch('pytest_metrics_xml_to_csv._determine_test_result')
  22. @mock.patch('ly_test_tools.cli.codeowners_hint.get_codeowners')
  23. @mock.patch('xml.etree.ElementTree.parse')
  24. def test_ParsePytestXMLToCsv_HappyPath_WorksCorrectly(self, mock_parse, mock_get_codeowners, mock_determine_results):
  25. mock_xml = mock.MagicMock()
  26. mock_entry = mock.MagicMock()
  27. mock_entry.attrib = {
  28. 'name': 'mock_test_name',
  29. 'time': 1,
  30. 'file': mock.MagicMock(),
  31. }
  32. mock_testcases = [
  33. mock_entry
  34. ]
  35. mock_xml.findall.return_value = mock_testcases
  36. mock_get_codeowners.return_value = (None, 'mock_codeowner', None)
  37. mock_parse.return_value.getroot.return_value = mock_xml
  38. mock_writer = mock.MagicMock()
  39. mock_xml_path = mock.MagicMock()
  40. mock_determine_results.return_value = 'passed'
  41. pytest_metrics_xml_to_csv.parse_pytest_xmls_to_csv(mock_xml_path, mock_writer)
  42. mock_writer.writerow.assert_called_once_with({
  43. 'test_name': 'mock_test_name',
  44. 'duration_seconds': 1,
  45. 'status': 'passed',
  46. 'sig_owner': 'mock_codeowner'
  47. })