Merge "rename tempest.conf to refstack_tempest.conf"
[functest.git] / functest / tests / unit / core / test_testcase.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 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 classe required to fully cover testcase."""
11
12 import logging
13 import unittest
14
15 import mock
16
17 from functest.core import testcase
18
19 __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>"
20
21
22 class TestCaseTesting(unittest.TestCase):
23
24     """The class testing TestCase."""
25     # pylint: disable=missing-docstring
26
27     logging.disable(logging.CRITICAL)
28
29     _case_name = "base"
30     _project_name = "functest"
31     _published_result = "PASS"
32
33     def setUp(self):
34         self.test = testcase.TestCase(case_name=self._case_name,
35                                       project_name=self._project_name)
36         self.test.start_time = "1"
37         self.test.stop_time = "2"
38         self.test.result = 100
39         self.test.details = {"Hello": "World"}
40
41     def test_run_unimplemented(self):
42         self.assertEqual(self.test.run(),
43                          testcase.TestCase.EX_RUN_ERROR)
44
45     @mock.patch('functest.utils.functest_utils.push_results_to_db',
46                 return_value=False)
47     def _test_missing_attribute(self, mock_function=None):
48         self.assertEqual(self.test.push_to_db(),
49                          testcase.TestCase.EX_PUSH_TO_DB_ERROR)
50         mock_function.assert_not_called()
51
52     def test_missing_project_name(self):
53         self.test.project_name = None
54         self._test_missing_attribute()
55
56     def test_missing_case_name(self):
57         self.test.case_name = None
58         self._test_missing_attribute()
59
60     def test_missing_start_time(self):
61         self.test.start_time = None
62         self._test_missing_attribute()
63
64     def test_missing_stop_time(self):
65         self.test.stop_time = None
66         self._test_missing_attribute()
67
68     @mock.patch('functest.utils.functest_utils.push_results_to_db',
69                 return_value=True)
70     def test_missing_details(self, mock_function=None):
71         self.test.details = None
72         self.assertEqual(self.test.push_to_db(),
73                          testcase.TestCase.EX_OK)
74         mock_function.assert_called_once_with(
75             self._project_name, self._case_name, self.test.start_time,
76             self.test.stop_time, self._published_result, self.test.details)
77
78     @mock.patch('functest.utils.functest_utils.push_results_to_db',
79                 return_value=False)
80     def test_push_to_db_failed(self, mock_function=None):
81         self.assertEqual(self.test.push_to_db(),
82                          testcase.TestCase.EX_PUSH_TO_DB_ERROR)
83         mock_function.assert_called_once_with(
84             self._project_name, self._case_name, self.test.start_time,
85             self.test.stop_time, self._published_result, self.test.details)
86
87     @mock.patch('functest.utils.functest_utils.push_results_to_db',
88                 return_value=True)
89     def test_push_to_db(self, mock_function=None):
90         self.assertEqual(self.test.push_to_db(),
91                          testcase.TestCase.EX_OK)
92         mock_function.assert_called_once_with(
93             self._project_name, self._case_name, self.test.start_time,
94             self.test.stop_time, self._published_result, self.test.details)
95
96     @mock.patch('functest.utils.functest_utils.push_results_to_db',
97                 return_value=True)
98     def test_push_to_db_res_ko(self, mock_function=None):
99         self.test.result = 0
100         self.assertEqual(self.test.push_to_db(),
101                          testcase.TestCase.EX_OK)
102         mock_function.assert_called_once_with(
103             self._project_name, self._case_name, self.test.start_time,
104             self.test.stop_time, 'FAIL', self.test.details)
105
106     @mock.patch('functest.utils.functest_utils.push_results_to_db',
107                 return_value=True)
108     def test_push_to_db_both_ko(self, mock_function=None):
109         self.test.result = 0
110         self.test.criteria = 0
111         self.assertEqual(self.test.push_to_db(),
112                          testcase.TestCase.EX_OK)
113         mock_function.assert_called_once_with(
114             self._project_name, self._case_name, self.test.start_time,
115             self.test.stop_time, 'FAIL', self.test.details)
116
117     def test_check_criteria_missing(self):
118         self.test.criteria = None
119         self.assertEqual(self.test.check_result(),
120                          testcase.TestCase.EX_TESTCASE_FAILED)
121
122     def test_check_result_missing(self):
123         self.test.result = None
124         self.assertEqual(self.test.check_result(),
125                          testcase.TestCase.EX_TESTCASE_FAILED)
126
127     def test_check_result_failed(self):
128         # Backward compatibility
129         # It must be removed as soon as TestCase subclasses
130         # stop setting result = 'PASS' or 'FAIL'.
131         self.test.result = 'FAIL'
132         self.assertEqual(self.test.check_result(),
133                          testcase.TestCase.EX_TESTCASE_FAILED)
134
135     def test_check_result_pass(self):
136         # Backward compatibility
137         # It must be removed as soon as TestCase subclasses
138         # stop setting result = 'PASS' or 'FAIL'.
139         self.test.result = 'PASS'
140         self.assertEqual(self.test.check_result(),
141                          testcase.TestCase.EX_OK)
142
143     def test_check_result_lt(self):
144         self.test.result = 50
145         self.assertEqual(self.test.check_result(),
146                          testcase.TestCase.EX_TESTCASE_FAILED)
147
148     def test_check_result_eq(self):
149         self.test.result = 100
150         self.assertEqual(self.test.check_result(),
151                          testcase.TestCase.EX_OK)
152
153     def test_check_result_gt(self):
154         self.test.criteria = 50
155         self.test.result = 100
156         self.assertEqual(self.test.check_result(),
157                          testcase.TestCase.EX_OK)
158
159     def test_check_result_zero(self):
160         self.test.criteria = 0
161         self.test.result = 0
162         self.assertEqual(self.test.check_result(),
163                          testcase.TestCase.EX_TESTCASE_FAILED)
164
165
166 if __name__ == "__main__":
167     unittest.main(verbosity=2)