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 文件路径/测试文件名

相关推荐
0思必得014 分钟前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
沈浩(种子思维作者)27 分钟前
系统要活起来就必须开放包容去中心化
人工智能·python·flask·量子计算
2301_7903009632 分钟前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python
m0_736919101 小时前
用Pandas处理时间序列数据(Time Series)
jvm·数据库·python
getapi1 小时前
实时音视频传输与屏幕共享(投屏)
python
java干货1 小时前
为什么 “File 10“ 排在 “File 2“ 前面?解决文件名排序的终极算法:自然排序
开发语言·python·算法
机器懒得学习1 小时前
智能股票分析系统
python·深度学习·金融
毕设源码-郭学长1 小时前
【开题答辩全过程】以 基于python的二手房数据分析与可视化为例,包含答辩的问题和答案
开发语言·python·数据分析
SR_shuiyunjian1 小时前
Python第三次作业
python
vx_biyesheji00011 小时前
豆瓣电影推荐系统 | Python Django 协同过滤 Echarts可视化 深度学习 大数据 毕业设计源码
大数据·爬虫·python·深度学习·django·毕业设计·echarts