Skip to content

Instantly share code, notes, and snippets.

@romainx
Last active January 31, 2022 08:40
Show Gist options
  • Save romainx/d902fae44143c18298ca385ff278bfc8 to your computer and use it in GitHub Desktop.
Save romainx/d902fae44143c18298ca385ff278bfc8 to your computer and use it in GitHub Desktop.
python - test import

Python Test Package

To check if a Python package works as expected one solution is to try to import its main (or top level) modules. It's possible to get the list of all its top level modules from the top_level.txt file and to try to import them.

This file is a list of the top-level module or package names provided by the project, one Python identifier per line.

Here is a test to illustrate this solution.

import pytest
import pkg_resources as pkg
import logging
from pathlib import Path
from importlib import import_module

# What is top_level.txt and when is it created?
# -> https://setuptools.pypa.io/en/latest/deprecated/python_eggs.html#top-level-txt-conflict-management-metadata

# One way of doings
def pkg_names():
    return map(lambda x: x.project_name, pkg.working_set)


@pytest.fixture(scope="session", params=pkg_names())
def pkg_name(request):
    return request.param


def test_import(pkg_name):
    logging.info(f"Trying to import modules for {pkg_name}...")
    metadata_dir = pkg.get_distribution(pkg_name).egg_info
    top_level_path = Path(metadata_dir) / "top_level.txt"
    if not top_level_path.exists():
        pytest.skip(f"Skipping {pkg_name} import no meta information found")
    with open(top_level_path, mode="r") as top_level:
        # instead of readlines to avoid trailing eol
        module_names = top_level.read().splitlines()
    for module_name in module_names:
        if module_name.startswith("_"):
            pytest.skip(f"Skipping internal module import {module_name} for {pkg_name}")
        else:
            logging.info(f"Trying to import -> {module_name}...") <https://stackoverflow.com/q/69468488>

            import_module(module_name)

Run it

pytest -s
# test_python.py::test_import[requests] 
# 2022-01-28 15:54:04 [    INFO] Trying to import modules for requests... (test_python.py:15)
# 2022-01-28 15:54:04 [    INFO] Trying to import -> requests... (test_python.py:21)
# PASSED
# test_python.py::test_import[wheel] 
# 2022-01-28 15:54:04 [    INFO] Trying to import modules for wheel... (test_python.py:15)
# 2022-01-28 15:54:04 [    INFO] Trying to import -> wheel... (test_python.py:21)
# PASSED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment