Modify TestCase constructor attributes
[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
32     def setUp(self):
33         self.test = testcase.TestCase(case_name=self._case_name,
34                                       project_name=self._project_name)
35         self.test.start_time = "1"
36         self.test.stop_time = "2"
37         self.test.criteria = "PASS"
38         self.test.details = {"Hello": "World"}
39
40     def test_run_unimplemented(self):
41         self.assertEqual(self.test.run(),
42                          testcase.TestCase.EX_RUN_ERROR)
43
44     @mock.patch('functest.utils.functest_utils.push_results_to_db',
45                 return_value=False)
46     def _test_missing_attribute(self, mock_function=None):
47         self.assertEqual(self.test.push_to_db(),
48                          testcase.TestCase.EX_PUSH_TO_DB_ERROR)
49         mock_function.assert_not_called()
50
51     def test_missing_project_name(self):
52         self.test.project_name = None
53         self._test_missing_attribute()
54
55     def test_missing_case_name(self):
56         self.test.case_name = None
57         self._test_missing_attribute()
58
59     def test_missing_criteria(self):
60         self.test.criteria = None
61         self._test_missing_attribute()
62
63     def test_missing_start_time(self):
64         self.test.start_time = None
65         self._test_missing_attribute()
66
67     def test_missing_stop_time(self):
68         self.test.stop_time = None
69         self._test_missing_attribute()
70
71     @mock.patch('functest.utils.functest_utils.push_results_to_db',
72                 return_value=True)
73     def test_missing_details(self, mock_function=None):
74         self.test.details = None
75         self.assertEqual(self.test.push_to_db(),
76                          testcase.TestCase.EX_OK)
77         mock_function.assert_called_once_with(
78             self._project_name, self._case_name, self.test.start_time,
79             self.test.stop_time, self.test.criteria, self.test.details)
80
81     @mock.patch('functest.utils.functest_utils.push_results_to_db',
82                 return_value=False)
83     def test_push_to_db_failed(self, mock_function=None):
84         self.assertEqual(self.test.push_to_db(),
85                          testcase.TestCase.EX_PUSH_TO_DB_ERROR)
86         mock_function.assert_called_once_with(
87             self._project_name, self._case_name, self.test.start_time,
88             self.test.stop_time, self.test.criteria, self.test.details)
89
90     @mock.patch('functest.utils.functest_utils.push_results_to_db',
91                 return_value=True)
92     def test_push_to_db(self, mock_function=None):
93         self.assertEqual(self.test.push_to_db(),
94                          testcase.TestCase.EX_OK)
95         mock_function.assert_called_once_with(
96             self._project_name, self._case_name, self.test.start_time,
97             self.test.stop_time, self.test.criteria, self.test.details)
98
99     def test_check_criteria_missing(self):
100         self.test.criteria = None
101         self.assertEqual(self.test.check_criteria(),
102                          testcase.TestCase.EX_TESTCASE_FAILED)
103
104     def test_check_criteria_failed(self):
105         self.test.criteria = 'FAILED'
106         self.assertEqual(self.test.check_criteria(),
107                          testcase.TestCase.EX_TESTCASE_FAILED)
108
109     def test_check_criteria_pass(self):
110         self.test.criteria = 'PASS'
111         self.assertEqual(self.test.check_criteria(),
112                          testcase.TestCase.EX_OK)
113
114
115 if __name__ == "__main__":
116     unittest.main(verbosity=2)