Pytest是一个高级别的测试

2024-09-30 18:20:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在一台机器上运行pytest,但是pytestconftest的寻源级别提高了一个级别。我使用的目录没有__init__.py

17:09:36  /shared/functionaltests/kubernetesTests
17:09:36  + rm __init__.py
17:09:36  + ls
17:09:36  conftest.py
17:09:36  helper_functions.py
17:09:36  kubernetesTests.groovy
17:09:36  readme.txt
17:09:36  test_complete.py
17:09:36  + pytest ./
17:09:36  ImportError while loading conftest '/shared/functionaltests/conftest.py'.
17:09:36  ../conftest.py:8: in <module>
17:09:36      import functionaltests.panda_functions as pf
17:09:36  ../panda_functions.py:1: in <module>
17:09:36      import pandas
17:09:36  E   ModuleNotFoundError: No module named 'pandas'

-Removed __init__.py
-launching pytest using py.test and python -m pytest
-using --rootdir=./

Tags: inpytestimportpandasinitpytest级别
1条回答
网友
1楼 · 发布于 2024-09-30 18:20:12

这是一种预期的行为,因为您没有显式指定根目录是什么(例如,在目录中放入pytest.ini),而在父目录中放入conftest.pypytest将进入父目录,查找pytest.ini,将遇到conftest.py,并将目录计数为rootdir本身或rootdir的子目录。如果要使kubernetesTests成为rootdir,请在其中放置一个空的pytest.ini

touch /shared/functionaltests/kubernetesTests/pytest.ini`

现在不会加载父级conftest。你知道吗

另一种解决方案是通过将/shared添加到sys.path来修复导入:

PYTHONPATH=/shared pytest

现在/shared仍然是rootdir(它应该是这样的),并且父conftest.py被加载并执行。你知道吗

相关问题 更多 >