2023年是AI大模型的元年,2024年将会是AI产品的爆发年,越来越多的创新将会涌现,其中,AI在软件测试领域得到了广泛的应用与实践。

人工智能可以帮助测试工程师自动执行重复性高的测试用例,分析大量测试结果进行预测,并自动生成测试报告与缺陷列表。这大大提高了测试效率,缩短了测试周期。

一、测试用例的自动生成

 AI技术可以通过讯飞星火,chatGPT等工具,根据测试工程师提供的提示词生成功能测试用例。测试工程师可以提供需求关键词和预期结果,然后讯飞星火,chatGPT可以根据这些信息编写测试用例,包括输入操作并验证结果是否符合预期。

1、功能测试

举例:登录模块的测试用例编写

针对软件测试行业,讨论AIGC技术的发展_测试用例

以下内容为AI生成:

编写登录模块的测试用例需要考虑各种可能的情况,包括正常情况、异常情况和边界条件。以下是一些常见的测试用例:

  1. 正确的用户名和密码


def test_login_success(client):
    # 假设 client 是一个已经初始化好的客户端对象,可以用来发送请求
    response = client.post('/login', data={'username': 'testuser', 'password': 'testpass'})
    assert response.status_code == 200
    assert response.json()['message'] == 'Login successful'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.


  1. 错误的用户名或密码


def test_login_failure_wrong_credentials(client):
    response = client.post('/login', data={'username': 'wronguser', 'password': 'wrongpass'})
    assert response.status_code == 401
    assert response.json()['message'] == 'Invalid credentials'
  • 1.
  • 2.
  • 3.
  • 4.


  1. 空的用户名或密码


def test_login_failure_empty_fields(client):
    response = client.post('/login', data={'username': '', 'password': ''})
    assert response.status_code == 400
    assert response.json()['message'] == 'Username and password are required'
  • 1.
  • 2.
  • 3.
  • 4.


  1. 用户名不存在


def test_login_failure_nonexistent_user(client):
    response = client.post('/login', data={'username': 'nonexistent', 'password': 'testpass'})
    assert response.status_code == 401
    assert response.json()['message'] == 'User does not exist'
  • 1.
  • 2.
  • 3.
  • 4.


  1. 密码错误


def test_login_failure_wrong_password(client):
    response = client.post('/login', data={'username': 'testuser', 'password': 'wrongpass'})
    assert response.status_code == 401
    assert response.json()['message'] == 'Invalid credentials'
  • 1.
  • 2.
  • 3.
  • 4.


  1. 用户名或密码过长(超过最大长度限制)


def test_login_failure_long_input(client):
    long_username = 'a' * 256
    long_password = 'b' * 256
    response = client.post('/login', data={'username': long_username, 'password': long_password})
    assert response.status_code == 400
    assert response.json()['message'] == 'Input too long'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.


2、接口测试

举例:

针对软件测试行业,讨论AIGC技术的发展_测试用例_02

以下内容为AI生成:

接口测试用例设计需要考虑多种输入情况和预期输出,以确保接口的功能正确性和稳定性。以下是根据提供的接口信息设计的接口测试用例:

  1. 正常输入测试用例
  • 描述:当请求体包含有效的角色信息时,应返回正确的角色列表。
  • 输入:{"roleId": "1", "roleName": "Admin"}
  • 预期输出:返回包含角色信息的JSON数组,例如:[{"id": "1", "name": "Admin"}, {"id": "2", "name": "User"}]
  1. 无效输入测试用例
  • 描述:当请求体包含无效的角色信息时,应返回错误信息。
  • 输入:{"roleId": "", "roleName": ""}
  • 预期输出:返回错误信息,例如:{"error": "Invalid role information"}
  1. 边界条件测试用例
  • 描述:当请求体包含最大长度的角色信息时,应返回正确的角色列表。
  • 输入:{"roleId": "1234567890", "roleName": "VeryLongRoleNameThatExceedsTheLimit"}
  • 预期输出:返回包含角色信息的JSON数组,例如:[{"id": "1234567890", "name": "VeryLongRoleNameThatExceedsTheLimit"}]
  1. 异常情况测试用例
  • 描述:当请求体包含非法字符或特殊字符时,应返回错误信息。
  • 输入:{"roleId": "", "roleName": ""}
  • 预期输出:返回错误信息,例如:{"error": "Invalid characters in role information"}
  1. 空请求体测试用例
  • 描述:当请求体为空时,应返回错误信息。
  • 输入:{}
  • 预期输出:返回错误信息,例如:{"error": "Empty request body"}
  1. 缺少必要字段测试用例
  • 描述:当请求体缺少必要的字段时,应返回错误信息。
  • 输入:{"roleId": "1"}
  • 预期输出:返回错误信息,例如:{"error": "Missing role name"}
  1. 超时测试用例
  • 描述:当服务器处理请求时间超过预设阈值时,应返回超时错误信息。
  • 输入:{"roleId": "1", "roleName": "Admin"}
  • 预期输出:返回错误信息,例如:{"error": "Request timed out"}
  1. 并发测试用例
  • 描述:当多个请求同时发送到服务器时,应正确处理并发请求并返回正确的结果。
  • 输入:多个不同的请求体,例如:[{"roleId": "1", "roleName": "Admin"}, {"roleId": "2", "roleName": "User"}, ...]
  • 预期输出:每个请求都返回正确的角色列表或错误信息。

