conftest.py 1.54 KB
Newer Older
1
# Copyright 2011-2018 Kwant authors.
2
3
4
#
# This file is part of Kwant.  It is subject to the license terms in the file
# LICENSE.rst found in the top-level directory of this distribution and at
5
# https://kwant-project.org/license.  A list of Kwant authors can be found in
6
# the file AUTHORS.rst at the top-level directory of this distribution and at
7
# https://kwant-project.org/authors.
8
9
10
11
12
13
14
15
16
17
18
19
"""Pytest plugin to ignore packages that have uninstalled dependencies.

This ignores packages on test collection, which is required when the
tests reside in a package that itself requires the dependency to be
installed.
"""

import importlib


# map from subpackage to sequence of dependency module names
subpackage_dependencies = {
20
21
    'kwant/continuum': ['sympy'],
    'kwant/tests/test_qsymm': ['qsymm', 'sympy'],
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
}


# map from subpackage to sequence of dependency modules that are not installed
dependencies_not_installed = {}
for package, dependencies in subpackage_dependencies.items():
    not_installed = []
    for dep in dependencies:
        try:
            importlib.import_module(dep)
        except ImportError:
            not_installed.append(dep)
    if len(not_installed) != 0:
        dependencies_not_installed[package] = not_installed


def pytest_ignore_collect(path, config):
    for subpackage, not_installed in dependencies_not_installed.items():
        if subpackage in path.strpath:
            print('ignoring {} because the following dependencies are not '
                  'installed: {}'.format(subpackage, ', '.join(not_installed)))
            return True