ImportError显示py.test,但在运行应用程序时不显示

时间:2016-06-01 05:33:58

标签: python python-3.x importerror pytest circular-dependency

与此问题不同: Importing modules from a sibling directory for use with py.test 我可以从我的应用中导入一些内容,但是在运行时,' myapp会导致导入错误(看起来像循环依赖项)测试而不是单独运行myapp

$ python3 myapp/myapp.py
Some dummy string (correct output)

可是:

$ python3 -m pytest                                                                               
================================================================= test session starts =================================================================
platform linux -- Python 3.4.3, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: /home/nico/temp/projects_structures/test04/myapp, inifile: 
plugins: cov-2.2.1
collected 0 items / 1 errors 

======================================================================= ERRORS ========================================================================
________________________________________________________ ERROR collecting tests/test_things.py ________________________________________________________
tests/test_things.py:4: in <module>
    from myapp.lib.core.base import do_things
myapp/lib/core/base.py:1: in <module>
    from lib import something
E   ImportError: No module named 'lib'
=============================================================== 1 error in 0.05 seconds ===============================================================

正如您所看到的,问题是来自测试文件的import语句。它是从“内心”中筹集而来的。 myapp

这是完整的结构:

.
└── myapp
    ├── myapp
    │   ├── __init__.py
    │   ├── lib
    │   │   ├── core
    │   │   │   ├── base.py
    │   │   │   └── __init__.py
    │   │   └── __init__.py
    │   └── myapp.py
    └── tests
        └── test_things.py

myapp.py包含:

#!/usr/bin/env python3

from lib.core import base

base.do_things()

lib / __ init__.py包含:

something = "Some dummy string (correct output)"

base.py包含:

from lib import something

def do_things():
    print(something)
    return True

和test_things包含:

import unittest
import sys
sys.path.insert(0, '..')
from myapp.lib.core.base import do_things

class DoThingsTestCase(unittest.TestCase):

    def test_do_things(self):
        self.assertTrue(do_things())

if __name__ == '__main__':
    unittest.main()

$PYTHONPATH似乎设置正确(所以这个: Py.test No module named * 没有回答我的问题)。 (或者,如果这不正确,我该如何纠正?)

$ echo $PYTHONPATH
/home/nico/temp/projects_structures/test04/myapp/myapp

2 个答案:

答案 0 :(得分:3)

设置PYTHONPATH应该可以解决问题。

$ export PYTHONPATH=<ABSOLUTE PATH TO TOPMOST myapp dir>

(在此示例中,它是myapp / myapp的路径;并确保您导出 PYTHONPATH,而不仅仅是设置它。

并从myapp运行

$ py.test

$ python3 -m pytest

答案 1 :(得分:2)

另一种方法是将conftest.py文件放入myapp的顶级或myapp/tests。像这样:

$ pwd
/home/nico/temp/projects_structures/test04/myapp/tests
$ touch conftest.py
$ cd ..
$ py.test
================================================================= test session starts =================================================================
platform linux2 -- Python 2.7.6, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: /home/nico/temp/projects_structures/test04/myapp, inifile: 
collected 1 items 

tests/test_things.py .

============================================================== 1 passed in 0.05 seconds ===============================================================
$

PYTHONPATH为空:

$ echo $PYTHONPATH

$

这样,py.test会自动将myapp添加到PYTHONPATH。这样可以避免忘记导出PYTHONPATH并使myapp的测试更容易为其他开发人员(他们都不需要解决此问题)。