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

【软件测试】测试框架(unittest/pytest)

本文介绍了Python 中最常用的两个测试框架:unittest 和 pytest,帮助你编写更规范、可维护的自动化测试用例。

一、unittest 框架

unittest 是 Python 内置的标准库,无需额外安装,适合初学者入门。它借鉴了 JUnit 的设计理念,提供了测试用例、测试套件、断言等基本功能。

1. 基本概念
  • TestCase:测试用例的基类,每个测试方法必须以 test_ 开头
  • TestSuite:测试套件,用于组织多个测试用例
  • TestRunner:测试运行器,执行测试并生成结果
  • Assertion:断言,验证测试结果
2. 简单示例

下面是一个使用 unittest 测试登录功能的示例:

import unittest
from selenium import webdriver
from selenium.webdriver.common.by import Byclass TestLogin(unittest.TestCase):def setUp(self):# 每个测试方法执行前运行,初始化浏览器self.driver = webdriver.Firefox()self.driver.get('https://example.com/login')self.driver.implicitly_wait(10)def tearDown(self):# 每个测试方法执行后运行,关闭浏览器self.driver.quit()def test_successful_login(self):# 测试成功登录的场景username = self.driver.find_element(By.ID, 'username')password = self.driver.find_element(By.ID, 'password')login_button = self.driver.find_element(By.ID, 'login-button')username.send_keys('valid_username')password.send_keys('valid_password')login_button.click()# 断言登录成功后页面上存在欢迎消息welcome_message = self.driver.find_element(By.CLASS_NAME, 'welcome-message')self.assertIn('欢迎', welcome_message.text)def test_failed_login(self):# 测试失败登录的场景username = self.driver.find_element(By.ID, 'username')password = self.driver.find_element(By.ID, 'password')login_button = self.driver.find_element(By.ID, 'login-button')username.send_keys('invalid_username')password.send_keys('invalid_password')login_button.click()# 断言错误消息存在error_message = self.driver.find_element(By.CLASS_NAME, 'error-message')self.assertIn('用户名或密码错误', error_message.text)if __name__ == '__main__':unittest.main()
3. 常用断言方法
断言方法作用
assertEqual(a, b)验证 a == b
assertNotEqual(a, b)验证 a != b
assertTrue(x)验证 x 为 True
assertFalse(x)验证 x 为 False
assertIn(a, b)验证 a 在 b 中
assertNotIn(a, b)验证 a 不在 b 中
assertIsNone(x)验证 x 为 None
assertIsNotNone(x)验证 x 不为 None

二、pytest 框架

pytest 是第三方测试框架,功能更强大,插件丰富,语法更简洁,是目前最流行的 Python 测试框架。

1. 安装

pip install pytest

2. 基本概念
  • 测试函数:以 test_ 开头的普通函数
  • 测试类:以 Test 开头的类,其中的方法以 test_ 开头
  • fixture:用于测试环境的初始化和清理
  • 参数化测试:一次编写,多次执行不同参数的测试
3. 简单示例

下面是使用 pytest 和 fixture 实现的登录测试:

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By@pytest.fixture
def browser():# 初始化浏览器driver = webdriver.Firefox()driver.implicitly_wait(10)yield driver  # 返回driver给测试函数# 测试结束后关闭浏览器driver.quit()def test_successful_login(browser):browser.get('https://example.com/login')username = browser.find_element(By.ID, 'username')password = browser.find_element(By.ID, 'password')login_button = browser.find_element(By.ID, 'login-button')username.send_keys('valid_username')password.send_keys('valid_password')login_button.click()welcome_message = browser.find_element(By.CLASS_NAME, 'welcome-message')assert '欢迎' in welcome_message.textdef test_failed_login(browser):browser.get('https://example.com/login')username = browser.find_element(By.ID, 'username')password = browser.find_element(By.ID, 'password')login_button = browser.find_element(By.ID, 'login-button')username.send_keys('invalid_username')password.send_keys('invalid_password')login_button.click()error_message = browser.find_element(By.CLASS_NAME, 'error-message')assert '用户名或密码错误' in error_message.text
4. 参数化测试

