3 # Copyright (c) 2017 Orange and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 """Define the classes required to fully cover robot."""
18 from robot.errors import DataError, RobotError
19 from robot.result import model
20 from robot.utils.robottime import timestamp_to_secs
22 from functest.core import robotframework
24 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
27 class ResultVisitorTesting(unittest.TestCase):
29 """The class testing ResultVisitor."""
30 # pylint: disable=missing-docstring
33 self.visitor = robotframework.ResultVisitor()
36 self.assertFalse(self.visitor.get_data())
39 data = {'name': 'foo',
42 'starttime': "20161216 16:00:00.000",
43 'endtime': "20161216 16:00:01.000",
45 'text': 'Hello, World!',
47 test = model.TestCase(
48 name=data['name'], status=data['status'], message=data['text'],
49 starttime=data['starttime'], endtime=data['endtime'])
50 test.parent = mock.Mock()
51 config = {'name': data['parent'],
52 'criticality.test_is_critical.return_value': data[
54 test.parent.configure_mock(**config)
55 self.visitor.visit_test(test)
56 self.assertEqual(self.visitor.get_data(), [data])
59 class ParseResultTesting(unittest.TestCase):
61 """The class testing RobotFramework.parse_results()."""
62 # pylint: disable=missing-docstring
64 _config = {'name': 'dummy', 'starttime': '20161216 16:00:00.000',
65 'endtime': '20161216 16:00:01.000'}
68 self.test = robotframework.RobotFramework(
69 case_name='robot', project_name='functest')
71 @mock.patch('robot.api.ExecutionResult', side_effect=DataError)
72 def test_raises_exc(self, mock_method):
73 with self.assertRaises(DataError):
74 self.test.parse_results()
75 mock_method.assert_called_once_with(
76 os.path.join(self.test.res_dir, 'output.xml'))
78 def _test_result(self, config, result):
80 suite.configure_mock(**config)
81 with mock.patch('robot.api.ExecutionResult',
82 return_value=mock.Mock(suite=suite)):
83 self.test.parse_results()
84 self.assertEqual(self.test.result, result)
85 self.assertEqual(self.test.start_time,
86 timestamp_to_secs(config['starttime']))
87 self.assertEqual(self.test.stop_time,
88 timestamp_to_secs(config['endtime']))
89 self.assertEqual(self.test.details,
90 {'description': config['name'], 'tests': []})
92 def test_null_passed(self):
93 self._config.update({'statistics.critical.passed': 0,
94 'statistics.critical.total': 20})
95 self._test_result(self._config, 0)
97 def test_no_test(self):
98 self._config.update({'statistics.critical.passed': 20,
99 'statistics.critical.total': 0})
100 self._test_result(self._config, 0)
102 def test_half_success(self):
103 self._config.update({'statistics.critical.passed': 10,
104 'statistics.critical.total': 20})
105 self._test_result(self._config, 50)
107 def test_success(self):
108 self._config.update({'statistics.critical.passed': 20,
109 'statistics.critical.total': 20})
110 self._test_result(self._config, 100)
113 class RunTesting(unittest.TestCase):
115 """The class testing RobotFramework.run()."""
116 # pylint: disable=missing-docstring
122 self.test = robotframework.RobotFramework(
123 case_name='robot', project_name='functest')
125 def test_exc_key_error(self):
126 self.assertEqual(self.test.run(), self.test.EX_RUN_ERROR)
128 @mock.patch('robot.run')
129 def _test_makedirs_exc(self, *args):
130 with mock.patch.object(self.test, 'parse_results') as mock_method:
132 self.test.run(suites=self.suites, variable=self.variable),
133 self.test.EX_RUN_ERROR)
134 args[0].assert_not_called()
135 mock_method.asser_not_called()
137 @mock.patch('os.makedirs', side_effect=Exception)
138 def test_makedirs_exc(self, *args):
139 self._test_makedirs_exc()
140 args[0].assert_called_once_with(self.test.res_dir)
142 @mock.patch('os.makedirs', side_effect=OSError)
143 def test_makedirs_oserror(self, *args):
144 self._test_makedirs_exc()
145 args[0].assert_called_once_with(self.test.res_dir)
147 @mock.patch('robot.run')
148 def _test_makedirs(self, *args):
149 with mock.patch.object(self.test, 'parse_results') as mock_method:
151 self.test.run(suites=self.suites, variable=self.variable),
153 args[0].assert_called_once_with(
154 *self.suites, log='NONE', output=self.test.xml_file,
155 report='NONE', stdout=mock.ANY, variable=self.variable)
156 mock_method.assert_called_once_with()
158 @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
159 def test_makedirs_oserror17(self, *args):
160 self._test_makedirs()
161 args[0].assert_called_once_with(self.test.res_dir)
163 @mock.patch('os.makedirs')
164 def test_makedirs(self, *args):
165 self._test_makedirs()
166 args[0].assert_called_once_with(self.test.res_dir)
168 @mock.patch('robot.run')
169 def _test_parse_results(self, status, *args):
171 self.test.run(suites=self.suites, variable=self.variable), status)
172 args[0].assert_called_once_with(
173 *self.suites, log='NONE', output=self.test.xml_file,
174 report='NONE', stdout=mock.ANY, variable=self.variable)
176 def test_parse_results_exc(self):
177 with mock.patch.object(self.test, 'parse_results',
178 side_effect=Exception) as mock_method:
179 self._test_parse_results(self.test.EX_RUN_ERROR)
180 mock_method.assert_called_once_with()
182 def test_parse_results_robot_error(self):
183 with mock.patch.object(self.test, 'parse_results',
184 side_effect=RobotError('foo')) as mock_method:
185 self._test_parse_results(self.test.EX_RUN_ERROR)
186 mock_method.assert_called_once_with()
189 if __name__ == "__main__":
190 logging.disable(logging.CRITICAL)
191 unittest.main(verbosity=2)