w30-python02-pytest入门

代码如下:

python 复制代码
import pytest
class Test_Obj:
    """测试类"""
    #用例级别前后置
    def setup(self):
        print('用例级别------的前置处理')

    def teardown(self):
        print("用例级别--------的后置处理")

    # 用例
    def test_case1(self):
        print("test_case1")

    def test_case2(self):
        print("test_case2")

    #类级别操作
    def setup_class(self):
        print('类级别的前置处理')
    def teardown_class(self):
        print('类级别的后置处理')

代码说明:

通过命名setup、teardown、用例级别的前置和后置

通过命名setup_class、teardown_class、类级别的前置和后置

常见报错一:

no pytest runner found in the selected in interpreter

Pycharm自身不带解释器 所以需要自己安装好环境并且在Pycharm里面配置使用的解释器

确定自己安装好环境之后 可以按照下面的步骤完成设置

如果有已经设置过的解释器 就直接点击小三角选择 没有的话 点击 Show All 进行添加

常见报错二:

test_c.py::Test_C::test_001

D:\Z_Enviroment\python\ApiTestProject\lib\site-packages_pytest\fixtures.py:901: PytestRemovedIn8Warning: Support for nose tests is deprecated and will be removed in a future release.

test_c.py::Test_C::test_001 is using nose-specific method: setup(self)

To remove this warning, rename it to setup_method(self)

See docs: Deprecations and Removals - pytest documentation

fixture_result = next(generator)

原因是:setup和teardown并不是pytest的原生用法,在pytest中的原生用法应该是setup_method和teardown_method

解决办法:修改方法名为setup_method和teardown_method

pytest运行方式:

第一种执行方式:通过pycharm 的 pytest插件来识别并执行

第二种执行方式:通过主函数来执行 pytest.main(['-s']) -s:可以在控制台显示执行结果

通常为了方便,可以以外部文件去运行,相当于写一个运行入口

import pytest

if name == 'main':

pytest.main(['-s', 'pytest01.py'])

第三种执行方式:通过命令行的方式执行,pytest 文件路径/测试文件名

相关推荐
胡耀超22 分钟前
标签体系设计与管理:从理论基础到智能化实践的综合指南
人工智能·python·深度学习·数据挖掘·大模型·用户画像·语义分析
博观而约取1 小时前
Django 数据迁移全解析:makemigrations & migrate 常见错误与解决方案
后端·python·django
熊猫钓鱼>_>2 小时前
用Python解锁图像处理之力:从基础到智能应用的深度探索
开发语言·图像处理·python
蛋仔聊测试2 小时前
Playwright 中特定的 Fixtures
python
蹦蹦跳跳真可爱5892 小时前
Python----大模型(使用api接口调用大模型)
人工智能·python·microsoft·语言模型
好开心啊没烦恼2 小时前
Python 数据分析:numpy,抽提,整数数组索引与基本索引扩展(元组传参)。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy·pandas
清幽竹客2 小时前
Day 3:Python模块化、异常处理与包管理实战案例
python
菜包eo3 小时前
二维码驱动的独立站视频集成方案
网络·python·音视频
Yo_Becky3 小时前
【PyTorch】PyTorch预训练模型缓存位置迁移,也可拓展应用于其他文件的迁移
人工智能·pytorch·经验分享·笔记·python·程序人生·其他
yzx9910133 小时前
关于网络协议
网络·人工智能·python·网络协议