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