Rename test_testcase_base to test_testcase
[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 import logging
11 import unittest
12
13 import mock
14
15 from functest.core import testcase
16
17
18 class TestCaseTesting(unittest.TestCase):
19
20     logging.disable(logging.CRITICAL)
21
22     def setUp(self):
23         self.test = testcase.TestCase()
24         self.test.project = "functest"
25         self.test.case_name = "base"
26         self.test.start_time = "1"
27         self.test.stop_time = "2"
28         self.test.criteria = "PASS"
29         self.test.details = {"Hello": "World"}
30
31     def test_run_unimplemented(self):
32         self.assertEqual(self.test.run(),
33                          testcase.TestCase.EX_RUN_ERROR)
34
35     @mock.patch('functest.utils.functest_utils.push_results_to_db',
36                 return_value=False)
37     def _test_missing_attribute(self, mock_function=None):
38         self.assertEqual(self.test.push_to_db(),
39                          testcase.TestCase.EX_PUSH_TO_DB_ERROR)
40         mock_function.assert_not_called()
41
42     def test_missing_case_name(self):
43         self.test.case_name = None
44         self._test_missing_attribute()
45
46     def test_missing_criteria(self):
47         self.test.criteria = None
48         self._test_missing_attribute()
49
50     def test_missing_start_time(self):
51         self.test.start_time = None
52         self._test_missing_attribute()
53
54     def test_missing_stop_time(self):
55         self.test.stop_time = None
56         self._test_missing_attribute()
57
58     @mock.patch('functest.utils.functest_utils.push_results_to_db',
59                 return_value=True)
60     def test_missing_details(self, mock_function=None):
61         self.test.details = None
62         self.assertEqual(self.test.push_to_db(),
63                          testcase.TestCase.EX_OK)
64         mock_function.assert_called_once_with(
65             self.test.project, self.test.case_name, self.test.start_time,
66             self.test.stop_time, self.test.criteria, self.test.details)
67
68     @mock.patch('functest.utils.functest_utils.push_results_to_db',
69                 return_value=False)
70     def test_push_to_db_failed(self, mock_function=None):
71         self.assertEqual(self.test.push_to_db(),
72                          testcase.TestCase.EX_PUSH_TO_DB_ERROR)
73         mock_function.assert_called_once_with(
74             self.test.project, self.test.case_name, self.test.start_time,
75             self.test.stop_time, self.test.criteria, self.test.details)
76
77     @mock.patch('functest.utils.functest_utils.push_results_to_db',
78                 return_value=True)
79     def test_push_to_db(self, mock_function=None):
80         self.assertEqual(self.test.push_to_db(),
81                          testcase.TestCase.EX_OK)
82         mock_function.assert_called_once_with(
83             self.test.project, self.test.case_name, self.test.start_time,
84             self.test.stop_time, self.test.criteria, self.test.details)
85
86     def test_check_criteria_missing(self):
87         self.test.criteria = None
88         self.assertEqual(self.test.check_criteria(),
89                          testcase.TestCase.EX_TESTCASE_FAILED)
90
91     def test_check_criteria_failed(self):
92         self.test.criteria = 'FAILED'
93         self.assertEqual(self.test.check_criteria(),
94                          testcase.TestCase.EX_TESTCASE_FAILED)
95
96     def test_check_criteria_pass(self):
97         self.test.criteria = 'PASS'
98         self.assertEqual(self.test.check_criteria(),
99                          testcase.TestCase.EX_OK)
100
101
102 if __name__ == "__main__":
103     unittest.main(verbosity=2)