• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pytest框架进阶自学系列 | fixture应用在初始化设置

武飞扬头像
热爱编程的通信人
帮助1

书籍来源:房荔枝 梁丽丽《pytest框架与自动化测试应用》

一边学习一边整理老师的课程内容及实验笔记,并与大家分享,侵权即删,谢谢支持!


初始化过程一般进行数据初始化、连接初始化等。

常用场景:测试用例执行时,有的用例的数据是可读取的,需要把数据读进来再执行测试用例。setup和teardown可以实现。fixture可以灵活命名实现。

具体实现步骤:

(1)导入pytest。

(2)创建data()函数。

(3)在data()函数上加@pytest.fixture()。

(4)在要使用的测试方法test_login中传入(data函数名称),也就是先执行data()函数再执行测试方法。

(5)不传入参数表明可以直接执行测试方法。

代码如下:

  1.  
    import pytest
  2.  
    import csv
  3.  
     
  4.  
    @pytest.fixture()
  5.  
    def data():
  6.  
    test_data = {'name': 'linda', 'age': 18}
  7.  
    return test_data
  8.  
     
  9.  
    def test_login(data):
  10.  
    name = data['name']
  11.  
    age = data['age']
  12.  
    print("笔者的名字叫:{},今年{}。".format(name, age))

如果测试数据是从csv文件中读取的,执行操作步骤如下:

(1)新建userinfo.csv文件,代码如下:

  1.  
    username,age
  2.  
    linda,18
  3.  
    tom,8
  4.  
    steven,28

(2)在test_fixture_data.py中增加代码如下:

  1.  
    import pytest
  2.  
    import csv
  3.  
     
  4.  
    @pytest.fixture()
  5.  
    def data():
  6.  
    test_data = {'name': 'linda', 'age': 18}
  7.  
    return test_data
  8.  
     
  9.  
    def test_login(data):
  10.  
    name = data['name']
  11.  
    age = data['age']
  12.  
    print("笔者的名字叫:{},今年{}。".format(name, age))
  13.  
     
  14.  
    @pytest.fixture()
  15.  
    def read_data():
  16.  
    with open('userinfo.csv') as f:
  17.  
    row = csv.reader(f, delimiter=',')
  18.  
    next(row)
  19.  
    users = []
  20.  
    for r in row:
  21.  
    users.append(r)
  22.  
    return users
  23.  
     
  24.  
    def test_logins(read_data):
  25.  
    name = read_data[0][0]
  26.  
    age = read_data[0][1]
  27.  
    print("s笔者的名字叫:{},今年{}。".format(name, age))
学新通

(3)鼠标右击选择pytest执行。

  1.  
    D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-3/test_fixture_data.py
  2.  
    Testing started at 11:21 ...
  3.  
    Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-3/test_fixture_data.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-3
  4.  
     
  5.  
    ============================= test session starts =============================
  6.  
    platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
  7.  
    cachedir: .pytest_cache
  8.  
    rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book
  9.  
    collecting ... collected 2 items
  10.  
     
  11.  
    test_fixture_data.py::test_login PASSED [ 50%]笔者的名字叫:linda,今年18
  12.  
     
  13.  
    test_fixture_data.py::test_logins PASSED [100%]s笔者的名字叫:linda,今年18
  14.  
     
  15.  
     
  16.  
    ============================== 2 passed in 0.01s ==============================
  17.  
     
  18.  
    Process finished with exit code 0
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhigkfjb
系列文章
更多 icon
同类精品
更多 icon
继续加载