用ChatGPT生成测试数据

大家好,欢迎来到 Crossin的编程教室 !

在之前的文章 用ChatGPT写一个数据采集程序 中,我们演示了如何用 ChatGPT 辅助编写代码。

除了直接让ChatGPT写代码,我们也可以让它生成一些开发中使用的测试数据。

比如在开发和测试时,常会用到 JSON 格式的数据:

用ChatGPT生成测试数据_第1张图片

[
  {
    "id": 1,
    "name": "John Smith",
    "age": 28,
    "gender": "male",
    "email": "[email protected]",
    "address": "123 Main Street, City",
    "phone": "123-456-7890"
  },
  {
    "id": 2,
    "name": "Jane Doe",
    "age": 32,
    "gender": "female",
    "email": "[email protected]",
    "address": "456 Oak Avenue, Town",
    "phone": "987-654-3210"
  },
  {
    "id": 3,
    "name": "David Johnson",
    "age": 45,
    "gender": "male",
    "email": "[email protected]",
    "address": "789 Elm Street, Village",
    "phone": "555-123-4567"
  },
  {
    "id": 4,
    "name": "Emily Williams",
    "age": 21,
    "gender": "female",
    "email": "[email protected]",
    "address": "321 Pine Avenue, Town",
    "phone": "777-888-9999"
  }
]

如果对数据内容或结构不满意,可以让ChatGPT做进一步修改,比如增加一个存在重复数据的字段:

用ChatGPT生成测试数据_第2张图片

[
  {
    "id": 1,
    "name": "John Smith",
    "age": 28,
    "gender": "male",
    "email": "[email protected]",
    "address": "123 Main Street, City",
    "phone": "123-456-7890",
    "occupation": "engineer"
  },
  {
    "id": 2,
    "name": "Jane Doe",
    "age": 32,
    "gender": "female",
    "email": "[email protected]",
    "address": "456 Oak Avenue, Town",
    "phone": "987-654-3210",
    "occupation": "doctor"
  },
  {
    "id": 3,
    "name": "David Johnson",
    "age": 45,
    "gender": "male",
    "email": "[email protected]",
    "address": "789 Elm Street, Village",
    "phone": "555-123-4567",
    "occupation": "teacher"
  },
  {
    "id": 4,
    "name": "Emily Williams",
    "age": 21,
    "gender": "female",
    "email": "[email protected]",
    "address": "321 Pine Avenue, Town",
    "phone": "777-888-9999",
    "occupation": "engineer"
  }
]

数据的格式也可以很方便地调整,比如从JSON换成XML:

用ChatGPT生成测试数据_第3张图片


  
    1
    John Smith
    28
    male
    [email protected]
    
123 Main Street, City
123-456-7890 engineer
2 Jane Doe 32 female [email protected]
456 Oak Avenue, Town
987-654-3210 doctor
3 David Johnson 45 male [email protected]
789 Elm Street, Village
555-123-4567 teacher
4 Emily Williams 21 female [email protected]
321 Pine Avenue, Town
777-888-9999 engineer

有同学要说了,可以用 faker 模块来生成哎!

是的,但那样毕竟也要自己动手写代码,还可能时不时要调整。现在有ChatGPT的辅助,可以秒出数据,岂不是更轻松?

当然你也可以将二者结合,比如这样:

用ChatGPT生成测试数据_第4张图片

import random
from faker import Faker
import json


faker = Faker()


def generate_user_data(num_users):
    users = []
    for _ in range(num_users):
        user = {
            "id": faker.random_number(digits=4),
            "name": faker.name(),
            "age": random.randint(18, 60),
            "gender": random.choice(["male", "female"]),
            "email": faker.email(),
            "address": faker.address(),
            "phone": faker.phone_number()
        }
        users.append(user)
    return users


num_users = 4
user_data = generate_user_data(num_users)


json_data = json.dumps(user_data, indent=4)
print(json_data)

以往在开发中,如果需要类似的测试数据,手动编写是非常耗时和低效的。现在,类似的很多编程辅助工作都可交由ChatGPT来处理,从而让开发者把时间花在更重要的事情之上。


以上内容节选自Crossin的新书《码上行动:零基础学会PYTHON编程(CHATGPT版)》。

本书力求做到浅显易懂,让完全没有编程经验的零基础“小白”也能学会Python。内容从环境搭建这种最基础的步骤开始讲起,逐渐深入到常见的实际应用当中。在讲解知识点的同时配有相应的代码示例,让读者可以边学边练加深理解。

全书涵盖Python环境搭建、基础语法、常见数据类型、实用模块、正则表达式、面向对象编程、多任务编程等知识点。另外还提供了爬虫、GUI、游戏三个实战项目。

书中还创新地使用 ChatGPT 作为编程学习的辅助,带领读者探索AI时代下学习编程的新模式。

用ChatGPT生成测试数据_第5张图片

感谢转发点赞的各位~

你可能感兴趣的:(python,ChatGPT,测试)