三、Web自动化测试(2)

一、Pytest框架了解

1. unittest

unittest 是 Python 的内置测试框架,受到了 Java 中 JUnit 的启发。它提供了一个基于类的测试框架,鼓励使用面向对象的方法编写测试用例。通常,测试用例被组织在测试类中,并且测试类继承自 unittest.TestCase 类。测试方法的命名通常以 test_ 开头。unittest 提供了丰富的断言方法,用于验证预期结果与实际结果是否一致。

2. pytest

pytest 是一个更加灵活和易用的测试框架,它不仅能够运行 unittest 风格的测试用例,还支持使用简洁的语法编写测试用例。相比于 unittestpytest 更加灵活,并且对测试用例的编写提供了更多的选择。pytest 通过自动发现测试文件和测试函数来运行测试,不需要显式继承特定的类或者按照特定的命名规范。此外,pytest 提供了丰富的插件生态系统,可以扩展其功能。

关系:

pytestunittest 都是 Python 的测试框架,它们之间并没有直接的父子关系或者继承关系。你可以选择使用其中的任何一个来编写和运行你的测试用例,具体选择取决于你的偏好、项目需求以及团队约定。有些团队可能更喜欢使用 unittest,因为它是 Python 的标准库之一,而有些团队可能更喜欢使用 pytest,因为它更加灵活且易于使用。

为什么使用pytest

  • 能够组织多用例执行

  • 提供丰富的断言方法: assert a==b (a一般表示预期结果, b一般表示实际结果)

  • 能够生成测试报告

二、基本使用

  1. pip install pytest==3.10.0
  2. pytest --version

  3. pip list | findstr "pytest"

运行方式:

  命令行运行

  pytest -s 文件名

  主函数运行

  在xxx.py文件中增加主函数:

if __name__ == '__main__':
    pytest.main(["-s", "xxx.py"])
# 主函数运行方式需要先导包 import pytest

 

断言:

assert xx 判断 xx 为真
assert not xx 判断 xx 不为真
assert a in b 判断 b 包含 a
assert a == b 判断 a 等于 b
assert a != b 判断 a 不等于 b

三、setup和teardown

1.初始化(前置处理方法):
    def setup(self)
2.销毁(后置处理方法):
    def teardown(self)
3.运行于测试方法的始末, 即:运行一次测试方法就会运行一次 setup 和 teardown
复制代码
import time

def add(x, y):
    return x + y

class TestPlus:
    # 获取并打印开始时间, 每个测试函数执行前都打印一次
    def setup(self):
        print("start-time:", time.time())

    # 获取并打印结束时间, 每个测试函数执行后都打印一次
    def teardown(self):
        print("end-time:", time.time())

    def test_a(self):
        assert 2 == add(1, 1)

    def test_b(self):
        assert add(1, 1) == 2
复制代码

 

四、配置文件

步骤:
1.新建 scripts 模块, 测试脚本放到模块中
2.新建 pytest.ini 文件, 名称为 pytest.ini, 第一行为 [pytest], 并且补全配置项
3.命令行运行 pytest 即可

 

# pytest.ini文件
[pytest] addopts
= -s testpaths = ./scripts python_files = test_*.py python_classes = Test* python_functions = test_*

 

五、数据参数化

# 数据参数化, 装饰器需要放在要传多组值的函数上
@pytest.mark.parametrize(argnames, argvalues)
# 参数:
    argnames: 参数名
    argvalues: 参数对应值, 类型必须是可迭代类型, 一般使用 list
复制代码
import pytest
class TestDemo:
# 需求: 不使用数据参数化, 分别打印用户名 "zhangsan" 和 "lisi"
    def test_a(self):
        print("zhangsan")

    def test_b(self):
        print("lisi")
# 需求: 使用数据参数化 (单一参数), 修改上面的代码
    @pytest.mark.parametrize("name", ["zhangsan", "lisi"])
    def test_c(self, name):
        print(name)
复制代码

 

@pytest.mark.parametrize 中,可以传递元组、列表、或者字典作为参数。

复制代码
import pytest
class TestDemo:
# 需求: 使用数据参数化 (推荐用法), 分别打印2组账号和密码: zhangsan / 111111 和 lisi / 222222
#     @pytest.mark.parametrize(("username", "password"), [("zhangsan", "111111"), ("lisi", "222222")])
#     def test_c(self, username, password):
#         print(username + "-----" + password)
    @pytest.mark.parametrize("dict", [{"username": "zhangsan", "password": "111111"}, {"username": "lisi", "password": "222222"}])
    def test_d(self, dict):
        print(dict)
        print(dict["username"])
        print(dict["password"])
#("zhangsan", "111111", "13000000000", "1", "1", "30", "......")
#("lisi", "222222", "13100000000", ??????)
# 推荐的用法是用字典表示参数值
#  {"username": "zhangsan", "password": "111111"}
复制代码
posted @   Mei_first  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示