pytest【1】

文章目录

pytest

单selenuim的问题:

  • 代码重用性
  • 元素加载延迟
  • 浏览器重复驱动

解决方法:封装框架 pytest

测试用例示例

  • 创建 test_ 开头的文件
  • 创建 test_ 开头的函数(或类)
  • 用例中使用断言assert
python 复制代码
def test_a(): # 测试用例a
	assert 2==2  #若不符合预期,则
class TestYongli:  # 测试套件,,类,将不同的测试用例打包在一起
	def test_b():  # 测试用例b
		...

例如:文件名test_class_3.py

python 复制代码
def test_a():
    a = 2
    b = 2
    assert a == b
    print("a==b")

def test_b():
    a = 1
    b = 2
    assert a == b
    print("a==b")

def test_c():
    a = 3
    b = 3
    c = 4
    assert a == b
    assert a == c
    print("a==b")

执行结果:(那个50%和100%是测试进度条,表示执行了50%的用例。。)

要注意:如果鼠标点在某个函数里点执行的话,执行的是那一个函数用例而不是全部。

python 复制代码
============================= test session starts =============================
collecting ... collected 3 items

test_class_3.py::test_a PASSED                                           [ 33%]a==b

test_class_3.py::test_b FAILED                                           [ 66%]
test_class_3.py:11 (test_b)
1 != 2

Expected :2
Actual   :1
<Click to see difference>

def test_b():
        a = 1
        b = 2
>       assert a == b
E       assert 1 == 2

test_class_3.py:15: AssertionError

test_class_3.py::test_c FAILED                                           [100%]
test_class_3.py:18 (test_c)
3 != 4

Expected :4
Actual   :3
<Click to see difference>

def test_c():
        a = 3
        b = 3
        c = 4
        assert a == b
>       assert a == c
E       assert 3 == 4

test_class_3.py:24: AssertionError


========================= 2 failed, 1 passed in 0.20s =========================

测试用例批量执行

1、terminal执行pytest

执行结果:

2、main代码启动

创建main.py

python 复制代码
import pytest

if __name__ == "__main__":	# 表示当当前文档是被import调用时,该if后的代码块不会被执行  
    pytest.main()  # 程序入口
    
# 当python文件直接被运行时,if __name__ == '__main__': 语句下面的代码段将被执行。
# 当python文件以模块形式被调用时,if __name__ == '__main__': 语句下面的代码段不会被执行。

测试结果

F:功能有问题;E:测试框架有问题

fixtures(夹具)

可以在测试用例的执行前和执行后,自动执行一部分代码,例如浏览器的启动和关闭。实现代码复用。

创建fixtures

fixtures = 函数 + fixtures装饰器

文件名必须叫 conftest.py

python 复制代码
import pytest
from selenium import webdriver

@pytest.fixture()
def driver():
    d = webdriver.Chrome()  # 启动浏览器
    d.get("https://baidu.com")  # 控制浏览器,访问百度
    return d

使用fixtures

在测试用例的参数列表中,加上夹具名称,夹具就会被自动使用

python 复制代码
def test_b(self, driver):   # 测试用例,"driver"表示调用了conftest.py中的driver函数
	...
def test_c(self, driver):
	...  

fixtures共享范围

将启动浏览器设置为共享范围,使得多个测试用例调用driver时只启动一次浏览器。
创建夹具,可以指定夹具的共享范围,在共享范围内的用例,会共享同一个夹具

pytest的夹具共享范围有5个:

  • function(函数)(默认,最小)
  • class(类)
  • module(文件)
  • package(文件夹)
  • session(会话)
    每个fixture只能有一个范围

两个测试用例如果要使用同一个浏览器,只需要调整fixtures的范围;

上述用例处于同一个类,就可以将fixtures改为:
@pytest.fixture(scope="class")

python 复制代码
import pytest
from selenium import webdriver

@pytest.fixture(scope="class") # 共享范围为class
def driver():
    d = webdriver.Chrome()  # 启动浏览器
    d.get("https://baidu.com")  # 控制浏览器,访问百度
    return d

POM:对元素的封装复用

POM实现对页面的封装

Page Object Model:页面对象模型,通过面向对象的思想,封装页面的元素。

属性:

方法:自动化操作

相关推荐
zone77395 小时前
001:简单 RAG 入门
后端·python·面试
F_Quant5 小时前
🚀 Python打包踩坑指南:彻底解决 Nuitka --onefile 配置文件丢失与重启报错问题
python·操作系统
允许部分打工人先富起来6 小时前
在node项目中执行python脚本
前端·python·node.js
IVEN_6 小时前
Python OpenCV: RGB三色识别的最佳工程实践
python·opencv
haosend7 小时前
AI时代,传统网络运维人员的转型指南
python·数据网络·网络自动化
曲幽7 小时前
不止于JWT:用FastAPI的Depends实现细粒度权限控制
python·fastapi·web·jwt·rbac·permission·depends·abac
IVEN_1 天前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang1 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮1 天前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling1 天前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python