Modify how to disable logging in unit test.
[functest.git] / functest / tests / unit / cli / commands / test_cli_testcase.py
1 #!/usr/bin/env python
2
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7
8
9 import logging
10 import unittest
11
12 import mock
13
14 from functest.cli.commands import cli_testcase
15 from functest.utils.constants import CONST
16
17
18 class CliTestCasesTesting(unittest.TestCase):
19
20     def setUp(self):
21         self.testname = 'testname'
22         with mock.patch('functest.cli.commands.cli_testcase.tb'):
23             self.cli_tests = cli_testcase.CliTestcase()
24
25     @mock.patch('functest.cli.commands.cli_testcase.vacation.main')
26     def test_run_vacation(self, mock_method):
27         self.cli_tests.run('vacation')
28         self.assertTrue(mock_method.called)
29
30     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
31                 return_value=False)
32     @mock.patch('functest.cli.commands.cli_testcase.click.echo')
33     def test_run_missing_env_file(self, mock_click_echo, mock_os):
34         self.cli_tests.run(self.testname)
35         mock_click_echo.assert_called_with("Functest environment is not ready."
36                                            " Run first 'functest env prepare'")
37
38     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
39                 return_value=True)
40     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
41     def test_run_default(self, mock_ft_utils, mock_os):
42         cmd = ("python %s/functest/ci/run_tests.py "
43                "%s -t %s" % (CONST.dir_repo_functest, "-n -r ", self.testname))
44         self.cli_tests.run(self.testname, noclean=True, report=True)
45         mock_ft_utils.assert_called_with(cmd)
46
47     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
48                 return_value=True)
49     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
50     def test_run_noclean_missing_report(self, mock_ft_utils, mock_os):
51         cmd = ("python %s/functest/ci/run_tests.py "
52                "%s -t %s" % (CONST.dir_repo_functest, "-n ", self.testname))
53         self.cli_tests.run(self.testname, noclean=True, report=False)
54         mock_ft_utils.assert_called_with(cmd)
55
56     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
57                 return_value=True)
58     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
59     def test_run_report_missing_noclean(self, mock_ft_utils, mock_os):
60         cmd = ("python %s/functest/ci/run_tests.py "
61                "%s -t %s" % (CONST.dir_repo_functest, "-r ", self.testname))
62         self.cli_tests.run(self.testname, noclean=False, report=True)
63         mock_ft_utils.assert_called_with(cmd)
64
65     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
66                 return_value=True)
67     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
68     def test_run_missing_noclean_report(self, mock_ft_utils, mock_os):
69         cmd = ("python %s/functest/ci/run_tests.py "
70                "%s -t %s" % (CONST.dir_repo_functest, "", self.testname))
71         self.cli_tests.run(self.testname, noclean=False, report=False)
72         mock_ft_utils.assert_called_with(cmd)
73
74     @mock.patch('functest.cli.commands.cli_testcase.click.echo')
75     def test_list(self, mock_click_echo):
76         with mock.patch.object(self.cli_tests.tiers, 'get_tiers',
77                                return_value=[]):
78             self.cli_tests.list()
79             mock_click_echo.assert_called_with("")
80
81     @mock.patch('functest.cli.commands.cli_testcase.click.echo')
82     def test_show_default_desc_none(self, mock_click_echo):
83         with mock.patch.object(self.cli_tests.tiers, 'get_test',
84                                return_value=None):
85             self.cli_tests.show(self.testname)
86             mock_click_echo.assert_any_call("The test case '%s' "
87                                             "does not exist or is"
88                                             " not supported."
89                                             % self.testname)
90
91     @mock.patch('functest.cli.commands.cli_testcase.click.echo')
92     def test_show_default(self, mock_click_echo):
93         mock_obj = mock.Mock()
94         with mock.patch.object(self.cli_tests.tiers, 'get_test',
95                                return_value=mock_obj):
96             self.cli_tests.show(self.testname)
97             mock_click_echo.assert_called_with(mock_obj)
98
99
100 if __name__ == "__main__":
101     logging.disable(logging.CRITICAL)
102     unittest.main(verbosity=2)