Generate test data with ChatGPT

Hello everyone, welcome to Crossin's programming classroom!

In the previous article writing a data acquisition program with ChatGPT  , we demonstrated how to use ChatGPT to assist in writing code.

In addition to letting ChatGPT write code directly, we can also let it generate some test data used in development.

For example, data in JSON format is often used during development and testing:

4c1c1d57b5322d7ee0ed0dff58e90f6a.png

[
  {
    "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"
  }
]

If you are not satisfied with the data content or structure, you can ask ChatGPT to make further modifications, such as adding a field with repeated data:

2e3c2a60be31eceb586bdb7919a2d500.png

[
  {
    "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"
  }
]

The format of the data can also be easily adjusted, such as changing from JSON to XML:

51f14abc871d74289f96fadfcc9693f0.png

<users>
  <user>
    <id>1</id>
    <name>John Smith</name>
    <age>28</age>
    <gender>male</gender>
    <email>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</email>
    <address>321 Pine Avenue, Town</address>
    <phone>777-888-9999</phone>
    <occupation>engineer</occupation>
  </user>
</users>

Some students want to say, you can use the faker module to generate it!

Yes, but then you have to write the code yourself after all, and you may have to adjust it from time to time. Now with the assistance of ChatGPT, the data can be output in seconds, wouldn't it be easier?

Of course, you can also combine the two, for example:

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)

In the past, in development, if similar test data was required, manual writing was very time-consuming and inefficient. Now, many similar programming assistance tasks can be handled by ChatGPT, allowing developers to spend time on more important things.


The above content is excerpted from Crossin's new book "Action on Code: Zero-Basic Learning of PYTHON Programming (CHATGPT Edition)" .

This book strives to be easy to understand, so that zero-based "novice" who has no programming experience at all can learn Python. The content starts from the most basic steps of environment construction, and gradually goes deep into common practical applications. While explaining the knowledge points, it is equipped with corresponding code examples, so that readers can learn and practice to deepen their understanding.

The book covers Python environment construction, basic grammar, common data types, practical modules, regular expressions, object-oriented programming, multi-task programming and other knowledge points. In addition, three practical projects of crawler, GUI and game are provided.

The book also innovatively uses ChatGPT as an aid to programming learning, leading readers to explore a new mode of learning programming in the AI ​​era.

f96815a4d140b2d2805018c5226df465.jpeg

Thank you for retweeting and liking ~

Guess you like

Origin blog.csdn.net/qq_40523737/article/details/131137138