最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytest自動化測試數(shù)據(jù)驅(qū)動yaml/excel/csv/json

 更新時(shí)間:2022年06月27日 14:21:01   作者:吱吱菌啦啦  
這篇文章主要為大家介紹了pytest自動化測試數(shù)據(jù)驅(qū)動yaml/excel/csv/json的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

數(shù)據(jù)驅(qū)動

數(shù)據(jù)的改變從而驅(qū)動自動化測試用例的執(zhí)行,最終引起測試結(jié)果的改變。簡單說就是參數(shù)化的應(yīng)用。

測試驅(qū)動在自動化測試中的應(yīng)用場景:

  • 測試步驟的數(shù)據(jù)驅(qū)動;
  • 測試數(shù)據(jù)的數(shù)據(jù)驅(qū)動;
  • 配置的數(shù)據(jù)驅(qū)動;

1、pytest結(jié)合數(shù)據(jù)驅(qū)動-yaml

實(shí)現(xiàn)讀yaml文件,先創(chuàng)建env.yml文件配置測試數(shù)據(jù)

工程目錄結(jié)構(gòu):

  • data目錄:存放yaml文件
-
  dev: 127.0.0.1
  #dev: 127.0.0.2
  #prod: 127.0.0.3
  • testcase目錄:存放測試用例文件
import pytest
import yaml
class TestYaml:
    @pytest.mark.parametrize("env", yaml.safe_load(open("./env.yml")))
    def test_yaml(self, env):
        if "test" in env:
            print("這是測試環(huán)境")
            # print(env)
            print("測試環(huán)境的ip是:", env["test"])
        elif "dev" in env:
            print("這是開發(fā)文件")
            print("開發(fā)環(huán)境的ip是:", env["dev"])
            # print(env)

結(jié)果示例:

2、pytest結(jié)合數(shù)據(jù)驅(qū)動-excel

常用的讀取方式有:xlrd、xlwings、pandas、openpyxl

以讀excel文件,實(shí)現(xiàn)A+B=C并斷言為例~

工程目錄結(jié)構(gòu):

data目錄:存放excel數(shù)據(jù)文件

  • func目錄:存放被測函數(shù)文件
def my_add(x, y):
    result = x + y
    return result
  • testcase目錄:存放測試用例文件
import openpyxl
import pytest
from test_pytest.read_excel.func.operation import my_add
def test_get_excel():
    """
    解析excel數(shù)據(jù)
    :return: [[1,1,2],[3,6,9],[100,200,300]]
    """
    book = openpyxl.load_workbook('../data/param.xlsx')
    sheet = book.active
    cells = sheet["A1":"C3"]
    print(cells)
    values = []
    for row in sheet:
        data = []
        for cell in row:
            data.append(cell.value)
        values.append(data)
    print(values)
    return values
class TestWithExcel:
    @pytest.mark.parametrize('x,y,expected', test_get_excel())
    def test_add(self, x, y, expected):
        assert my_add(int(x), int(y)) == int(expected)

3、pyetst結(jié)合數(shù)據(jù)驅(qū)動-csv

csv:逗號文件,以逗號分隔的string文件

讀取csv數(shù)據(jù):

  • 內(nèi)置函數(shù)open()
  • 內(nèi)置模塊csv
  • 方法:csv.reader(iterable)
  • 參數(shù):iterable,文件或列表對象
  • 返回:迭代器,遍歷迭代器,每次會返回一行數(shù)據(jù)

以讀csv文件,實(shí)現(xiàn)A+B=C并斷言為例~

工程目錄結(jié)構(gòu):

data目錄:存放csv數(shù)據(jù)文件

  • func目錄:存放被測函數(shù)文件
def my_add(x, y):
    result = x + y
    return result
  • testcase目錄:存放測試用例文件
import csv
import pytest
from test_pytest.read_csv.func.operation import my_add
def test_get_csv():
    """
    解析csv文件
    :return:
    """
    with open('../data/params.csv') as file:
        raw = csv.reader(file)
        data = []
        for line in raw:
            data.append(line)
    print(data)
    return data
class TestWithCsv:
    @pytest.mark.parametrize('x,y,expected', test_get_csv())
    def test_add(self, x, y, expected):
        assert my_add(int(x), int(y)) == int(expected)

4、pytest結(jié)合數(shù)據(jù)驅(qū)動-json

json:js對象,是一種輕量級的數(shù)據(jù)交換格式。

json結(jié)構(gòu):

  • 對象{"key":value}
  • 數(shù)組[value1,value2...]

查看json文件:

  • 1.pycharm
  • 2.txt記事本

讀取json文件:

  • 內(nèi)置函數(shù)open()
  • 內(nèi)置庫json
  • 方法 json.loads() json.dumps()

以讀json文件,實(shí)現(xiàn)A+B=C并斷言為例~

工程目錄結(jié)構(gòu):

data目錄:存放json數(shù)據(jù)文件

  • func目錄:存放被測函數(shù)文件
def my_add(x, y):
    result = x + y
    return result
  • testcase目錄:存放測試用例文件
import json
import pytest
from test_pytest.read_json.func.operation import my_add
def test_get_json():
    """
    解析json數(shù)據(jù)
    :return: [[1,1,2],[3,6,9],[100,200,300]]
    """
    with open('../data/params.json', 'r') as file:
        data = json.loads(file.read())
        print(list(data.values()))
        return list(data.values())
class TestWithJson:
    @pytest.mark.parametrize('x,y,expected', test_get_json())
    def test_add(self, x, y, expected):
        assert my_add(int(x), int(y)) == int(expected)

以上就是pytest自動化測試數(shù)據(jù)驅(qū)動yaml/excel/csv/json的詳細(xì)內(nèi)容,更多關(guān)于pytest測試數(shù)據(jù)驅(qū)動yaml/excel/csv/json的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

湘乡市| 安达市| 上高县| 花莲县| 上犹县| 拉孜县| 慈利县| 阿克陶县| 台中市| 南平市| 吐鲁番市| 厦门市| 三穗县| 桦川县| 九龙县| 东丽区| 尤溪县| 桂阳县| 西宁市| 响水县| 岫岩| 高州市| 宣武区| 枣庄市| 彩票| 昔阳县| 仙游县| 天长市| 宜都市| 剑河县| 革吉县| 旬邑县| 沾益县| 缙云县| 新巴尔虎左旗| 原平市| 棋牌| 福贡县| 溧水县| 涞源县| 富阳市|