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
123 self.test = robotframework.RobotFramework(
124 case_name='robot', project_name='functest')
126 def test_exc_key_error(self):
127 self.assertEqual(self.test.run(), self.test.EX_RUN_ERROR)
129 @mock.patch('robot.run')
130 def _test_makedirs_exc(self, *args):
131 with mock.patch.object(self.test, 'parse_results') as mock_method:
134 suites=self.suites, variable=self.variable,
135 variablefile=self.variablefile),
136 self.test.EX_RUN_ERROR)
137 args[0].assert_not_called()
138 mock_method.asser_not_called()
140 @mock.patch('os.makedirs', side_effect=Exception)
141 def test_makedirs_exc(self, *args):
142 self._test_makedirs_exc()
143 args[0].assert_called_once_with(self.test.res_dir)
145 @mock.patch('os.makedirs', side_effect=OSError)
146 def test_makedirs_oserror(self, *args):
147 self._test_makedirs_exc()
148 args[0].assert_called_once_with(self.test.res_dir)
150 @mock.patch('robot.run')
151 def _test_makedirs(self, *args):
152 with mock.patch.object(self.test, 'parse_results') as mock_method:
154 self.test.run(suites=self.suites, variable=self.variable),
156 args[0].assert_called_once_with(
157 *self.suites, log='NONE', output=self.test.xml_file,
158 report='NONE', stdout=mock.ANY, variable=self.variable,
159 variablefile=self.variablefile)
160 mock_method.assert_called_once_with()
162 @mock.patch('os.makedirs', side_effect=OSError(errno.EEXIST, ''))
163 def test_makedirs_oserror17(self, *args):
164 self._test_makedirs()
165 args[0].assert_called_once_with(self.test.res_dir)
167 @mock.patch('os.makedirs')
168 def test_makedirs(self, *args):
169 self._test_makedirs()
170 args[0].assert_called_once_with(self.test.res_dir)
172 @mock.patch('robot.run')
173 def _test_parse_results(self, status, *args):
176 suites=self.suites, variable=self.variable,
177 variablefile=self.variablefile),
179 args[0].assert_called_once_with(
180 *self.suites, log='NONE', output=self.test.xml_file,
181 report='NONE', stdout=mock.ANY, variable=self.variable,
182 variablefile=self.variablefile)
184 def test_parse_results_exc(self):
185 with mock.patch.object(self.test, 'parse_results',
186 side_effect=Exception) as mock_method:
187 self._test_parse_results(self.test.EX_RUN_ERROR)
188 mock_method.assert_called_once_with()
190 def test_parse_results_robot_error(self):
191 with mock.patch.object(self.test, 'parse_results',
192 side_effect=RobotError('foo')) as mock_method:
193 self._test_parse_results(self.test.EX_RUN_ERROR)
194 mock_method.assert_called_once_with()
197 if __name__ == "__main__":
198 logging.disable(logging.CRITICAL)
199 unittest.main(verbosity=2)