老猿学自动化测试扫盲贴:自动化框架之selenium+pytest+python

在这里插入图片描述

1.概述

selenium:

基于JavaScript代码库的自动化测试框架,通过脚本语言,模拟用户行为操作,最接近用户真实场景,实现对web自动测试。Selenium,是目前的最火爆企业最主流的webUI自动化框架

pytest:

pytest是一个非常成熟的全功能的Python测试框架,是unittest框架的扩展,主要特点有以下几点:

⦁ 1、简单灵活,非常方便的组织自动化测试用例;

⦁ 2、支持参数化,可以细粒度地控制要测试的测试用例;

⦁ 3、能够支持简单的单元测试和复杂的功能测试,比如web端selenium/移动端appnium等自动化测试、request接口自动化测试

⦁ 4、pytest具有很多第三方插件,并且可以自定义扩展,比如测试报告生成,失败重运行机制

⦁ 5、测试用例的skip和fail处理;

⦁ 6、结合业界最美的测试报告allure+Jenkins,持续集成

2.环境搭建

pip install -U pytest

pytest-xdist #多线程

pip install -U pytest-rerunfailures #重试运行cases

pip install pytest-html #生成测试报告

pytest --version # 会展示当前已安装版本

3.编写规则

编写pytest测试样例非常简单,只需要按照下面的规则:

⦁ 测试文件以test_开头(以_test结尾也可以)

⦁ 测试类以Test开头,并且不能带有 init 方法

⦁ 测试函数以test_开头

⦁ 断言使用基本的assert即可

4.selenium+pytest案例实操

在这里插入图片描述

import pytest # 引入pytest包
 def test_a(): # test开头的测试函数
     print("------->test_a")
     assert 1 # 断言成功
 def test_b():
     print("------->test_b")
     assert 0 # 断言失败
 if __name__ == '__main__':
        pytest.main(['-s', 'class01.py'])

可以包含一个或多个Test开头的测试类,test_开头的函数

1.测试类主函数模式

pytest.main([’-s’, ‘test.py’])

2.命令行模式运行

pytest 文件路径/测试文件名

5. 扩展插件

5.1. 测试报告:pytest-html

pytest.main([’-s’,‘test.py’,’–html=./report/result.html’])

:表示当前路径下生成report文件夹,result.html文件

5.2 批量运行用例:pytest-xdist

pytest.main([’-s’,‘test.py’,’–html=./report/result.html’,’-n=2’])

-n=2 表示2个线程并发运行所有的测试用例

5.3 allure测试报告:allure-pytest

运行用例:pytest.main([’-s’, ‘-q’, ‘–alluredir’, ‘./report/xml’])

pytest 测试目标文件 --alluredir 数据目录

运行后的结果,是生成xml的数据集合

生成报告:allure generate --clean ./report/xml/ -o ./results/html/

第一个是数据集目录,第二个是生成报告目录

-o是指向目标生成测试报告的目录;

6.conftest配置

当自动化用例越来越庞大的时候,很多用例的数据可以共享,复用,让用例脚本可读性,维护性更高,比如登录等

conftest.py 配置里可以实现数据共享,比如py跨文件共享前置

conftest.py配置脚本名称是固定的,不能改名称

不需要import导入 conftest.py,pytest用例会自动查找

自从使用了pytest框架,爱了爱了,深深的感受到它的可扩展性和灵活性,老板再也不用担心我的自动化效率低!

在这里插入图片描述

最后:

欢迎大家关注公众号:程序员阿沐,暗号:CSDN整理好的软件测试大厂面试/技术资料分享。我们都是测试人!

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python+Selenium+pytest+PO模式是一种常用的Web UI自动化测试框架,下面是一些基本的步骤: 1. 安装pytestSelenium:使用pip install pytest selenium命令进行安装。 2. 创建pytest测试用例:创建一个py文件,使用pytest框架编写测试用例,可以使用Selenium进行web页面的操作和数据的验证。 3. 创建PO模式的页面对象:使用Page Object模式构建页面对象,将页面的元素和操作封装在一个类中,方便管理和维护。 4. 运行pytest测试:使用pytest运行测试用例,并查看测试报告。 5. 使用pytest插件:pytest提供了许多插件,例如pytest-html、pytest-xdist等,可以用来生成HTML测试报告、运行分布式测试等。 下面是一个简单的示例代码: ```python # conftest.py文件 from selenium import webdriver import pytest @pytest.fixture(scope='module') def driver(): driver = webdriver.Chrome() yield driver driver.quit() # test_login.py文件 from pages.login_page import LoginPage def test_login(driver): login_page = LoginPage(driver) login_page.open() login_page.input_username('test') login_page.input_password('test123') login_page.click_submit() assert login_page.get_login_result() == '登录成功' # login_page.py文件 from selenium.webdriver.common.by import By class LoginPage: url = 'http://localhost/login' def __init__(self, driver): self.driver = driver def open(self): self.driver.get(self.url) def input_username(self, username): self.driver.find_element(By.ID, 'username').send_keys(username) def input_password(self, password): self.driver.find_element(By.ID, 'password').send_keys(password) def click_submit(self): self.driver.find_element(By.ID, 'submit').click() def get_login_result(self): return self.driver.find_element(By.ID, 'result').text ``` 在上面的示例中,我们使用了pytest的fixture机制来管理WebDriver对象的生命周期,使用PO模式的页面对象来封装登录页面的元素和操作。我们可以通过执行pytest test_login.py命令来运行测试,并生成测试报告。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值