python - 在 pytest 中运行单个文件,该文件是 PEP 420 隐式命名空间包中的模块

标签 python python-3.x pytest

我有一个使用相对导入的文件dir/subdir/module.py,以便

python3 dir/subdir/module.py

失败并显示

ModuleNotFoundError: No module named '__main__.test_forward'; '__main__' is not a package

但是

python3 -m dir.subdir.module

运行。

毫不奇怪,

python3 -m pytest dir/subdir/module.py
pytest dir/subdir/module.py

也失败了。我可以从 pytest 运行此模块的测试(而不更改它)吗?

当然,我看过How to test single file under pytesthttps://docs.pytest.org/en/latest/usage.html#cmdline ,但未能找到答案。

这是一个最小的复制示例:

文件proj/main.py:

def func():
    return 1

文件proj/tests.py:

from .main import func

def test_func():
    assert func() == 1

if __name__ == "__main__":
    test_func()

请注意,我按照 PEP 420proj 中省略了 __init__.py .

(venv) romanov@k9-09:~/temp$ python3 -m proj.tests
(venv) romanov@k9-09:~/temp$

测试运行并通过(如果我将 test_func 更改为失败,我会得到预期的结果)。

(venv) romanov@k9-09:~/temp$ python3 proj/tests.py
Traceback (most recent call last):
  File "proj/tests.py", line 1, in <module>
    from .main import func
SystemError: Parent module '' not loaded, cannot perform relative import

使用 pytest:

(venv) romanov@k9-09:~/temp$ pytest proj/tests.py
============================================================================================ test session starts =============================================================================================
platform linux -- Python 3.5.2, pytest-4.3.1, py-1.8.0, pluggy-0.9.0
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/home/romanov/temp/.hypothesis/examples')
rootdir: /home/romanov/temp, inifile:
plugins: hypothesis-4.0.1
collected 0 items / 1 errors

=================================================================================================== ERRORS ===================================================================================================
_______________________________________________________________________________________ ERROR collecting proj/tests.py _______________________________________________________________________________________
proj/tests.py:1: in <module>
    from .main import func
E   SystemError: Parent module '' not loaded, cannot perform relative import
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================================================== 1 error in 0.09 seconds ===========================================================================================

不幸的是,这并不能完全重现问题;这里 ivan_pozdeev 的答案中建议的 pytest --pyargs proj.testsERROR: file or package not find: proj.tests (missing __init__.py?) 并添加空 >proj/__init__.py 让它运行。

最佳答案

根据Usage and Invocations — pytest documentation :

Run tests from packages

pytest --pyargs pkg.testing

This will import pkg.testing and use its filesystem location to find and run tests from.

In [9]: !ls -R t
t:
__init__.py  main.py  tests.py

In [10]: pwd
Out[10]: u'c:\\Users\\Sasha\\Desktop'

In [7]: os.environ['PYTHONPATH']=r'c:\Users\Sasha\Desktop'

In [8]: !pytest --pyargs t.tests
============================= test session starts =============================
platform win32 -- Python 2.7.15+, pytest-3.0.3, py-1.5.4, pluggy-0.4.0
rootdir: c:\Users\Sasha\Desktop, inifile:
plugins: xdist-1.15.0, mock-1.5.0, instafail-0.3.0
collected 1 items

t\tests.py .

========================== 1 passed in 0.02 seconds ===========================

如果按照 PEP 420 省略 __init__.pypytest 会变得困惑,因为其逻辑的核心前提是在子树中搜索测试文件并导入它们。如果没有 __init__.py,它就无法确定是否应该将文件作为子包或独立模块导入。

关于python - 在 pytest 中运行单个文件,该文件是 PEP 420 隐式命名空间包中的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55351510/

相关文章:

Python 用意外的间距诅咒 Git 子进程输出

python - 在 Django 中管理继承

python - 安装 MySQL-Python 时出错

Python urllib3 错误 - 导入错误 : cannot import name UnrewindableBodyError

python-3.x - Flask-restplus API中如何将API改为域名?

python - 如何为 py.test 中的所有测试跨模块共享变量

python - MySQL 服务器消失了 python MySQLdb

python - 如何制定更好的多次读数据库策略?

python - 如何参数化测试函数 : Pytest?

python - 当使用带有作用域 session 和类的装置时,带有作用域类的测试运行两次