20

我在包中有一个名称为“test”的文件,当我运行 pytest 时出现错误

import file mismatch:
imported module 'my_project.my_file_test' has this __file__ attribute:
  /my_project/src/my_project/build/lib/python2.7/site-packages/foo/my_file_test.py
which is not the same as the test file we want to collect:
  /my_project/src/my_project/build/private/python2.7/lib/foo/my_file_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

如果我从文件中删除“测试”,它可以正常工作,但不幸的是我无法更改它。

所以问题是如何告诉 pytest 忽略这个文件?

4

3 回答 3

37

就我而言,__init__.py测试目录中缺少文件。

于 2020-05-26T13:57:32.027 回答
5

所以最后很简单,我只需要在文件中添加测试文件模式pytest.ini

python_files = test_*.py

因此 pytest 停止查找test名称末尾带有默认情况的文件。

于 2018-12-27T00:29:26.160 回答
0

即使我的文件名与其他文件名不完全相同,我也面临同样的问题。

我有test_01_login_chrome.pytest_01_login_firefox.py

我得到的错误

import file mismatch:
imported module 'test_01_login_chrome.py' has this __file__ attribute:
  /Users/avicii/PycharmProjects/Omni_Sanity/TestCase/Firefox_TestCases/test_01_login_chrome.py
which is not the same as the test file we want to collect:
  /Users/avicii/PycharmProjects/Omni_Sanity/TestCase/01_Chrome_TestCases/test_01_login_chrome.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

我尝试添加__init__.py到测试用例文件夹,但它不起作用。

然后我尝试了一种解决方法。我为我的测试用例创建了一个 python 包,它__init__.py默认创建,然后将我的测试用例移动到该文件夹​​。

我的问题得到了解决。

于 2020-09-06T05:23:24.023 回答