1#!/usr/bin/env vpython3 2 3# Copyright 2021 The Chromium Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7import unittest 8 9from test_results import TestResult 10from test_results import _build_json_data 11 12 13class TestResultTests(unittest.TestCase): 14 def test_equality_and_hashing(self): 15 a1 = TestResult('foo', 'PASS', 'FAIL') 16 a2 = TestResult('foo', 'PASS', 'FAIL') 17 b = TestResult('bar', 'PASS', 'FAIL') 18 c = TestResult('foo', 'FAIL', 'FAIL') 19 d = TestResult('foo', 'PASS', 'PASS') 20 self.assertEqual(a1, a2) 21 self.assertEqual(hash(a1), hash(a2)) 22 self.assertNotEqual(a1, b) 23 self.assertNotEqual(a1, c) 24 self.assertNotEqual(a1, d) 25 26 def test_pass_expected_repr(self): 27 pass_expected_repr = repr(TestResult('foo', 'PASS')) 28 self.assertIn('foo', pass_expected_repr) 29 self.assertIn('PASS', pass_expected_repr) 30 self.assertNotIn('FAIL', pass_expected_repr) 31 self.assertIn('TestResult', pass_expected_repr) 32 33 def test_fail_expected_repr(self): 34 fail_expected_repr = repr(TestResult('foo', 'PASS', 'FAIL')) 35 self.assertIn('foo', fail_expected_repr) 36 self.assertIn('PASS', fail_expected_repr) 37 self.assertIn('FAIL', fail_expected_repr) 38 self.assertIn('TestResult', fail_expected_repr) 39 40 41class BuildJsonDataTests(unittest.TestCase): 42 def test_grouping_of_tests(self): 43 t1 = TestResult('group1//foo', 'PASS') 44 t2 = TestResult('group1//bar', 'FAIL') 45 t3 = TestResult('group2//baz', 'FAIL') 46 actual_result = _build_json_data([t1, t2, t3], 123) 47 # yapf: disable 48 expected_result = { 49 'interrupted': False, 50 'path_delimiter': '//', 51 'seconds_since_epoch': 123, 52 'version': 3, 53 'tests': { 54 'group1': { 55 'foo': { 56 'expected': 'PASS', 57 'actual': 'PASS' 58 }, 59 'bar': { 60 'expected': 'PASS', 61 'actual': 'FAIL' 62 }}, 63 'group2': { 64 'baz': { 65 'expected': 'PASS', 66 'actual': 'FAIL' 67 }}}, 68 'num_failures_by_type': { 69 'PASS': 1, 70 'FAIL': 2 71 } 72 } 73 # yapf: enable 74 self.assertEqual(actual_result, expected_result) 75 76 77if __name__ == '__main__': 78 unittest.main() 79