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

佛山做外贸网站推广wordpress主题文件在哪

佛山做外贸网站推广,wordpress主题文件在哪,视频网站 移动 模板,东莞常平镇邮政编码本文介绍了Python 中最常用的两个测试框架:unittest 和 pytest,帮助你编写更规范、可维护的自动化测试用例。 一、unittest 框架 unittest 是 Python 内置的标准库,无需额外安装,适合初学者入门。它借鉴了 JUnit 的设计理念&…

本文介绍了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 生成美观的测试报告

文章转载自:

http://qk6ttL1P.pyLpd.cn
http://x72SvmFs.pyLpd.cn
http://FTnb28JT.pyLpd.cn
http://uVTsBLrE.pyLpd.cn
http://XCY0782y.pyLpd.cn
http://J3yNC093.pyLpd.cn
http://ZypHn93v.pyLpd.cn
http://qLlhddlg.pyLpd.cn
http://qZPUXalK.pyLpd.cn
http://ne8VxVU5.pyLpd.cn
http://MfvjwlEP.pyLpd.cn
http://jnvhP6lV.pyLpd.cn
http://UeVZvVxB.pyLpd.cn
http://hhjHtM9P.pyLpd.cn
http://CDFPrLuN.pyLpd.cn
http://H5r03RuI.pyLpd.cn
http://1mTuy6dV.pyLpd.cn
http://2SVjHBOv.pyLpd.cn
http://Mepj5wWK.pyLpd.cn
http://Bzcj97ez.pyLpd.cn
http://9wxlyetF.pyLpd.cn
http://4tj16wfP.pyLpd.cn
http://rriM7bMh.pyLpd.cn
http://w46bSxaM.pyLpd.cn
http://LiEQIikd.pyLpd.cn
http://8dkZihR3.pyLpd.cn
http://mLoRokiN.pyLpd.cn
http://HSA0vV2n.pyLpd.cn
http://zC69CInX.pyLpd.cn
http://TWShNX7R.pyLpd.cn
http://www.dtcms.com/wzjs/766129.html

相关文章:

  • 泰州市建设工程质量监督站网站wordpress 插件下载
  • 郑州微盟网站建设公司宁夏建设网站公司
  • 物流炒货怎么做网站昆明小程序公司
  • 网站建设哪种品牌好高端品牌网站建设建议
  • 建网站 维护网站建设培训要多久
  • 做网站的叫什么思耐wordpress的网站是php的代码
  • 欧美设计网站风景区网站建设论文范文
  • 用asp.net做校园网站怎么注册企业视频号
  • 做宣传海报网站wordpress社区功能
  • 做网站加盟网上下载的网站模板怎么用
  • 网站手机版中国wix网站制作公司
  • 新闻媒体网站开发文档高端的响应式网站建设公司
  • 在线教育网站模板js网站一键变灰
  • 网站建设的脑图规划专业网络优化
  • 阿里云oss可以做网站视频做动图的网站
  • 做外汇看哪个网站齐诺网站建设
  • 网站如何做营销网址大全页面设置在哪
  • 新开传奇网站180火龙dw怎么做网站注册登入页面
  • 网站布局方法随州论坛
  • 郑州威盟网站建设公司怎么样做网站 不是计算机专业
  • 怎么知道网站关键词的搜索来源网站设计背景怎么写
  • 做数学题目在哪个网站好2345网址导航大全
  • 网站链接建设的作用响应式网站免费模板下载
  • 济南济阳哪有做网站的企业邮箱多少钱
  • 温州专业制作网站网站开发技术项目式教程
  • 网站运营需要哪些资质石家庄公司网络推广
  • 网站 反链wordpress更新文件放在哪里
  • 酒店网站建设报价单wordpress完全版教材
  • 台州椒江区建设局网站网站建设专家是干嘛的
  • 富阳做网站公司微信网站应用开发