Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pytest中运行完整测试时会发生导入文件不匹配错误,但单独运行时通过测试_Python_Pytest - Fatal编程技术网

Python 在pytest中运行完整测试时会发生导入文件不匹配错误,但单独运行时通过测试

Python 在pytest中运行完整测试时会发生导入文件不匹配错误,但单独运行时通过测试,python,pytest,Python,Pytest,使用命令python-m pytest tests运行完整的测试时,它会失败,并出现导入文件不匹配错误 import file mismatch: imported module 'test_simplifyvcf_integrations' has this __file__ attribute: /home/promechpc5/Desktop/VCF-SimplifyDev/tests/test_integrations/test_simplifyvcf_integrations.py

使用命令
python-m pytest tests
运行完整的测试时,它会失败,并出现导入文件不匹配错误

import file mismatch:
imported module 'test_simplifyvcf_integrations' has this __file__ attribute:
  /home/promechpc5/Desktop/VCF-SimplifyDev/tests/test_integrations/test_simplifyvcf_integrations.py
which is not the same as the test file we want to collect:
  /home/promechpc5/Desktop/VCF-SimplifyDev/tests/testfiles/TestFullApp/test_simplifyvcf_integrations.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

这里显示的错误是test_simplifyvcf_integration.py,它位于tests文件夹下。但当我使用命令python-m pytest tests/test_integrations/test_simplifyvcf_integrations.py单独运行测试时,它通过了所有测试

这是一个与pytest特别相关的问题,因此您不必太担心,因为它只与缓存文件相关

步骤1:从测试目录中删除所有
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
文件夹

步骤2:在bashrc/zshrc文件中添加以下代码段,具体取决于用于停止生成这些缓存的代码段

export PYTHONDONTWRITEBYTECODE=1

此处的解决方案在错误消息中:

和/或为测试文件模块使用唯一的基名称

您必须确保您的测试中有唯一的模块名——pytest将把它们全部提升到顶级命名空间中,除非您添加
\uuuuu init\uuuuuuuuuuy.py
文件,使它们成为不同的包

因此,您的选择是:

  • 重命名相同的文件
  • 添加一些
    \uuuu init\uuuuu.py
    文件

我不建议使用
pythontwritebytecode
,这将大大降低所有导入的速度,因为它们无法缓存python的编译步骤