aboutsummaryrefslogtreecommitdiffstats
path: root/functest/tests/unit/core/test_testcase_base.py
blob: b7c81d87c64c70d4f25e697b69483e3c066c58d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python

# Copyright (c) 2016 Orange and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0

import logging
import mock
import unittest

from functest.core import testcase_base


class TestcaseBaseTesting(unittest.TestCase):

    logging.disable(logging.CRITICAL)

    def setUp(self):
        self.test = testcase_base.TestcaseBase()
        self.test.project = "functest"
        self.test.case_name = "base"
        self.test.start_time = "1"
        self.test.stop_time = "2"
        self.test.criteria = "PASS"
        self.test.details = {"Hello": "World"}

    def test_run_unimplemented(self):
        self.assertEqual(self.test.run(),
                         testcase_base.TestcaseBase.EX_RUN_ERROR)

    @mock.patch('functest.utils.functest_utils.push_results_to_db',
                return_value=False)
    def _test_missing_attribute(self, mock_function):
        self.assertEqual(self.test.push_to_db(),
                         testcase_base.TestcaseBase.EX_PUSH_TO_DB_ERROR)
        mock_function.assert_not_called()

    def test_missing_case_name(self):
        self.test.case_name = None
        self._test_missing_attribute()

    def test_missing_criteria(self):
        self.test.criteria = None
        self._test_missing_attribute()

    def test_missing_start_time(self):
        self.test.start_time = None
        self._test_missing_attribute()

    def test_missing_stop_time(self):
        self.test.stop_time = None
        self._test_missing_attribute()

    @mock.patch('functest.utils.functest_utils.push_results_to_db',
                return_value=True)
    def test_missing_details(self, mock_function):
        self.test.details = None
        self.assertEqual(self.test.push_to_db(),
                         testcase_base.TestcaseBase.EX_OK)
        mock_function.assert_called_once_with(
            self.test.project, self.test.case_name, self.test.start_time,
            self.test.stop_time, self.test.criteria, self.test.details)

    @mock.patch('functest.utils.functest_utils.push_results_to_db',
                return_value=False)
    def test_push_to_db_failed(self, mock_function):
        self.assertEqual(self.test.push_to_db(),
                         testcase_base.TestcaseBase.EX_PUSH_TO_DB_ERROR)
        mock_function.assert_called_once_with(
            self.test.project, self.test.case_name, self.test.start_time,
            self.test.stop_time, self.test.criteria, self.test.details)

    @mock.patch('functest.utils.functest_utils.push_results_to_db',
                return_value=True)
    def test_push_to_db(self, mock_function):
        self.assertEqual(self.test.push_to_db(),
                         testcase_base.TestcaseBase.EX_OK)
        mock_function.assert_called_once_with(
            self.test.project, self.test.case_name, self.test.start_time,
            self.test.stop_time, self.test.criteria, self.test.details)

    def test_check_criteria_missing(self):
        self.test.criteria = None
        self.assertEqual(self.test.check_criteria(),
                         testcase_base.TestcaseBase.EX_TESTCASE_FAILED)

    def test_check_criteria_failed(self):
        self.test.criteria = 'FAILED'
        self.assertEqual(self.test.check_criteria(),
                         testcase_base.TestcaseBase.EX_TESTCASE_FAILED)

    def test_check_criteria_pass(self):
        self.test.criteria = 'PASS'
        self.assertEqual(self.test.check_criteria(),
                         testcase_base.TestcaseBase.EX_OK)


if __name__ == "__main__":
    unittest.main(verbosity=2)