Add unit tests for shell.py

This commit try to add unit tests for shell.py. Also find a bug
in parse_args if not set the config file.

Change-Id: I73a80b238ec95073fc64698655c045fc88ba352a
changes/17/406817/2
Dong Ma 6 years ago
parent a379416145
commit a4ff5c214f

@ -57,9 +57,9 @@ def list_opts():
return [('DEFAULT', copy.deepcopy(SHELL_OPTS))]
def parse_args(argv):
CONF(argv[1:], project='coverage2sql', version=_version_)
CONF(default_config_files=[CONF.config_file])
def parse_args(argv, default_config_files=None):
CONF(argv[1:], project='coverage2sql', version=_version_,
default_config_files=default_config_files)
def process_results(project_name=".", coverage_rate=0.0):

@ -0,0 +1,51 @@
# Copyright (c) 2016 Hewlett Packard Enterprise Development L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import fixtures
import mock
from oslo_config import cfg
import tempfile
from coverage2sql.migrations import cli as migration_cli
from coverage2sql import shell
from coverage2sql.tests import base
class TestMain(base.TestCase):
def setUp(self):
super(TestMain, self).setUp()
cfg.CONF.reset()
cfg.CONF.unregister_opt(migration_cli.command_opt)
self.fake_args = ['coverage2sql', 'test']
self.useFixture(fixtures.MonkeyPatch('sys.argv', self.fake_args))
@mock.patch('coverage2sql.read_coverage.ReadCoverage')
@mock.patch('coverage2sql.shell.process_results')
def test_main(self, process_results_mock, read_coverage_mock):
tfile = tempfile.NamedTemporaryFile()
tfile.write(b'test me later')
tfile.flush()
self.fake_args.extend([tfile.name])
fake_read_coverage = mock.MagicMock('ReadCoverage')
fake_get_coverage_rate = 'fake coverage rate'
fake_read_coverage.get_coverage_rate = mock.MagicMock(
'get_coverage_rate')
fake_read_coverage.get_coverage_rate.return_value = (
fake_get_coverage_rate)
read_coverage_mock.return_value = fake_read_coverage
shell.main()
read_coverage_mock.assert_called_with(mock.ANY)
self.assertEqual(fake_get_coverage_rate,
process_results_mock.call_args_list[0][0][1])
Loading…
Cancel
Save