lundi 30 novembre 2015

Python unittest extension module relative import 'proper' handling

Given a module setup as follows:

myproject
├── MANIFEST.in
├── README.md
├── build
├── dist
├── examples
│   ├── __init__.py
│   ├── mypackage-example.py
│   ├── mypackage-simple-v1.py
│   ├── mypackage-simple-v2-report.py
│   └── mypackage-simple-v2.py
├── mypackage
│   ├── PKG-INFO
│   ├── __init__.py
│   ├── api_methods.py
│   ├── config.py
│   ├── connector.py
│   ├── contrib.py
│   ├── examples
│   │   ├── __init__.py
│   │   ├── mypackage-example.py
│   │   ├── mypackage-simple-v1.py
│   │   ├── mypackage-simple-v2-report.py
│   │   └── mypackage-simple-v2.py
│   ├── qcache
│   │   ├── __init__.py
│   │   └── redis.conf
│   ├── mypackage.egg-info
│   │   ├── PKG-INFO
│   │   ├── PKG-INFO.bak
│   │   ├── PKG-INFO.bak-e
│   │   ├── SOURCES.txt
│   │   ├── dependency_links.txt
│   │   ├── requires.txt
│   │   └── top_level.txt
│   ├── settings.py
│   ├── setup.cfg
│   ├── tests
│   │   └── test_qualys_cache.py
│   ├── util.py
│   └── version.py
└── setup.py

where I want to have tests within mypackage.tests I am having issues with dependent relative imports on the qcache extension module. The module depends on (and must depend on) the parent module. I was attempting to bring in parent module requirements via relative imports as follows:

from .. import api_methods, connect

Which works from the top-level project path, but not from within the module itself. Am I doing this right for pypi standard unit tests? I'm really new to writing pypi eggs so any advice here is appreciated.

Right now I'm running my tests as follows (from the project, not package directory)

python -m unittest mypackage.tests.test_qualys_cache

Aucun commentaire:

Enregistrer un commentaire