您现在的位置是:首页 >技术杂谈 >用ChatGPT生成测试数据网站首页技术杂谈

用ChatGPT生成测试数据

Crossin的编程教室 2024-10-09 00:01:02
简介用ChatGPT生成测试数据

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

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

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

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

4c1c1d57b5322d7ee0ed0dff58e90f6a.png

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

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

2e3c2a60be31eceb586bdb7919a2d500.png

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

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

51f14abc871d74289f96fadfcc9693f0.png

<users>
  <user>
    <id>1</id>
    <name>John Smith</name>
    <age>28</age>
    <gender>male</gender>
    <email>john.smith@example.com</email>
    <address>123 Main Street, City</address>
    <phone>123-456-7890</phone>
    <occupation>engineer</occupation>
  </user>
  <user>
    <id>2</id>
    <name>Jane Doe</name>
    <age>32</age>
    <gender>female</gender>
    <email>jane.doe@example.com</email>
    <address>456 Oak Avenue, Town</address>
    <phone>987-654-3210</phone>
    <occupation>doctor</occupation>
  </user>
  <user>
    <id>3</id>
    <name>David Johnson</name>
    <age>45</age>
    <gender>male</gender>
    <email>david.johnson@example.com</email>
    <address>789 Elm Street, Village</address>
    <phone>555-123-4567</phone>
    <occupation>teacher</occupation>
  </user>
  <user>
    <id>4</id>
    <name>Emily Williams</name>
    <age>21</age>
    <gender>female</gender>
    <email>emily.williams@example.com</email>
    <address>321 Pine Avenue, Town</address>
    <phone>777-888-9999</phone>
    <occupation>engineer</occupation>
  </user>
</users>

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

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

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

5d24c5b6c00c209cd9293649ff84c8e2.png

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时代下学习编程的新模式。

f96815a4d140b2d2805018c5226df465.jpeg

感谢转发点赞的各位~

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。