Merge "Tackerclient added in test-requirement."
[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     logging.disable(logging.CRITICAL)
21
22     def setUp(self):
23         self.testname = 'testname'
24         with mock.patch('functest.cli.commands.cli_testcase.tb'):
25             self.cli_tests = cli_testcase.CliTestcase()
26
27     @mock.patch('functest.cli.commands.cli_testcase.vacation.main')
28     def test_run_vacation(self, mock_method):
29         self.cli_tests.run('vacation')
30         self.assertTrue(mock_method.called)
31
32     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
33                 return_value=False)
34     @mock.patch('functest.cli.commands.cli_testcase.click.echo')
35     def test_run_missing_env_file(self, mock_click_echo, mock_os):
36         self.cli_tests.run(self.testname)
37         mock_click_echo.assert_called_with("Functest environment is not ready."
38                                            " Run first 'functest env prepare'")
39
40     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
41                 return_value=True)
42     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
43     def test_run_default(self, mock_ft_utils, mock_os):
44         cmd = ("python %s/functest/ci/run_tests.py "
45                "%s -t %s" % (CONST.dir_repo_functest, "-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" % (CONST.dir_repo_functest, "-n ", self.testname))
55         self.cli_tests.run(self.testname, noclean=True, report=False)
56         mock_ft_utils.assert_called_with(cmd)
57
58     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
59                 return_value=True)
60     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
61     def test_run_report_missing_noclean(self, mock_ft_utils, mock_os):
62         cmd = ("python %s/functest/ci/run_tests.py "
63                "%s -t %s" % (CONST.dir_repo_functest, "-r ", self.testname))
64         self.cli_tests.run(self.testname, noclean=False, report=True)
65         mock_ft_utils.assert_called_with(cmd)
66
67     @mock.patch('functest.cli.commands.cli_testcase.os.path.isfile',
68                 return_value=True)
69     @mock.patch('functest.cli.commands.cli_testcase.ft_utils.execute_command')
70     def test_run_missing_noclean_report(self, mock_ft_utils, mock_os):
71         cmd = ("python %s/functest/ci/run_tests.py "
72                "%s -t %s" % (CONST.dir_repo_functest, "", self.testname))
73         self.cli_tests.run(self.testname, noclean=False, report=False)
74         mock_ft_utils.assert_called_with(cmd)
75
76     @mock.patch('functest.cli.commands.cli_testcase.click.echo')
77     def test_list(self, mock_click_echo):
78         with mock.patch.object(self.cli_tests.tiers, 'get_tiers',
79                                return_value=[]):
80             self.cli_tests.list()
81             mock_click_echo.assert_called_with("")
82
83     @mock.patch('functest.cli.commands.cli_testcase.click.echo')
84     def test_show_default_desc_none(self, mock_click_echo):
85         with mock.patch.object(self.cli_tests.tiers, 'get_test',
86                                return_value=None):
87             self.cli_tests.show(self.testname)
88             mock_click_echo.assert_any_call("The test case '%s' "
89                                             "does not exist or is"
90                                             " not supported."
91                                             % self.testname)
92
93     @mock.patch('functest.cli.commands.cli_testcase.click.echo')
94     def test_show_default(self, mock_click_echo):
95         mock_obj = mock.Mock()
96         with mock.patch.object(self.cli_tests.tiers, 'get_test',
97                                return_value=mock_obj):
98             self.cli_tests.show(self.testname)
99             mock_click_echo.assert_called_with(mock_obj)
100
101
102 if __name__ == "__main__":
103     unittest.main(verbosity=2)