解决:HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

运行pytest用例,出现如下的报错:

root@0d71811f7c01:/var/jenkins_home/workspace/first_python# python3 -m pytest --alluredir=./unit/allure_result
======================================================================= test session starts ========================================================================
platform linux -- Python 3.6.8, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /var/jenkins_home/workspace/first_python
plugins: allure-pytest-2.8.19
collected 9 items / 1 error / 8 selected

============================================================================== ERRORS ==============================================================================
__________________________________________________________________ ERROR collecting test_case.py ___________________________________________________________________
import file mismatch:
imported module 'test_case' has this __file__ attribute:
  /var/jenkins_home/workspace/first_python/test_case
which is not the same as the test file we want to collect:
  /var/jenkins_home/workspace/first_python/test_case.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
===================================================================== short test summary info ======================================================================
ERROR test_case.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================================= 1 error in 0.24s =========================================================================

这是因为我一个test_case.py文件和test_case目录同名导致的错误,如下:

 ls
api  base  config  test_case  test_case.py  unit

解决办法,修改文件名为另外的名字从新运行就可以解决了。

mv test_case.py test_pr.py
root@0d71811f7c01:/var/jenkins_home/workspace/first_python# ls
api  base  config  test_case  test_pr.py  unit
root@0d71811f7c01:/var/jenkins_home/workspace/first_python# python3 -m pytest --alluredir=./unit/allure_result
======================================================================= test session starts ========================================================================
platform linux -- Python 3.6.8, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /var/jenkins_home/workspace/first_python
plugins: allure-pytest-2.8.19
collected 12 items

test_pr.py ...                                                                                                                                               [ 25%]
test_case/test_case.py ...                                                                                                                                   [ 50%]
test_case/test_tag.py EEEEE                                                                                                                                  [ 91%]
test_case/test_wechat_api.py F                                                                                                                               [100%]

法二:

在同名文件所在目录(即first_python目录)下添加__init__.py文件即可解决问题

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值