File: test_runner_test.py

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (138 lines) | stat: -rwxr-xr-x 5,974 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env vpython3
#
# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest

from unittest import mock

import test_runner


class UploadTestScriptRecordsTest(unittest.TestCase):

  def setUp(self):
    self.sink_client = mock.MagicMock()
    self.exc_recorder = mock.MagicMock()
    self.mm_recorder = mock.MagicMock()

  def testNoRecords(self):
    self.exc_recorder.size.return_value = 0
    self.mm_recorder.size.return_value = 0
    test_runner.UploadTestScriptRecords(self.sink_client, self.exc_recorder,
                                        self.mm_recorder)
    self.exc_recorder.to_dict.assert_not_called()
    self.mm_recorder.to_dict.assert_not_called()
    self.sink_client.UpdateInvocationExtendedProperties.assert_not_called()

  def testUploadSuccess(self):
    test_runner.UploadTestScriptRecords(self.sink_client, self.exc_recorder,
                                        self.mm_recorder)
    self.exc_recorder.to_dict.assert_called_once()
    self.mm_recorder.to_dict.assert_called_once()
    self.sink_client.UpdateInvocationExtendedProperties.assert_called_once()
    self.exc_recorder.clear.assert_called_once()
    self.mm_recorder.clear.assert_called_once()

  def testUploadSuccessWithClearStacktrace(self):
    self.sink_client.UpdateInvocationExtendedProperties.side_effect = [
        Exception("Error 1"), None
    ]
    test_runner.UploadTestScriptRecords(self.sink_client, self.exc_recorder,
                                        self.mm_recorder)
    self.assertEqual(self.exc_recorder.to_dict.call_count, 2)
    self.assertEqual(self.mm_recorder.to_dict.call_count, 2)
    self.assertEqual(
        self.sink_client.UpdateInvocationExtendedProperties.call_count, 2)
    self.exc_recorder.clear_stacktrace.assert_called_once()
    self.exc_recorder.clear.assert_called_once()
    self.mm_recorder.clear.assert_called_once()

  def testUploadSuccessWithClearRecords(self):
    self.sink_client.UpdateInvocationExtendedProperties.side_effect = [
        Exception("Error 1"), Exception("Error 2"), None
    ]
    test_runner.UploadTestScriptRecords(self.sink_client, self.exc_recorder,
                                        self.mm_recorder)
    self.assertEqual(self.exc_recorder.to_dict.call_count, 3)
    self.assertEqual(self.mm_recorder.to_dict.call_count, 3)
    self.assertEqual(
        self.sink_client.UpdateInvocationExtendedProperties.call_count, 3)
    self.exc_recorder.clear_stacktrace.assert_called_once()
    self.assertEqual(self.exc_recorder.clear.call_count, 2)
    self.exc_recorder.register.assert_called_once()
    self.mm_recorder.clear.assert_called_once()

  def testUploadFailure(self):
    self.sink_client.UpdateInvocationExtendedProperties.side_effect = (
        Exception("Error"))
    test_runner.UploadTestScriptRecords(self.sink_client, self.exc_recorder,
                                        self.mm_recorder)
    self.assertEqual(self.exc_recorder.to_dict.call_count, 3)
    self.assertEqual(self.mm_recorder.to_dict.call_count, 3)
    self.assertEqual(
        self.sink_client.UpdateInvocationExtendedProperties.call_count, 3)
    self.assertEqual(self.exc_recorder.clear.call_count, 2)
    self.exc_recorder.clear_stacktrace.assert_called_once()
    self.exc_recorder.register.assert_called_once()
    self.mm_recorder.clear.assert_called_once()


class TestRunnerHelperTest(unittest.TestCase):

  def testCreateStructuredTestDict(self):
    # pylint: disable=protected-access
    t_instance_mock = mock.MagicMock()
    t_result_mock = mock.MagicMock()
    test_id = 'foo.bar.class#test1[28]'
    t_result_mock.GetNameForResultSink.return_value = test_id
    t_instance_mock.suite = 'foo_suite'

    # junit tests
    t_instance_mock.TestType.return_value = 'junit'
    test_dict = test_runner._CreateStructuredTestDict(t_instance_mock,
                                                      t_result_mock)
    self.assertEqual(test_dict['coarseName'], 'foo.bar')
    self.assertEqual(test_dict['fineName'], 'class')
    self.assertTrue('test1[28]' in test_dict['caseNameComponents'])

    # instrumentation tests
    t_instance_mock.TestType.return_value = 'instrumentation'
    test_dict = test_runner._CreateStructuredTestDict(t_instance_mock,
                                                      t_result_mock)
    self.assertEqual(test_dict['coarseName'], 'foo.bar')
    self.assertEqual(test_dict['fineName'], 'class')
    self.assertTrue('test1[28]' in test_dict['caseNameComponents'])

    # Can't be parsed as an instrumentation test as it's missing the #.
    test_id = 'foo.bar.class.test1[28]'
    t_result_mock.GetNameForResultSink.return_value = test_id
    test_dict = test_runner._CreateStructuredTestDict(t_instance_mock,
                                                      t_result_mock)
    self.assertIsNone(test_dict)

    test_id = 'foo.bar.class$parameter#test1[28]'
    t_result_mock.GetNameForResultSink.return_value = test_id
    test_dict = test_runner._CreateStructuredTestDict(t_instance_mock,
                                                      t_result_mock)
    self.assertEqual(test_dict['coarseName'], 'foo.bar')
    self.assertEqual(test_dict['fineName'], 'class$parameter')
    self.assertTrue('test1[28]' in test_dict['caseNameComponents'])

    # gtest
    t_instance_mock.TestType.return_value = 'gtest'
    test_id = 'foo.bar.class.test1[28]'
    t_result_mock.GetNameForResultSink.return_value = test_id
    test_dict = test_runner._CreateStructuredTestDict(t_instance_mock,
                                                      t_result_mock)
    self.assertIsNone(test_dict['coarseName'])
    self.assertEqual(test_dict['fineName'], 'foo_suite')
    self.assertTrue(test_id in test_dict['caseNameComponents'])
    # pylint: disable=protected-access


if __name__ == '__main__':
  # Suppress logging messages.
  unittest.main(buffer=True)