在项目中创建一个dep1目录,该目录下又有两个目录,如下所示:
在dep1目录下执行pytest -sv命令,报错如下:
D:\pythonProject\dependency\dep1>pytest -sv
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\dependency\dep1
plugins: allure-pytest-2.9.41, dependency-0.5.1
collected 2 items / 1 error / 1 selected
============================================================================== ERRORS ===============================================================================
_______________________________________________________________ ERROR collecting dir2/test_dir_01.py ________________________________________________________________
import file mismatch:
imported module 'test_dir_01' has this __file__ attribute:
D:\pythonProject\dependency\dep1\dir1\test_dir_01.py
which is not the same as the test file we want to collect:
D:\pythonProject\dependency\dep1\dir2\test_dir_01.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
====================================================================== short test summary info ======================================================================
ERROR dir2/test_dir_01.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================================= 1 error in 0.17s ==========================================================================
解决方法一:修改任一目录下的py文件名称
解决方法二:在dir1或dir2目录下添加添加__init__.py文件。例如:
执行结果:
D:\pythonProject\dependency\dep1>pytest -sv
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\dependency\dep1
plugins: allure-pytest-2.9.41, dependency-0.5.1
collected 4 items
dir1/test_dir_01.py::Testna::test_01 function test_01
PASSED
dir1/test_dir_01.py::Testna::test_02 function test_02
PASSED
dir2/test_dir_01.py::Testna::test_01 function test_01
PASSED
dir2/test_dir_01.py::Testna::test_02 function test_02
PASSED
========================================================================= 4 passed in 0.06s =========================================================================