Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

Generate Random Fake User Data for Testing in Python

This code generates a list of fake user dictionaries with random names, emails, ages, and timestamps using the Python standard library for testing purposes.

Easy Python 3.9+ Jun 27, 2026 Automation & scripting 1 views 0 copies

Python code

28 lines
Python 3.9+
import json
import random
import string
from datetime import datetime, timedelta

def generate_user_data(num_users=1):
    first_names = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
    last_names = ["Smith", "Johnson", "Brown", "Taylor", "Wilson"]
    domains = ["example.com", "test.org", "demo.net"]
    
    users = []
    for _ in range(num_users):
        first = random.choice(first_names)
        last = random.choice(last_names)
        user = {
            "id": ''.join(random.choices(string.digits, k=6)),
            "name": f"{first} {last}",
            "email": f"{first.lower()}.{last.lower()}@{random.choice(domains)}",
            "age": random.randint(18, 80),
            "is_active": random.choice([True, False]),
            "created_at": (datetime.now() - timedelta(days=random.randint(0, 365))).isoformat()
        }
        users.append(user)
    return users

if __name__ == "__main__":
    fake_users = generate_user_data(3)
    print(json.dumps(fake_users, indent=2))

Output

stdout
[
  {
    "id": "482915",
    "name": "Alice Brown",
    "email": "alice.brown@demo.net",
    "age": 34,
    "is_active": true,
    "created_at": "2024-11-12T10:30:00.123456"
  },
  {
    "id": "739201",
    "name": "Bob Taylor",
    "email": "bob.taylor@test.org",
    "age": 61,
    "is_active": false,
    "created_at": "2024-08-25T10:30:00.123456"
  },
  {
    "id": "564810",
    "name": "Eve Wilson",
    "email": "eve.wilson@example.com",
    "age": 25,
    "is_active": true,
    "created_at": "2025-03-01T10:30:00.123456"
  }
]

How it works

The function generates unique user data by randomly picking from predefined first and last names, email domains, and other attributes. An ID is created from random digits using random.choices. The created_at timestamp is set to a random past date within the last year, formatted as an ISO string. This approach uses only Python's standard library (json, random, string, datetime), requiring no external packages.

Common mistakes

  • Forgetting to convert list of users to JSON string with `json.dumps` for printing.
  • Using `random.sample` instead of `random.choice` for selecting a single value, which fails if the pool is too small.
  • Not importing `timedelta` from `datetime` leads to a `NameError`.
  • Assuming random output is deterministic without setting a seed with `random.seed()`.

Variations

  1. Use the `faker` library for more realistic and localized fake data (names, addresses, etc.).
  2. Make the `generate_user_data` function a generator with `yield` to avoid high memory usage for large numbers of users.

Real-world use cases

  • Populating a local development database with dummy user records to test CRUD operations.
  • Loading test data into a staging environment to simulate load and verify API endpoints.
  • Creating mock user profiles for unit tests that require multiple distinct user objects.

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Automation & scripting

Related tutorials and quizzes for this topic.