这个错误可能是由于导入Type时在pytest中出现了问题,可能会有以下两个原因造成此问题:
在pytest运行过程中发现Python版本不兼容:在Python 3.5及更早版本中,Type不是一个内置模块,因此如果您的代码在早期版本的Python中编写,pytest就可能无法正常导入Type。您可以检查使用的Python版本或考虑升级您的Python版本。
某些库或框架导致导入问题:它也可能是由于某些库或框架与其中的引用关系和导入规则不兼容而导致错误。如果这种情况出现,您可以尝试升级您使用的库或框架,或者在测试文件中检查导入的库是否存在问题。
针对此问题,可以尝试以下解决方法:
确认Python版本:请确保您的Python版本是否符合要求。在Python 3.5及以前版本,Type为不是内置模块。
重新安装依赖项:如果此问题是由于依赖于某些库或框架而导致的,请尝试重新安装或更新相关软件包。
检查导入的库:请检查您的测试文件中导入的库是否存在问题,可以通过重新导入这些库来修复此问题。
如果上述解决方法无法解决此问题,建议检查导入Type的其他地方以找出其他可能性。
【相关推荐】
importlib模式是pytest6.0以后的版本支持的新的方式,importlib方式不再需要修改sys.path和sys.modules,因此不存在上面prepend和append面临的潜在的问题,采用的是一种全新的导入方式,这里首先也来看个例子
目录结构
demo01/
|----demo02/
|----demo04/
|----test_demo01.py
|----demo04/
|----test_demo01.py
如果按照prepend或者append的思路分析,这里肯定是执行不起来的,导入模块的名字肯定是重复的,这里也可以执行以下如下:
$ pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: G:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collecting ... sys.path:['G:\\src\\blog\\tests\\demo01\\demo02\\demo04', 'D:\\python39\\Scripts\\pytest.exe', 'D:\\python39\\python39.zip', 'D:\\python39\\DLLs', 'D:\\py
thon39\\lib', 'D:\\python39', 'D:\\python39\\lib\\site-packages']
module:test_demo01
collected 1 item / 1 error
================================================================================ ERRORS ================================================================================
____________________________________________________________ ERROR collecting demo01/demo04/test_demo01.py _____________________________________________________________
import file mismatch:
imported module 'test_demo01' has this __file__ attribute:
G:\src\blog\tests\demo01\demo02\demo04\test_demo01.py
which is not the same as the test file we want to collect:
G:\src\blog\tests\demo01\demo04\test_demo01.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
======================================================================= short test summary info ========================================================================
ERROR demo01/demo04/test_demo01.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
=========================================================================== 1 error in 0.16s ===========================================================================
但是因为importlib模式不会去修改sys.paht和sys.mo,因此也就不会有这个问题了,执行结果如下:
$ pytest -s --import-mode=importlib
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.6, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: G:\src\blog\tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, hypothesis-6.31.6, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collecting ... sys.path:['D:\\python39\\Scripts\\pytest.exe', 'D:\\python39\\python39.zip', 'D:\\python39\\DLLs', 'D:\\python39\\lib', 'D:\\python39', 'D:\\python39\\lib
\\site-packages']
sys.path:['D:\\python39\\Scripts\\pytest.exe', 'D:\\python39\\python39.zip', 'D:\\python39\\DLLs', 'D:\\python39\\lib', 'D:\\python39', 'D:\\python39\\lib\\site-packages
']
collected 2 items
demo01\demo02\demo04\test_demo01.py .
demo01\demo04\test_demo01.py .
========================================================================== 2 passed in 0.06s ===========================================================================
这就是pytest自动化脚本的加载原理,至此也就明白当前pytest默认情况下采用的是prepared模式,而在这种模式下,如果文件夹中没有__init__.py文件,一定要保持测试文件命名的独一无二性,所以在实践中,为了减少一些潜在的问题,建议在创建文件夹的时候,直接在所有文件夹下创建__init__.py文件,如此则不需要担心测试脚本文件名重名的问题了。