Merge "Unlink vnf from constants"
[functest.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     variablefile = []
121
122     def setUp(self):
123         self.test = robotframework.RobotFramework(
124             case_name='robot', project_name='functest')
125
126     def test_exc_key_error(self):
127         self.assertEqual(self.test.run(), self.test.EX_RUN_ERROR)
128
129     @mock.patch('robot.run')
130     def _test_makedirs_exc(self, *args):
131         with mock.patch.object(self.test, 'parse_results') as mock_method:
132             self.assertEqual(
133                 self.test.run(
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()
139
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)
144
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)
149
150     @mock.patch('robot.run')
151     def _test_makedirs(self, *args):
152         with mock.patch.object(self.test, 'parse_results') as mock_method:
153             self.assertEqual(
154                 self.test.run(suites=self.suites, variable=self.variable),
155                 self.test.EX_OK)
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()
161
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)
166
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)
171
172     @mock.patch('robot.run')
173     def _test_parse_results(self, status, *args):
174         self.assertEqual(
175             self.test.run(
176                 suites=self.suites, variable=self.variable,
177                 variablefile=self.variablefile),
178             status)
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)
183
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()
189
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()
195
196
197 if __name__ == "__main__":
198     logging.disable(logging.CRITICAL)
199     unittest.main(verbosity=2)