Merge "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" %
44                (CONST.__getattribute__('dir_repo_functest'),
45                 "-n -r ", self.testname))
46         self.cli_tests.run(self.testname, noclean=True, report=True)
47         mock_ft_utils.assert_called_with(cmd)
48
49     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
50                 return_value=True)
51     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
52     def test_run_noclean_missing_report(self, mock_ft_utils, mock_os):
53         cmd = ("python %s/functest/ci/run_tests.py "
54                "%s -t %s" %
55                (CONST.__getattribute__('dir_repo_functest'),
56                 "-n ", self.testname))
57         self.cli_tests.run(self.testname, noclean=True, report=False)
58         mock_ft_utils.assert_called_with(cmd)
59
60     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
61                 return_value=True)
62     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
63     def test_run_report_missing_noclean(self, mock_ft_utils, mock_os):
64         cmd = ("python %s/functest/ci/run_tests.py "
65                "%s -t %s" %
66                (CONST.__getattribute__('dir_repo_functest'),
67                 "-r ", self.testname))
68         self.cli_tests.run(self.testname, noclean=False, report=True)
69         mock_ft_utils.assert_called_with(cmd)
70
71     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
72                 return_value=True)
73     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
74     def test_run_missing_noclean_report(self, mock_ft_utils, mock_os):
75         cmd = ("python %s/functest/ci/run_tests.py "
76                "%s -t %s" %
77                (CONST.__getattribute__('dir_repo_functest'),
78                 "", self.testname))
79         self.cli_tests.run(self.testname, noclean=False, report=False)
80         mock_ft_utils.assert_called_with(cmd)
81
82     @mock.patch('functest.cli.commands.cli_testcase.click.echo')
83     def test_list(self, mock_click_echo):
84         with mock.patch.object(self.cli_tests.tiers, 'get_tiers',
85                                return_value=[]):
86             self.cli_tests.list()
87             mock_click_echo.assert_called_with("")
88
89     @mock.patch('functest.cli.commands.cli_testcase.click.echo')
90     def test_show_default_desc_none(self, mock_click_echo):
91         with mock.patch.object(self.cli_tests.tiers, 'get_test',
92                                return_value=None):
93             self.cli_tests.show(self.testname)
94             mock_click_echo.assert_any_call("The test case '%s' "
95                                             "does not exist or is"
96                                             " not supported."
97                                             % self.testname)
98
99     @mock.patch('functest.cli.commands.cli_testcase.click.echo')
100     def test_show_default(self, mock_click_echo):
101         mock_obj = mock.Mock()
102         with mock.patch.object(self.cli_tests.tiers, 'get_test',
103                                return_value=mock_obj):
104             self.cli_tests.show(self.testname)
105             mock_click_echo.assert_called_with(mock_obj)
106
107
108 if __name__ == "__main__":
109     logging.disable(logging.CRITICAL)
110     unittest.main(verbosity=2)