如何测试我的 `__main__` 文件的功能?

问题描述 投票:0回答:0

我正在使用 pytest。注意 W10。
我将通过

> python src/core
来运行我的应用程序。 “core”是我的中心模块,有一个
__main__.py
文件。

因此,我将“测试”目录放在 [项目根目录]/src/core 中。

我有一个测试文件test_xxx.py。在其中我尝试导入

__main__

import __main__

... 

def test_xxxxx():
   print(f'main {__main__} type {type(__main__)}')

这将打印以下内容:

main <module '__main__' from 'D:\\apps\\Python\\virtual_envs\\doc_indexer v2.0.0\\Scripts\\pytest.exe\\__main__.py'> type <class 'module'>

...换句话说,它不是从本地“核心”模块导入我本地的

__main__.py
,而是从其模块导入 pytest 自己的
__main__
。到目前为止还算可以理解。然后我查看 sys.path:

path: D:\My Documents\software projects\EclipseWorkspace\doc_indexer\src\core\tests
path: D:\My Documents\software projects\EclipseWorkspace\doc_indexer\src\core
path: D:\apps\Python\virtual_envs\doc_indexer v2.0.0\Scripts\pytest.exe
path: D:\apps\Python\PyQt5
...

现在我正在摸不着头脑,这不是第一次使用 pytest。这...sr

python testing module pytest
© www.soinside.com 2019 - 2024. All rights reserved.