aboutsummaryrefslogtreecommitdiffstats
path: root/xtesting/tests/unit/core/test_unit.py
blob: 8afe0bde5650289f44ba2d9a43951380c3980428 (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
#!/usr/bin/env python

# 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

# pylint: disable=missing-docstring

import logging
import unittest

import mock

from xtesting.core import unit
from xtesting.core import testcase


class PyTestSuiteRunnerTesting(unittest.TestCase):

    def setUp(self):
        self.psrunner = unit.Suite()
        self.psrunner.suite = "foo"

    @mock.patch('unittest.TestLoader')
    def _test_run(self, mock_class=None, result=mock.Mock(),
                  status=testcase.TestCase.EX_OK):
        with mock.patch('xtesting.core.unit.unittest.TextTestRunner.run',
                        return_value=result):
            self.assertEqual(self.psrunner.run(), status)
            mock_class.assert_not_called()

    def test_check_suite_null(self):
        self.assertEqual(unit.Suite().suite, None)
        self.psrunner.suite = None
        self._test_run(result=mock.Mock(),
                       status=testcase.TestCase.EX_RUN_ERROR)

    def test_run_no_ut(self):
        mock_result = mock.Mock(testsRun=0, errors=[], failures=[])
        self._test_run(result=mock_result,
                       status=testcase.TestCase.EX_RUN_ERROR)
        self.assertEqual(self.psrunner.result, 0)
        self.assertEqual(self.psrunner.details,
                         {'errors': 0, 'failures': 0, 'stream': '',
                          'testsRun': 0})
        self.assertEqual(self.psrunner.is_successful(),
                         testcase.TestCase.EX_TESTCASE_FAILED)

    def test_run_result_ko(self):
        self.psrunner.criteria = 100
        mock_result = mock.Mock(testsRun=50, errors=[('test1', 'error_msg1')],
                                failures=[('test2', 'failure_msg1')])
        self._test_run(result=mock_result)
        self.assertEqual(self.psrunner.result, 96)
        self.assertEqual(self.psrunner.details,
                         {'errors': 1, 'failures': 1, 'stream': '',
                          'testsRun': 50})
        self.assertEqual(self.psrunner.is_successful(),
                         testcase.TestCase.EX_TESTCASE_FAILED)

    def test_run_result_ok(self):
        mock_result = mock.Mock(testsRun=50, errors=[],
                                failures=[])
        self._test_run(result=mock_result)
        self.assertEqual(self.psrunner.result, 100)
        self.assertEqual(self.psrunner.details,
                         {'errors': 0, 'failures': 0, 'stream': '',
                          'testsRun': 50})
        self.assertEqual(self.psrunner.is_successful(),
                         testcase.TestCase.EX_OK)

    @mock.patch('unittest.TestLoader')
    def test_run_name_exc(self, mock_class=None):
        mock_obj = mock.Mock(side_effect=ImportError)
        mock_class.side_effect = mock_obj
        self.assertEqual(self.psrunner.run(name='foo'),
                         testcase.TestCase.EX_RUN_ERROR)
        mock_class.assert_called_once_with()
        mock_obj.assert_called_once_with()

    @mock.patch('unittest.TestLoader')
    def test_run_name(self, mock_class=None):
        mock_result = mock.Mock(testsRun=50, errors=[],
                                failures=[])
        mock_obj = mock.Mock()
        mock_class.side_effect = mock_obj
        with mock.patch('xtesting.core.unit.unittest.TextTestRunner.run',
                        return_value=mock_result):
            self.assertEqual(self.psrunner.run(name='foo'),
                             testcase.TestCase.EX_OK)
        mock_class.assert_called_once_with()
        mock_obj.assert_called_once_with()


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