test_results_unittests.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env vpython3
  2. # Copyright 2021 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. import unittest
  6. from test_results import TestResult
  7. from test_results import _build_json_data
  8. class TestResultTests(unittest.TestCase):
  9. def test_equality_and_hashing(self):
  10. a1 = TestResult('foo', 'PASS', 'FAIL')
  11. a2 = TestResult('foo', 'PASS', 'FAIL')
  12. b = TestResult('bar', 'PASS', 'FAIL')
  13. c = TestResult('foo', 'FAIL', 'FAIL')
  14. d = TestResult('foo', 'PASS', 'PASS')
  15. self.assertEqual(a1, a2)
  16. self.assertEqual(hash(a1), hash(a2))
  17. self.assertNotEqual(a1, b)
  18. self.assertNotEqual(a1, c)
  19. self.assertNotEqual(a1, d)
  20. def test_pass_expected_repr(self):
  21. pass_expected_repr = repr(TestResult('foo', 'PASS'))
  22. self.assertIn('foo', pass_expected_repr)
  23. self.assertIn('PASS', pass_expected_repr)
  24. self.assertNotIn('FAIL', pass_expected_repr)
  25. self.assertIn('TestResult', pass_expected_repr)
  26. def test_fail_expected_repr(self):
  27. fail_expected_repr = repr(TestResult('foo', 'PASS', 'FAIL'))
  28. self.assertIn('foo', fail_expected_repr)
  29. self.assertIn('PASS', fail_expected_repr)
  30. self.assertIn('FAIL', fail_expected_repr)
  31. self.assertIn('TestResult', fail_expected_repr)
  32. class BuildJsonDataTests(unittest.TestCase):
  33. def test_grouping_of_tests(self):
  34. t1 = TestResult('group1//foo', 'PASS')
  35. t2 = TestResult('group1//bar', 'FAIL')
  36. t3 = TestResult('group2//baz', 'FAIL')
  37. actual_result = _build_json_data([t1, t2, t3], 123)
  38. # yapf: disable
  39. expected_result = {
  40. 'interrupted': False,
  41. 'path_delimiter': '//',
  42. 'seconds_since_epoch': 123,
  43. 'version': 3,
  44. 'tests': {
  45. 'group1': {
  46. 'foo': {
  47. 'expected': 'PASS',
  48. 'actual': 'PASS'
  49. },
  50. 'bar': {
  51. 'expected': 'PASS',
  52. 'actual': 'FAIL'
  53. }},
  54. 'group2': {
  55. 'baz': {
  56. 'expected': 'PASS',
  57. 'actual': 'FAIL'
  58. }}},
  59. 'num_failures_by_type': {
  60. 'PASS': 1,
  61. 'FAIL': 2
  62. }
  63. }
  64. # yapf: enable
  65. self.assertEqual(actual_result, expected_result)
  66. if __name__ == '__main__':
  67. unittest.main()