pytest 最强大的功能之一是参数化测试,可以使用 @pytest.mark.parametrize 装饰器:

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By@pytest.fixture
def browser():driver = webdriver.Firefox()yield driverdriver.quit()@pytest.mark.parametrize("username, password, expected_message",[("valid_user1", "valid_pass1", "欢迎"),  # 测试用例1("valid_user2", "valid_pass2", "欢迎"),  # 测试用例2("invalid_user", "wrong_password", "用户名或密码错误"),  # 测试用例3]
)
def test_login_parameterized(browser, username, password, expected_message):browser.get('https://example.com/login')username_field = browser.find_element(By.ID, 'username')password_field = browser.find_element(By.ID, 'password')login_button = browser.find_element(By.ID, 'login-button')username_field.send_keys(username)password_field.send_keys(password)login_button.click()if "valid" in username:message = browser.find_element(By.CLASS_NAME, 'welcome-message').textelse:message = browser.find_element(By.CLASS_NAME, 'error-message').textassert expected_message in message
5. 运行测试

在终端中执行测试:

# 运行当前目录下所有以test_开头的文件
pytest# 运行指定文件
pytest test_login.py# 显示详细输出
pytest -v# 生成HTML测试报告
pytest --html=report.html

三、unittest vs pytest 对比

特性unittestpytest
断言方式内置断言方法使用 Python 原生 assert 语句
测试发现基于类和方法命名规则更灵活,支持更多命名模式
参数化测试需要使用第三方库内置参数化功能
测试夹具setUp/tearDown 方法灵活的 fixture 机制
插件生态较少丰富的插件(如测试报告、并行执行)
代码简洁度较繁琐简洁易读

四、推荐实践

  1. 使用 pytest:对于大多数项目,推荐使用 pytest,它的灵活性和丰富插件能大大提高效率
  2. 合理使用 fixture:将浏览器初始化、登录等操作封装到 fixture 中,避免代码重复
  3. 参数化测试:使用参数化覆盖多种测试场景,减少代码量
  4. 生成测试报告:使用 pytest-html 或 allure-pytest 生成美观的测试报告

相关文章:

  • StarRocks部署方案详解:从单机到分布式集群
  • 数据库系统概论(十三)详细讲解SQL中数据更新(插入,修改与更新)
  • SpringBoot-配置Spring MVC
  • 掌握HttpClient技术:从基础到实战(Apache)
  • 【Redis】Set 集合
  • AI Agent企业级生产应用全解析
  • Flask中secret_key设置解析
  • 振动分析师(ISO18436-2)四级能力矩阵 - 简介
  • 齐次变换矩阵与运动旋量的指数映射
  • 如何学习才能更好地理解人工智能工程技术专业和其他信息技术专业的关联性?
  • Vue3(watch,watchEffect,标签中ref的使用,TS,props,生命周期)
  • DAY43打卡
  • Github 2025-06-02 开源项目周报 Top11
  • 效率办公Office 2003-2024网盘下载与安装教程指南
  • 【Pytorch学习笔记】模型模块07——hook实现Grad-CAM
  • pbootcms 搜索自定义字段模糊、精准搜索
  • 如何自定义WordPress主题(5个分步教程)
  • Pycharm的终端无法使用Anaconda命令行问题详细解决教程
  • 【CVE-2025-4123】Grafana完整分析SSRF和从xss到帐户接管
  • 字节跳动开源图标库:2000+图标一键换肤的魔法
  • 新疆建设兵团建工集团官方网站/seo管理
  • 云南网络宣传公司/谷歌seo优化
  • 做网站设计的公司排名/seo管理系统
  • 急招平面设计3名/百度seo新算法
  • 网站推广怎么做优化/谷歌seo是什么意思
  • java怎么做直播网站/b站网站推广mmm