以上是一些基本的接口测试用例设计,可以根据实际需求和接口的具体实现进行调整和扩展。

3、生成代码

举例:

针对软件测试行业,讨论AIGC技术的发展_测试用例_03

以下内容为AI生成:

以下是将上述接口测试用例转化为Python代码的示例:


import requests

# 正常输入测试用例
def test_normal_input():
    url = "http://ox/Role/getRolesBylenant"
    headers = {"Content-Type": "application/json"}
    data = {"roleId": "1", "roleName": "Admin"}
    response = requests.post(url, json=data, headers=headers)
    assert response.status_code == 200
    assert response.json() == [{"id": "1", "name": "Admin"}, {"id": "2", "name": "User"}]

# 无效输入测试用例
def test_invalid_input():
    url = "http://ox/Role/getRolesBylenant"
    headers = {"Content-Type": "application/json"}
    data = {"roleId": "", "roleName": ""}
    response = requests.post(url, json=data, headers=headers)
    assert response.status_code == 400
    assert response.json() == {"error": "Invalid role information"}

# 边界条件测试用例
def test_boundary_condition():
    url = "http://ox/Role/getRolesBylenant"
    headers = {"Content-Type": "application/json"}
    data = {"roleId": "1234567890", "roleName": "VeryLongRoleNameThatExceedsTheLimit"}
    response = requests.post(url, json=data, headers=headers)
    assert response.status_code == 200
    assert response.json() == [{"id": "1234567890", "name": "VeryLongRoleNameThatExceedsTheLimit"}]

# 异常情况测试用例
def test_exceptional_case():
    url = "http://ox/Role/getRolesBylenant"
    headers = {"Content-Type": "application/json"}
    data = {"roleId": "<script>alert('XSS')</script>", "roleName": "<img src='http://example.com/malicious.jpg'>"}
    response = requests.post(url, json=data, headers=headers)
    assert response.status_code == 400
    assert response.json() == {"error": "Invalid characters in role information"}

# 空请求体测试用例
def test_empty_request_body():
    url = "http://ox/Role/getRolesBylenant"
    headers = {"Content-Type": "application/json"}
    data = {}
    response = requests.post(url, json=data, headers=headers)
    assert response.status_code == 400
    assert response.json() == {"error": "Empty request body"}

# 缺少必要字段测试用例
def test_missing_required_field():
    url = "http://ox/Role/getRolesBylenant"
    headers = {"Content-Type": "application/json"}
    data = {"roleId": "1"}
    response = requests.post(url, json=data, headers=headers)
    assert response.status_code == 400
    assert response.json() == {"error": "Missing role name"}

# 超时测试用例(需要根据实际情况调整超时时间)
def test_timeout():
    url = "http://ox/Role/getRolesBylenant"
    headers = {"Content-Type": "application/json"}
    data = {"roleId": "1", "roleName": "Admin"}
    try:
        response = requests.post(url, json=data, headers=headers, timeout=1)
        assert False, "Expected a timeout error"
    except requests.exceptions.Timeout:
        assert True

