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

我有一个使用相对导入的文件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 pytest和https://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 420,我在__init__.py中省略了proj

(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 found: proj.tests (missing __init__.py?)并添加空的proj/__init__.py使其运行。

参考方案

根据Usage and Invocations — pytest documentation:

从包运行测试

pytest --pyargs pkg.testing

这将导入pkg.testing并使用其文件系统位置查找
并从中运行测试。

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__.py,则pytest会感到困惑,因为其逻辑的核心前提是在子树中搜索测试文件并将其导入。如果没有__init__.py,它将无法确定是否应将文件作为子包或独立模块导入。

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…