简体   繁体   English

Python 运行测试时导入 src 模块

[英]Python import src modules when running tests

My source files are located under src and my test files are located under tests.我的源文件位于 src 下,我的测试文件位于 tests 下。 When I want to run a test file, say python myTest.py, I get an import error: "No module named ASourceModule.py".当我想运行一个测试文件时,比如 python myTest.py,我收到一个导入错误:“No module named ASourceModule.py”。

How do I import all the modules from source needed to run my tests?如何从运行测试所需的源代码导入所有模块?

You need to add that directory to the path:您需要将该目录添加到路径中:

import sys
sys.path.append('../src')

Maybe put this into a module if you are using it a lot.如果你经常使用它,也许可以把它放到一个模块中。

If you don't want to add the source path to each test file or change your PYTHONPATH , you can use nose to run the tests.如果您不想将源路径添加到每个测试文件或更改您的PYTHONPATH ,您可以使用nose来运行测试。

Suppose your directory structure is like this:假设你的目录结构是这样的:

project
    package
        __init__.py
        module.py
    tests
        __init__.py
        test_module.py

You should import the module normally in the test_module.py (eg from package import module ).您应该在test_module.py正常from package import module (例如from package import module )。 Then run the tests by running nosetests in the project folder.然后通过在项目文件夹中运行nosetests测试来运行测试。 You can also run specific tests by doing nosetests tests/test_module.py .您还可以通过执行nosetests tests/test_module.py来运行特定测试。

The __init__.py in the tests directory is necessary if you want to run the tests from inside it.如果您想从其中运行测试,则tests目录中的__init__.py是必需的。

You can install nose easily with easy_install or pip :您可以使用easy_installpip轻松安装鼻子:

easy_install nose

or要么

pip install nose

nose extends unittest in a lot more ways, to learn more about it you can check their website: https://nose.readthedocs.org/en/latest/鼻子以更多方式扩展单元测试,要了解更多信息,您可以查看他们的网站: https : //nose.readthedocs.org/en/latest/

On my system (Windows 10), I was required to do something like this:在我的系统(Windows 10)上,我需要做这样的事情:

import sys
import os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/../src")

Appending the relative directory directly to sys.path did not work将相对目录直接附加到sys.path不起作用

The best (most manageable) solution appears to be using a virtualenv and setuptools/distribute to install a development copy of your (src) package.最好(最易于管理)的解决方案似乎是使用 virtualenv 和 setuptools/distribute 来安装 (src) 包的开发副本。 That way your tests execute against a fully "installed" system.这样,您的测试将针对完全“安装”的系统执行。

In the pystest docs there is a section on "good practices" explaining this approach, see here .在 pystest 文档中有一个关于“良好实践”的部分解释了这种方法,请参见此处

For those using Pytest:对于那些使用Pytest 的人:

  • Make sure src is recognized as a package by putting an empty __init__.py inside.通过在里面放置一个空的__init__.py确保src被识别为 package。
  • Put an empty conftest.py in the project folder.在项目文件夹中放置一个空的conftest.py
  • Make sure there is no __init__.py in the test directory.确保test目录中没有__init__.py

Looks like this:看起来像这样:

project
    conftest.py
    src
        __init__.py
        module.py
    test
        test_module.py

And make sure module.py contains the line:并确保module.py包含以下行:

from src import module

Source: Pytest docs来源: Pytest 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM