38e9039b27ef89a814bbda949a668989efc0368f
[functest-xtesting.git] / functest / tests / unit / core / test_robotframework.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Orange and others.
4 #
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
9
10 """Define the classes required to fully cover robot."""
11
12 import errno
13 import logging
14 import os
15 import unittest
16
17 import mock
18 from robot.errors import DataError, RobotError
19 from robot.result import model
20 from robot.utils.robottime import timestamp_to_secs
21
22 from functest.core import robotframework
23
24 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
25
26
27 class ResultVisitorTesting(unittest.TestCase):
28
29     """The class testing ResultVisitor."""
30     # pylint: disable=missing-docstring
31
32     def setUp(self):
33         self.visitor = robotframework.ResultVisitor()
34
35     def test_empty(self):
36         self.assertFalse(self.visitor.get_data())
37
38     def test_ok(self):
39         data = {'name': 'foo',
40                 'parent': 'bar',
41                 'status': 'PASS',
42                 'starttime': "20161216 16:00:00.000",
43                 'endtime': "20161216 16:00:01.000",
44                 'elapsedtime': 1000,
45                 'text': 'Hello, World!',
46                 'critical': True}
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[
53                       'critical']}
54         test.parent.configure_mock(**config)
55         self.visitor.visit_test(test)
56         self.assertEqual(self.visitor.get_data(), [data])
57
58
59 class ParseResultTesting(unittest.TestCase):
60
61     """The class testing RobotFramework.parse_results()."""
62     # pylint: disable=missing-docstring
63
64     _config = {'name': 'dummy', 'starttime': '20161216 16:00:00.000',
65                'endtime': '20161216 16:00:01.000'}
66
67     def setUp(self):
68         self.test = robotframework.RobotFramework(
69             case_name='robot', project_name='functest')
70
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'))
77
78     def _test_result(self, config, result):
79         suite = mock.Mock()
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': []})
91
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)
96
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)
101
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)
106
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)
111
112
113 class RunTesting(unittest.TestCase):
114
115     """The class testing RobotFramework.run()."""
116     # pylint: disable=missing-docstring
117
118     suites = ["foo"]
119     variable = []
120
121     def setUp(self):
122         self.test = robotframework.RobotFramework(
123             case_name='robot', project_name='functest')
124
125     def test_exc_key_error(self):
126         self.assertEqual(self.test.run(), self.test.EX_RUN_ERROR)
127
128     @mock.patch('robot.run')
129     def _test_makedirs_exc(self, *args):
130         with mock.patch.object(self.test, 'parse_results') as mock_method:
131             self.assertEqual(
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()
136
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)
141
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)
146
147     @mock.patch('robot.run')
148     def _test_makedirs(self, *args):
149         with mock.patch.object(self.test, 'parse_results') as mock_method:
150             self.assertEqual(
151                 self.test.run(suites=self.suites, variable=self.variable),
152                 self.test.EX_OK)
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()
157
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)
162
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)
167
168     @mock.patch('robot.run')
169     def _test_parse_results(self, status, *args):
170         self.assertEqual(
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)
175
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()
181
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()
187
188
189 if __name__ == "__main__":
190     logging.disable(logging.CRITICAL)
191     unittest.main(verbosity=2)