forked from biotite-dev/biotite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_modname.py
54 lines (49 loc) · 1.72 KB
/
test_modname.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# This source code is part of the Biotite package and is distributed
# under the 3-Clause BSD License. Please see 'LICENSE.rst' for further
# information.
import pkgutil
from os.path import dirname, join, isdir, splitext
import importlib
import pytest
from .util import cannot_import
def find_all_modules(package_name, src_dir):
"""
Recursively look for Python modules in the given source directory.
(Sub-)Packages are not considered as modules.
"""
module_names = []
for _, module_name, is_package in pkgutil.iter_modules([src_dir]):
full_module_name = f"{package_name}.{module_name}"
if is_package:
module_names.extend(find_all_modules(
full_module_name,
join(src_dir, module_name)
))
else:
module_names.append(full_module_name)
return module_names
@pytest.mark.skipif(
cannot_import("matplotlib") | cannot_import("mdtraj"),
reason="Optional dependencies are not met"
)
@pytest.mark.parametrize(
"module_name",
find_all_modules(
"biotite",
join(dirname(dirname(__file__)), "src", "biotite")
)
)
def test_module_name(module_name):
"""
Test whether the '__name__' attribute of each module in Biotite is
set to the name of the subpackage.
This needs to be tested, since by default the '__name__' attribute
is equal to the name of the module.
For example, we expect 'biotite.structure' instead of
'biotite.structure.atoms'.
"""
# Remove the part after the last '.' of the module name
# to obtain the package name
package_name = ".".join(module_name.split(".")[:-1])
module = importlib.import_module(module_name)
assert module.__name__ == package_name