Pytest01-环境安装与入门

Pytest01-安装与入门


B站高清视频链接

一、pytest介绍与安装

1.pytest框架介绍

Pytest是Python的另一个第三方单元测试库。在自动化测试领域,pytest就是用来组织自动化用例执行的,包括指定执行的用例模块、用例前置后置操作、跳过执行、失败退出等控制功能。
pytest的特性有:

  • 支持用简单的assert语句实现丰富的断言,无需复杂的self.assert*函数
  • 自动识别测试模块和测试函数
  • 模块化夹具用以管理各类测试资源
  • 对 unittest 完全兼容,对 nose基本兼容
    支持Python3和PyPy3
  • 丰富的插件生态,已有1000多个各式各样的插件,社区繁荣
    插件链接:https://docs.pytest.org/en/latest/reference/plugin_list.html

2.pytest 安装

a)安装

注意,尽量使用下面版本,最新版本可能会引入兼容性问题,比如Allure报告参数信息展示等等

pip install pytest==7.3.1

如果不清楚第三方库的版本和pytest的版本兼容关系可以在如下网址查询:

https://pypi.org/project/pytest-rerunfailures/#requirements

b)验证安装

pytest --version

c) 运行模式的配置

在这里插入图片描述

如上图,请在File>Setting>Tools>Python Integrated Tools>Testing>Defalut test runner中选择Unittests模式

默认是pytest或者auto,走的Pytest调试模式,只支持单文件调试,Unittests模式才能运行多文件。

d) pytest文档

官方文档:https://docs.pytest.org/en/latest/contents.html

3.pytest的用例运行规则

1)pytest将在当前目录及其子目录中运行所有格式为test_.py或者_test.py文件
2)测试方法/测试函数 默认必须是test开头
3) 测试类必须是Test开头
4) 测试类不能有构造方法 init
参考代码目录:
在这里插入图片描述

# 文件名: test_pytest.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
1)pytest将在当前目录及其子目录中运行所有格式为test_*.py或者*_test.py文件
2)测试方法/测试函数 默认必须是test开头
3) 测试类必须是Test开头
4) 测试类不能有构造方法 __init__
'''
import pytest

def test01():
    print("hello")
    assert 1 == 2

if __name__ == '__main__':
    pytest.main(['-s'])
# 文件名: test_aabb.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 测试函数
def test02():
    print("doc目录下的用例test02")

class TestShopping:
    # pytest测试类不能使用构造方法
    # def __init__(self):
    #     print("构造方法")

    # 测试方法
    def test03(self):
        print("类下的用例")

在test_pytest.py文件运行后结果

D:\Python38\python.exe 
============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-7.3.1, pluggy-0.13.1
rootdir: E:\p01_pytest_discover
plugins: allure-pytest-2.8.11, base-url-2.0.0, forked-1.1.3, html-3.0.0, metadata-1.8.0, ordering-0.6, parallel-0.1.0, playwright-0.3.3, repeat-0.9.1, rerunfailures-9.1.1, xdist-1.31.0
collected 3 items

test_pytest.py hello
F
doc\test_aabb.py doc目录下的用例test02
.类下的用例
.

================================== FAILURES ===================================
___________________________________ test01 ____________________________________

    def test01():
        print("hello")
>       assert 1 == 2
E       assert 1 == 2

test_pytest.py:13: AssertionError
=========================== short test summary info ===========================
FAILED test_pytest.py::test01 - assert 1 == 2
========================= 1 failed, 2 passed in 0.21s =========================

Process finished with exit code 0

测试技术交流请联系我
在这里插入图片描述
备注CSDN扶摇

【学习软件测试/Python自动化测试技术/领取Python自动化测试学习路线图/简历优化】
视频链接:
课程服务介绍

自动化全栈学习路线图讲解

软件测试面试合集

Python编程刷题合集

Pytest入门到实战

Python接口自动化合集

PythonWeb自动化合集

性能测试合集

Jmeter接口自动化测试实战全集

2023GPT探索发现合集

加微信(备注CSDN扶摇)即可免费领取下面的自动化测试资料和一份软件测试面试宝典
在这里插入图片描述
在这里插入图片描述

  • 23
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试老宅男扶摇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值