简体   繁体   English

pytest不执行test / __ init__.py文件

[英]pytest doesn't execute test/__init__.py file

I have the following problem: I have project and a small library there and a test directory for lib, something like that: 我有以下问题:我有项目和一个小的库和一个lib的测试目录,类似的东西:

project\\ 项目\\

+mylib\\ + MYLIB \\

++test\\ ++测试\\

In test__init__.py I have the following code: 在test__init__.py中,我有以下代码:

SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, "test_data_files")))

So just updating syspath to be able open some files with test data. 所以只需更新syspath即可打开一些带有测试数据的文件。 When I run test by right-click on directory in Pycharm it executes succesfully, but when I run 当我通过右键单击Pycharm中的目录来运行测试时,它会成功执行,但是当我运行时

python -m pytest lib/test

It doesn't executes test/ init .py file and fails with FileNotFoundError, as it can find test data files. 它不执行test / init .py文件并因FileNotFoundError而失败,因为它可以找到测试数据文件。

What is the reason of such behavior, and how can I fix it? 这种行为的原因是什么,我该如何解决?

Thank you. 谢谢。

If you run pytest in lib/test , it will look for test_*.py files and run the tests therein. 如果你在lib/test运行pytest,它将查找test_*.py .py文件并在其中运行测试。 As you found out, it does not import the test package so that the __init__ is never imported. 正如您所知,它不会导入test包,因此永远不会导入__init__ If you run pytest "globally", it imports the packages it runs tests in so that your __init__ is imported. 如果你运行pytest“global”,它会导入它运行测试的包,以便导入__init__

To fix, you could make use of pytest's conftest.py . 要修复,你可以使用pytest的conftest.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM