生成随机字符串
import random
import string
def generate_random_string(length=10):
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
print(generate_random_string(15))
生成随机整数
import random
def generate_random_integer(start=1, end=100):
return random.randint(start, end)
print(generate_random_integer(1, 1000))
生成随机日期
from datetime import datetime, timedelta
import random
def generate_random_date(start_date=None, end_date=None):
if start_date is None:
start_date = datetime(2000, 1, 1)
if end_date is None:
end_date = datetime.now()
delta = end_date - start_date
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
random_second = random.randrange(int_delta)
return start_date + timedelta(seconds=random_second)
print(generate_random_date().strftime('%Y-%m-%d'))
生成随机时间
from datetime import time, timedelta
import random
def generate_random_time():
hours = random.randint(0, 23)
minutes = random.randint(0, 59)
seconds = random.randint(0, 59)
return time(hours, minutes, seconds)
print(generate_random_time().strftime('%H:%M:%S'))
生成随机电子邮件地址
import random
import string
def generate_random_email():
local_part = ''.join(random.choices(string.ascii_letters + string.digits + '_', k=8))
domain = ''.join(random.choices(string.ascii_letters, k=4)) + '.com'
return f"{local_part}@{domain}"
print(generate_random_email())
生成随机IP地址
import random
def generate_random_ip():
return '.'.join(map(str, (random.randint(0, 255) for _ in range(4))))
print(generate_random_ip())
生成随机UUID
import uuid
def generate_random_uuid():
return str(uuid.uuid4())
print(generate_random_uuid())
生成随机地址(城市、州、国家)
import random
from faker import Faker
fake = Faker()
def generate_random_address():
return {
'city': fake.city(),
'state': fake.state(),
'country': fake.country()
}
print(generate_random_address())
生成随机电话号码
from faker import Faker
fake = Faker()
def generate_random_phone_number():
return fake.phone_number()
print(generate_random_phone_number())
生成随机文本段落
from faker import Faker
fake = Faker()
def generate_random_text(nb_sentences=3):
return fake.text(nb_sentences=nb_sentences)
print(generate_random_text(5))
生成随机URL
import random
from faker import Faker
fake = Faker()
def generate_random_url():
return fake.url()
print(generate_random_url())
从CSV文件读取测试数据
import csv
def read_test_data_from_csv(file_path):
test_data = []
with open(file_path, mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
test_data.append(row)
return test_data
data = read_test_data_from_csv('test_data.csv') # 读取CSV文件中的测试数据
for row in data:
print(row)
生成随机颜色代码
import random
def generate_random_color():
return "#{:02X}{:02X}{:02X}".format(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
print(generate_random_color())
生成随机JSON对象
import json
import random
import string
def generate_random_json():
data = {
'key1': random.choice(string.ascii_letters) + str(random.randint(0, 9)),
'key2': random.uniform(1.0, 100.0),
'key3': [random.randint(0, 100) for _ in range(5)],
'key4': {
'subkey1': random.choice([True, False]),
'subkey2': generate_random_string(10)
}
}
return json.dumps(data)
print(generate_random_json())
随机生成MAC地址
import random
def generate_random_mac():
mac = [0x00, 0x1A, 0x2B,
random.randint(0x00, 0xff),
random.randint(0x00, 0xff),
random.randint(0x00, 0xff)]
return ':'.join(map(lambda x: format(x, '02x'), mac))