1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 # Unittest for yardstick.common.utils
16 from yardstick.common import utils
19 class IterSubclassesTestCase(unittest.TestCase):
20 # Disclaimer: this class is a modified copy from
21 # rally/tests/unit/common/plugin/test_discover.py
22 # Copyright 2015: Mirantis Inc.
23 def test_itersubclasses(self):
36 self.assertEqual([B, C, D], list(utils.itersubclasses(A)))
39 class TryAppendModuleTestCase(unittest.TestCase):
41 @mock.patch('yardstick.common.utils.importutils')
42 def test_try_append_module_not_in_modules(self, mock_importutils):
46 utils.try_append_module(name, modules)
47 mock_importutils.import_module.assert_called_with(name)
49 @mock.patch('yardstick.common.utils.importutils')
50 def test_try_append_module_already_in_modules(self, mock_importutils):
54 utils.try_append_module(name, modules)
55 self.assertFalse(mock_importutils.import_module.called)
58 class ImportModulesFromPackageTestCase(unittest.TestCase):
60 @mock.patch('yardstick.common.utils.os.walk')
61 @mock.patch('yardstick.common.utils.try_append_module')
62 def test_import_modules_from_package_no_mod(self, mock_append, mock_walk):
65 mock_walk.return_value = ([
66 ('..' + sep + 'foo', ['bar'], ['__init__.py']),
67 ('..' + sep + 'foo' + sep + 'bar', [], ['baz.txt', 'qux.rst'])
70 utils.import_modules_from_package('foo.bar')
71 self.assertFalse(mock_append.called)
73 @mock.patch('yardstick.common.utils.os.walk')
74 @mock.patch('yardstick.common.utils.importutils')
75 def test_import_modules_from_package(self, mock_importutils, mock_walk):
78 mock_walk.return_value = ([
79 ('foo' + sep + '..' + sep + 'bar', [], ['baz.py'])
82 utils.import_modules_from_package('foo.bar')
83 mock_importutils.import_module.assert_called_with('bar.baz')
89 if __name__ == '__main__':