运行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文件即可解决问题

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