当前位置: 首页 > news >正文

w30-python02-pytest入门

代码如下:

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

http://www.lryc.cn/news/406301.html

相关文章:

  • WPF+Mvvm项目入门完整教程-仓储管理系统(二)
  • SkyWalking入门搭建【apache-skywalking-apm-10.0.0】
  • exo项目目录架构
  • mysql中where与on区别
  • filebeat把日志文件上传到Es中配置(ES7版本)
  • Vue Router基础
  • Apache压测工具ab(Apache Bench)工具的下载安装和使用示例
  • IPIDEA与Python爬虫:联手解锁全球电商数据宝库
  • Fine-BI学习笔记
  • AI 辅助编程 Coding AI 辅助研发组织的技术蓝图
  • VScode 批量操作
  • 【Linux】管道通信和 system V 通信
  • Python | Leetcode Python题解之第279题完全平方数
  • mysql定时备份
  • 数据结构:逻辑结构与物理结构
  • pycharm报错:No module named pip/No module named pytest
  • Linux:Linux权限
  • 新版Glide检测生命周期原理
  • Ansible的脚本-----playbook剧本【上】
  • sql注入学习与防护
  • 饥荒dst联机服务器搭建基于Ubuntu
  • AtCoder Beginner Contest 363
  • Protel DXP 面试题详解及参考答案(4万字长文)
  • 雪花算法 集群uid重复问题 uid-generator-spring-boot-starter
  • 【AutoDL】AutoDL+Xftp+Xshell+VSCode配合使用教程
  • 使用minio cllient(mc)完成不同服务器的minio的数据迁移和mc基本操作
  • Vue3分段控制器(Segmented)
  • SpringSecurity如何正确的设置白名单
  • 【Langchain大语言模型开发教程】评估
  • Python爬虫小项目实战