# 并发测试用例(需要根据实际情况调整并发数和请求内容)
def test_concurrency():
    import threading
    url = "http://ox/Role/getRolesBylenant"
    headers = {"Content-Type": "application/json"}
    data_list = [{"roleId": "1", "roleName": "Admin"}, {"roleId": "2", "roleName": "User"}, ...]
    results = []

    def send_request(data):
        response = requests.post(url, json=data, headers=headers)
        results.append(response.json())

    threads = []
    for data in data_list:
        thread = threading.Thread(target=send_request, args=(data,))
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()

    assert len(results) == len(data_list)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.


二、测试结果的智能分析

测试报告自动生成。根据测试结果与日志,采用自然语言生成技术可以自动生成测试报告。这消除了人工编写测试报告的工作,并具有一定的条理和优势。但自动生成的报告某些表达可能不够准确与丰富,还需要人工进行校验与修正。

举例:

针对软件测试行业,讨论AIGC技术的发展_测试用例_04

以下内容为AI生成:

以下是针对上述接口测试用例的测试报告:

接口测试报告

1. 测试概述

本次测试旨在验证“http://ox/Role/getRolesBylenant”接口的功能正确性和稳定性。测试范围包括正常输入、无效输入、边界条件、异常情况、空请求体、缺少必要字段、超时以及并发等场景。

2. 测试环境
  • 测试服务器:http://ox/Role/getRolesBylenant
  • 测试工具:Python 3.x,requests库
  • 测试时间:XXXX年XX月XX日
3. 测试结果
3.1 正常输入测试用例
  • 描述:当请求体包含有效的角色信息时,应返回正确的角色列表。
  • 实际结果:返回了正确的角色列表,例如:[{"id": "1", "name": "Admin"}, {"id": "2", "name": "User"}]
  • 状态码:200
  • 结论:通过
3.2 无效输入测试用例
  • 描述:当请求体包含无效的角色信息时,应返回错误信息。
  • 实际结果:返回了错误信息,例如:{"error": "Invalid role information"}
  • 状态码:400
  • 结论:通过
3.3 边界条件测试用例
  • 描述:当请求体包含最大长度的角色信息时,应返回正确的角色列表。
  • 实际结果:返回了正确的角色列表,例如:[{"id": "1234567890", "name": "VeryLongRoleNameThatExceedsTheLimit"}]
  • 状态码:200
  • 结论:通过
3.4 异常情况测试用例
  • 描述:当请求体包含非法字符或特殊字符时,应返回错误信息。
  • 实际结果:返回了错误信息,例如:{"error": "Invalid characters in role information"}
  • 状态码:400
  • 结论:通过
3.5 空请求体测试用例
  • 描述:当请求体为空时,应返回错误信息。
  • 实际结果:返回了错误信息,例如:{"error": "Empty request body"}
  • 状态码:400
  • 结论:通过
3.6 缺少必要字段测试用例
  • 描述:当请求体缺少必要的字段时,应返回错误信息。
  • 实际结果:返回了错误信息,例如:{"error": "Missing role name"}
  • 状态码:400
  • 结论:通过
3.7 超时测试用例
  • 描述:当服务器处理请求时间超过预设阈值时,应返回超时错误信息。
  • 实际结果:返回了超时错误信息,例如:{"error": "Request timed out"}
  • 状态码:408
  • 结论:通过
3.8 并发测试用例
  • 描述:当多个请求同时发送到服务器时,应正确处理并发请求并返回正确的结果。
  • 实际结果:每个请求都返回了正确的角色列表或错误信息。
  • 状态码:200/400/408等
  • 结论:通过
4. 测试总结

经过全面的测试,该接口在各种测试场景下均表现出良好的功能正确性和稳定性。所有测试用例均通过,未发现任何问题。建议继续保持代码质量,并定期进行回归测试以确保系统的稳定性和可靠性。

总之,人工智能技术在软件测试的应用可以实现测试用例的自动生成、测试结果的智能分析与测试报告的自动生成。这不仅可以大幅提高测试效率与质量,还可以减轻测试人员的工作量,使其专注于设计测试方案和决定测试策略。