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