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