pytest 简介2

1. pytest 中的 DDT,使用格式:@pytest.mark.parametrize()

import pytest

test_data = [{"name":"jike1"},{"name":"jike2"}]

#第一个参数接受数据,并传给后面的函数
@pytest.mark.parametrize("datas", test_data)
def test_get_names(datas):
    name = datas["name"]
    print(f"姓名有:{name}")

2. pytest 中的重跑机制,当用例执行失败以后,设置重跑

  1. 安装插件, pip  install pytest-rerunfailures
  2. 执行命令中加 “pytest -s --reruns 2 --reruns-delay 3 ”,表示用例运行失败后,失败用例重运行2次,每次间隔3s钟
  3. 注意:重运行2次,一共需要运行3次

3. fixture 测试夹具

  1. 相当于 unittest 中 setUp 和 tearDown 操作
  2. 在项目目录下建立一个 conftest.py 文件,文件名称不可以修改
  3. 将 setUp 和 tearDown 操作写到一个函数中,setUp 和 tearDown 之间的用 yield xxx 生成器,表示 setUp 操作完后返回调用函数执行函数体,执行完以后,再返回来执行 yield 后面的操作
  4. 方法上加装饰器,@pytest.fixture()
#conftest.py文件内容


# pytest.fixture()中
# 1. 参数为空,即默认是 scope='function',相当于 unittest 中的setUp,tearDown
# 2. 参数是 scope = 'class',相当于 unittest 中的 setUpClass,tearDownClass
# 3. 参数是 scope = 'module', 表示整个模块只调用一次
# 4. 参数是 scope = 'package', 表示整个包只调用一次
@pytest.fixture()
def setup_and_teardown():
    driver = webdriver.Chrome()
    driver.implicitly_wait(5)

    #yiled 相当于return,只是不退出程序,先返回并执行调用该方法的程序,然后再折回继续执行 yiled 后面的程序
    yield driver

    driver.quit()

 

import pytest

test_data = [{"name":"jike1"},{"name":"jike2"}]

#第一个参数接受数据,并传给后面的函数
@pytest.mark.test
@pytest.mark.parametrize("datas", test_data)
# 测试夹具中的函数名“setup_and_teardown”作为 参数 传给调用函数
def test_get_names(datas, setup_and_teardown):
    name = datas["name"]
    assert name == "jike1"

4. 生成 allure 报告

  1. 安装 allure,下载https://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.13.2/,下载完解压到想要的路径,再将bin路径添加到系统路径
  2. 安装 pytest-allure, pip install pytest-allure
  3. 执行命令 pytest --alluredir = allure_report, 在项目目录下生成一个 allure_report 文件夹,里面存的是 json 格式文档
  4. 执行 allure serve allure_report,自动生成 allure 报告
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值