pytest时提示:Empty test suite.

本文探讨了在使用pytest进行代码测试时遇到的Emptytestsuite错误,并提供了详细的解决方案。主要原因是测试类命名和main函数调用方式不当。通过调整这些设置,可以避免此错误并确保测试顺利运行。

从来没接触过pytest,在test别人代码时,一直出现Empty test suite.百度了很多种方法都没有用

默认情况下,PyCharm将检查以test开头的文件,它们是unittest.TestCase的子类,但是你可以控制模式和子类选项。 
根据您的测试文件名更改模式,它接受Python正则表达式。 

Empty test suite的出现前提是:

1.类名用Test命名

2.代码中使用了main函数调用类中的内容

所以解决办法:只要不满足上面的某一条件就行
 

### 关于 pytest 中 AttributeError: module 'pytest' has no attribute 'hookimpl' 的解决方案 在使用 pytest 模块,如果遇到 `AttributeError: module 'pytest' has no attribute 'hookimpl'` 错误,通常是由以下原因引起的:脚本文件名与 Python 预留模块名冲突,或者环境配置不正确。以下是详细的分析和解决方法: #### 1. 文件名冲突 如果脚本文件名被命名为 `pytest.py`,这将与 pytest 模块的名称冲突,导致 Python 在导入 pytest 模块优先加载当前目录下的 `pytest.py` 文件[^2]。这种情况下,Python 会认为 pytest 模块中没有 `hookimpl` 属性。 **解决方法** 将脚本文件名更改为非冲突名称,例如 `test_pytest.py` 或 `pytest_demo.py`。确保脚本文件名不与 Python 标准库或第三方模块名称冲突[^2]。 #### 2. 环境问题 如果环境中安装了多个版本的 pytest,可能会导致模块加载错误。此外,某些插件或依赖可能未正确安装,也可能引发类似问题。 **解决方法** - 卸载并重新安装 pytest 模块以确保环境干净: ```bash pip uninstall pytest pip install pytest ``` - 如果使用的是虚拟环境,请确保激活正确的虚拟环境后再执行上述命令。 #### 3. 插件冲突 某些 pytest 插件(如 `pytest-assume`)可能与 pytest 主模块存在兼容性问题。如果插件未正确安装或版本不匹配,可能导致属性缺失错误。 **解决方法** 检查插件安装情况,并根据需要重新安装插件: ```bash pip uninstall pytest-assume pip install pytest-assume ``` #### 示例代码 以下是一个简单的 pytest 脚本示例,用于验证问题是否解决: ```python import pytest @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): outcome = yield report = outcome.get_result() if report.when == "call": print("Test executed") ``` 运行脚本,请确保使用 pytest 命令而不是直接运行 Python 脚本: ```bash pytest test_script.py ``` ### 总结 通过更改脚本文件名以避免与 pytest 模块冲突、清理并重新安装 pytest 及其相关插件,可以有效解决 `AttributeError: module 'pytest' has no attribute 'hookimpl'` 问题[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值