get_test_health_unittest.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. # Copyright 2022 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Tests for the get_test_health script."""
  6. import pathlib
  7. import subprocess
  8. import sys
  9. import tempfile
  10. import unittest
  11. _TOOLS_ANDROID_PATH = pathlib.Path(__file__).resolve(strict=True).parents[1]
  12. if str(_TOOLS_ANDROID_PATH) not in sys.path:
  13. sys.path.append(str(_TOOLS_ANDROID_PATH))
  14. from python_utils import git_metadata_utils
  15. from python_utils import subprocess_utils
  16. _GET_TEST_HEALTH_PATH = (pathlib.Path(__file__).parent /
  17. 'get_test_health.py').resolve(strict=True)
  18. _CHROMIUM_SRC_PATH = git_metadata_utils.get_chromium_src_path()
  19. _TEST_FILES_PATH = (_CHROMIUM_SRC_PATH / 'tools' / 'android' / 'test_health' /
  20. 'testdata' / 'javatests').relative_to(_CHROMIUM_SRC_PATH)
  21. class GetTestHealthTests(unittest.TestCase):
  22. def test_gettesthealth_writes_to_output_file(self):
  23. with tempfile.TemporaryDirectory() as tmpdir:
  24. json_file = pathlib.Path(tmpdir) / 'test_output'
  25. subprocess_utils.run_command([
  26. str(_GET_TEST_HEALTH_PATH), '--output-file',
  27. str(json_file), '--test-dir',
  28. str(_TEST_FILES_PATH)
  29. ])
  30. self.assertTrue(json_file.exists())
  31. with open(json_file) as f:
  32. json_lines = f.readlines()
  33. self.assertGreater(len(json_lines), 0)
  34. def test_gettesthealth_output_file_not_specified(self):
  35. with self.assertRaises(subprocess.CalledProcessError) as error_cm:
  36. subprocess_utils.run_command([
  37. str(_GET_TEST_HEALTH_PATH), '--test-dir',
  38. str(_TEST_FILES_PATH)
  39. ])
  40. self.assertGreater(error_cm.exception.returncode, 0)
  41. self.assertIn('the following arguments are required: -o/--output-file',
  42. error_cm.exception.stderr)
  43. if __name__ == '__main__':
  44. unittest.main()