Relative imports with unittest in Python

时间:2022-05-27 13:13:02

I am trying to use Python unittest and relative imports, and I can't seem to figure it out. I know there are a lot of related questions, but none of them have helped so far. Sorry if this is repetitive, but I would really appreciate any help. I was trying to use the syntax from PEP 328 http://www.python.org/dev/peps/pep-0328/ but I must have something wrong.

我正在尝试使用Python单元测试和相对导入,我似乎无法弄明白。我知道有很多相关的问题,但到目前为止他们都没有帮助。对不起,如果这是重复的,但我真的很感激任何帮助。我试图使用PEP 328 http://www.python.org/dev/peps/pep-0328/的语法,但我一定有问题。

My directory structure is:

我的目录结构是:

project/
    __init__.py
    main_program.py
    lib/
        __init__.py
        lib_a
        lib_b
    tests/
        __init__.py
        test_a
        test_b

I run my tests using:

我运行我的测试:

python -m unittest test_module1 test_module2

test_a needs to import both lib/lib_a and main_program. This is the code from test_a I am trying to use for the import:

test_a需要导入lib / lib_a和main_program。这是我试图用于导入的test_a中的代码:

from ..lib import lib_a as lib
from ...project import main_program

both raise this error:

都引发了这个错误:

ValueError: Attempted relative import in non-package

All of my init.py files are currently empty.

我的所有init.py文件当前都是空的。

Any specific advice would be greatly appreciated!!

任何具体的建议将不胜感激!!

Edit:

This may be the answer: Python Packages? I'm still verifying if this will work.

这可能是答案:Python包?我还在核实这是否有效。

Edit II:

To clarify, at this point I have attempted to run my test file in 3 different ways:

为了澄清,此时我试图以3种不同的方式运行我的测试文件:

project/tests $ python -m unittest test_a
project/tests $ python -m test_a
project/tests $ ./test_a

All three fail with the same error as above. When I use the same three syntaxes but in the project directory, I get this error:

所有三个都失败并出现与上述相同的错误。当我在项目目录中使用相同的三种语法时,我收到此错误:

ValueError: Attempted relative import beyond toplevel package

Thanks again.

2 个解决方案

#1


16  

In my experience it is easiest if your project root is not a package, like so:

根据我的经验,如果您的项目根不是包,那么这是最简单的,如下所示:

project/
  test.py
  run.py
  package/
    __init__.py
    main_program.py
    lib/
      __init__.py
      lib_a
      lib_b
    tests/
      __init__.py
      test_a
      test_b

However, as of python 3.2 , the unittest module provides the -t option, which lets you set the top level directory, so you could do (from package/):

但是,从python 3.2开始,unittest模块提供了-t选项,它允许你设置*目录,所以你可以(从包/):

python -m unittest discover -t ..

More details at the unittest docs.

有关unittest docs的更多详细信息。

#2


2  

I run with the same problem and kai's answer solved it. I just want to complement his answer with the content of test.py (as @gsanta asked). I've only tested it on Python 2.7:

我运行同样的问题,凯的答案解决了它。我只想用test.py的内容补充他的答案(如@gsanta所说)。我只在Python 2.7上测试过它:

from packages.tests import test_a, test_b
import unittest

# for test_a
unittest.main(test_a)

# for test_b
unittest.main(test_a)

then you can just

那么你可以

../project $ python test.py

#1


16  

In my experience it is easiest if your project root is not a package, like so:

根据我的经验,如果您的项目根不是包,那么这是最简单的,如下所示:

project/
  test.py
  run.py
  package/
    __init__.py
    main_program.py
    lib/
      __init__.py
      lib_a
      lib_b
    tests/
      __init__.py
      test_a
      test_b

However, as of python 3.2 , the unittest module provides the -t option, which lets you set the top level directory, so you could do (from package/):

但是,从python 3.2开始,unittest模块提供了-t选项,它允许你设置*目录,所以你可以(从包/):

python -m unittest discover -t ..

More details at the unittest docs.

有关unittest docs的更多详细信息。

#2


2  

I run with the same problem and kai's answer solved it. I just want to complement his answer with the content of test.py (as @gsanta asked). I've only tested it on Python 2.7:

我运行同样的问题,凯的答案解决了它。我只想用test.py的内容补充他的答案(如@gsanta所说)。我只在Python 2.7上测试过它:

from packages.tests import test_a, test_b
import unittest

# for test_a
unittest.main(test_a)

# for test_b
unittest.main(test_a)

then you can just

那么你可以

../project $ python test.py