python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python DDT数据驱动

python中DDT数据驱动的实现

作者:徐田垚

DDT是一种软件测试方法,本文主要介绍了python中DDT数据驱动的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

DDT(Data-Driven Testing,数据驱动测试)是一种软件测试方法,通过外部数据源(如Excel、CSV、数据库等)驱动测试用例的执行。它的核心思想是将测试数据与测试逻辑分离,从而提高测试的灵活性和可维护性。

以下是关于DDT的详细介绍和实现方法:

1. DDT 的核心概念

2. DDT 的优点

3. DDT 的实现方法

以下是使用 Python 实现 DDT 的几种常见方式:

方法 1: unittest 和 ddt 库

ddt 是一个 Python 库,专门用于数据驱动测试。

安装 ddt

pip install ddt

示例代码1:

import unittest
from ddt import ddt, data, unpack

@ddt
class TestMathOperations(unittest.TestCase):

    @data((1, 2, 3), (4, 5, 9), (10, -5, 5))
    @unpack
    def test_addition(self, a, b, expected_result):
        self.assertEqual(a + b, expected_result)

if __name__ == "__main__":
    unittest.main()

运行结果:

示例代码2:

import requests
import unittest
from ddt import ddt, data
test_data = [
    {'method':'post', 'url':'http://www.jd.com'},
    {'method':'put', 'url':'http://www.jd.com'},
    {'method':'put', 'url':'http://www.jd.com'},
]
@ddt
class Test(unittest.TestCase):
    @data(*test_data)
    def test01(self, case):
        print('请求', type(case))
        res = requests.request(method=case['method'], url=case['url'])
        print(res)


if __name__ == '__main__':
    unittest.main()

运行结果:

请求 <class 'dict'>
<Response [200]>
请求 <class 'dict'>
<Response [200]>
请求 <class 'dict'>

<Response [200]>
Ran 3 tests in 0.423s

OK

方法 2:使用 pytest 和参数化

pytest 是一个功能强大的测试框架,支持数据驱动测试。

示例代码:

import pytest

# 测试数据
test_data = [
    (1, 2, 3),
    (4, 5, 9),
    (10, -5, 5)
]

@pytest.mark.parametrize("a, b, expected_result", test_data)
def test_addition(a, b, expected_result):
    assert a + b == expected_result

运行结果:

每组数据会生成一个独立的测试用例。

方法 3:从外部文件加载数据

可以从 CSV、Excel 或 JSON 文件中加载测试数据。

从 CSV 文件加载数据:

import csv
import pytest

def load_test_data_from_csv(file_path):
    test_data = []
    with open(file_path, newline='') as csvfile:
        reader = csv.reader(csvfile)
        next(reader)  # 跳过表头
        for row in reader:
            test_data.append(tuple(map(int, row)))  # 将数据转换为整数
    return test_data

test_data = load_test_data_from_csv("test_data.csv")

@pytest.mark.parametrize("a, b, expected_result", test_data)
def test_addition(a, b, expected_result):
    assert a + b == expected_result

CSV 文件示例 (test_data.csv):

a,b,expected_result
1,2,3
4,5,9
10,-5,5

从 JSON 文件加载数据:

import json
import pytest

def load_test_data_from_json(file_path):
    with open(file_path) as f:
        return json.load(f)

test_data = load_test_data_from_json("test_data.json")

@pytest.mark.parametrize("data", test_data)
def test_addition(data):
    assert data["a"] + data["b"] == data["expected_result"]

JSON 文件示例 (test_data.json):

[
    {"a": 1, "b": 2, "expected_result": 3},
    {"a": 4, "b": 5, "expected_result": 9},
    {"a": 10, "b": -5, "expected_result": 5}
]

4. DDT 的最佳实践

5. DDT 的应用场景

到此这篇关于python中DDT数据驱动的实现的文章就介绍到这了,更多相关python DDT数据驱动内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文