Skip to content

工具函数(Mock Functions)

什么是工具函数?

工具函数是 AsyncTest 在自定义脚本中提供的一组 Mock 数据生成函数,用于快速生成各种类型的随机测试数据。

你可以通过 at.func 访问工具函数对象,也可以直接通过 at.func.函数名() 的方式调用。


基本用法

直接调用工具函数

python
# 生成随机布尔值
bool_result = at.func.boolean(10, 20, "true").value
print(f"Boolean result: {bool_result}")

# 生成随机自然数
num_result = at.func.natural(1, 100).value
print(f"Random number: {num_result}")

链式调用(工具函数 + 管道函数)

工具函数的返回值支持链式调用管道函数:

python
# 工具函数 -> MD5
md5_result = at.func.boolean(10, 20, "true").md5().value
print(f"MD5 result: {md5_result}")

# 工具函数 -> MD5 -> SHA1
sha_result = at.func.boolean(10, 20, "true").md5().sha("sha1").value
print(f"SHA1 result: {sha_result}")

# 工具函数 -> Base64
b64_result = at.func.natural(1, 100).base64().value
print(f"Base64 encoded: {b64_result}")

注意: 不能连续调用两个工具函数,但可以在工具函数后链式调用多个管道函数。


工具函数列表

基础类型

boolean()

python
at.func.boolean(probability_true: int, probability_false: int, default_return: str = "true")

生成随机布尔值

  • probability_true: 返回 True 的概率(1-100)
  • probability_false: 返回 False 的概率(1-100)
  • default_return: 当概率之和不足 100 时的默认返回值("true" 或 "false")

示例:

python
# 30% 概率返回 True,40% 概率返回 False,其余返回 True
result = at.func.boolean(30, 40, "true").value

natural()

python
at.func.natural(min_val: int, max_val: int)

生成随机自然数(非负整数)

  • min_val: 最小值(≥0)
  • max_val: 最大值(≥min_val)

示例:

python
# 生成 1 到 100 之间的随机数
num = at.func.natural(1, 100).value

integer()

python
at.func.integer(min_val: int, max_val: int)

生成随机整数(可以为负数)

  • min_val: 最小值
  • max_val: 最大值(≥min_val)

示例:

python
# 生成 -100 到 100 之间的随机整数
num = at.func.integer(-100, 100).value

float()

python
at.func.float(min_val: int, max_val: int, min_decimal: int, max_decimal: int)

生成随机浮点数

  • min_val: 整数部分最小值
  • max_val: 整数部分最大值
  • min_decimal: 小数位数最小值
  • max_decimal: 小数位数最大值

示例:

python
# 生成 0.00 到 100.0000 之间的随机浮点数
num = at.func.float(0, 100, 2, 4).value

字符串类型

string()

python
at.func.string(chars: str, min_length: int, max_length: int)

生成随机字符串

  • chars: 字符集(从中随机选择字符)
  • min_length: 最小长度
  • max_length: 最大长度

示例:

python
# 生成 5-10 位的随机字母字符串
s = at.func.string("abcdefghijklmnopqrstuvwxyz", 5, 10).value

character()

python
at.func.character(type_str: str)

从指定字符集中随机选择一个字符

  • type_str: 字符集字符串

示例:

python
# 从 "abc123" 中随机选择一个字符
char = at.func.character("abc123").value

日期时间类型

date()

python
at.func.date(format_str: str)

生成随机日期

  • format_str: 日期格式(支持 yyyy、MM、dd)

示例:

python
# 生成格式为 "2023-05-15" 的随机日期
date = at.func.date("yyyy-MM-dd").value

time()

python
at.func.time(format_str: str)

生成随机时间

  • format_str: 时间格式(支持 HH、mm、ss)

示例:

python
# 生成格式为 "14:30:25" 的随机时间
time = at.func.time("HH:mm:ss").value

datetime()

python
at.func.datetime(format_str: str)

生成随机日期时间(仅时间部分)

  • format_str: 时间格式(支持 HH、mm、ss)

示例:

python
# 生成随机时间部分
datetime = at.func.datetime("HH:mm:ss").value

now()

python
at.func.now(precision: str, format_str: str, offset: str = "", mode: str = "start")

生成当前时间(支持偏移和精度控制)

  • precision: 精度(year、month、week、day、hour、minute、second)
  • format_str: 输出格式(支持 yyyy、MM、dd、HH、mm、ss)
  • offset: 时间偏移(如 "+1 day"、"-2 hour")
  • mode: 模式("start" 为开始时间,"end" 为结束时间)

示例:

python
# 获取今天的开始时间
today_start = at.func.now("day", "yyyy-MM-dd HH:mm:ss", "", "start").value

# 获取明天的结束时间
tomorrow_end = at.func.now("day", "yyyy-MM-dd HH:mm:ss", "+1 day", "end").value

timestamp()

python
at.func.timestamp(precision: str, offset: str = "")

生成时间戳

  • precision: 精度("s" 为秒,"ms" 为毫秒)
  • offset: 时间偏移(如 "+1 day"、"-2 hour")

示例:

python
# 获取当前时间戳(秒)
ts = at.func.timestamp("s", "").value

# 获取明天的时间戳(毫秒)
tomorrow_ts = at.func.timestamp("ms", "+1 day").value

中文数据

cname()

python
at.func.cname()

生成随机中文姓名

示例:

python
name = at.func.cname().value  # 如:张伟、李娜

cfirst()

python
at.func.cfirst()

生成随机中文姓氏

示例:

python
surname = at.func.cfirst().value  # 如:张、李、王

clast()

python
at.func.clast()

生成随机中文名字

示例:

python
given_name = at.func.clast().value  # 如:伟、娜

ctitle()

python
at.func.ctitle(min_length: int, max_length: int)

生成随机中文标题

  • min_length: 最小长度
  • max_length: 最大长度

示例:

python
title = at.func.ctitle(5, 10).value

cword()

python
at.func.cword(min_length: int, max_length: int)

生成随机中文词语

  • min_length: 最小长度
  • max_length: 最大长度

示例:

python
word = at.func.cword(2, 4).value

csentence()

python
at.func.csentence(min_length: int, max_length: int)

生成随机中文句子

  • min_length: 最小长度
  • max_length: 最大长度

示例:

python
sentence = at.func.csentence(10, 20).value

cparagraph()

python
at.func.cparagraph(min_sentences: int, max_sentences: int)

生成随机中文段落

  • min_sentences: 最小句子数
  • max_sentences: 最大句子数

示例:

python
paragraph = at.func.cparagraph(3, 5).value

英文数据

name()

python
at.func.name(generate_middle_name: str)

生成随机英文姓名

  • generate_middle_name: 是否生成中间名("true" 或 "false")

示例:

python
name = at.func.name("false").value  # 如:John Smith
full_name = at.func.name("true").value  # 如:John Michael Smith

first()

python
at.func.first()

生成随机英文名

示例:

python
first_name = at.func.first().value  # 如:John、Sophia

last()

python
at.func.last()

生成随机英文姓

示例:

python
last_name = at.func.last().value  # 如:Smith、Johnson

word()

python
at.func.word(min_length: int, max_length: int)

生成随机英文单词

  • min_length: 最小长度
  • max_length: 最大长度

示例:

python
word = at.func.word(3, 8).value

title()

python
at.func.title(min_words: int, max_words: int)

生成随机英文标题

  • min_words: 最小单词数
  • max_words: 最大单词数

示例:

python
title = at.func.title(3, 6).value

sentence()

python
at.func.sentence(min_words: int, max_words: int)

生成随机英文句子

  • min_words: 最小单词数
  • max_words: 最大单词数

示例:

python
sentence = at.func.sentence(10, 20).value

paragraph()

python
at.func.paragraph(min_sentences: int, max_sentences: int)

生成随机英文段落

  • min_sentences: 最小句子数
  • max_sentences: 最大句子数

示例:

python
paragraph = at.func.paragraph(3, 5).value

个人信息

id()

python
at.func.id()

生成随机身份证号(18 位,符合校验规则)

示例:

python
id_card = at.func.id().value

qq()

python
at.func.qq()

生成随机 QQ 号(5-11 位)

示例:

python
qq = at.func.qq().value

phone()

python
at.func.phone()

生成随机手机号(11 位,符合中国手机号规则)

示例:

python
phone = at.func.phone().value

landline()

python
at.func.landline()

生成随机座机号(带区号)

示例:

python
landline = at.func.landline().value  # 如:010-12345678

gender()

python
at.func.gender(gender: str = None)

生成随机性别

  • gender: 指定性别("male" 或 "female"),不指定则随机

示例:

python
gender = at.func.gender().value  # 随机返回 "male" 或 "female"
male = at.func.gender("male").value  # 固定返回 "male"

地理位置

region()

python
at.func.region()

生成随机中国大区

示例:

python
region = at.func.region().value  # 如:华北、华东

province()

python
at.func.province()

生成随机中国省份

示例:

python
province = at.func.province().value  # 如:北京、上海

city()

python
at.func.city(include_province: str)

生成随机城市

  • include_province: 是否包含省份("true" 或 "false")

示例:

python
city = at.func.city("false").value  # 如:北京
city_with_province = at.func.city("true").value  # 如:北京 (北京)

county()

python
at.func.county(include_province_city: str)

生成随机区县

  • include_province_city: 是否包含省市("true" 或 "false")

示例:

python
county = at.func.county("false").value  # 如:朝阳区
full = at.func.county("true").value  # 如:朝阳区 (北京 北京)

zip()

python
at.func.zip()

生成随机邮政编码(6 位)

示例:

python
zip_code = at.func.zip().value

网络相关

email()

python
at.func.email()

生成随机邮箱地址

示例:

python
email = at.func.email().value  # 如:abc123@example.com

ip()

python
at.func.ip()

生成随机 IP 地址

示例:

python
ip = at.func.ip().value  # 如:192.168.1.100

url()

python
at.func.url(protocol: str)

生成随机 URL

  • protocol: 协议(如 "http"、"https")

示例:

python
url = at.func.url("https").value  # 如:https://www.example.com/path

domain()

python
at.func.domain(top_level_domain: str)

生成随机域名

  • top_level_domain: 顶级域名(如 "com"、"net")

示例:

python
domain = at.func.domain("com").value  # 如:example.com

protocol()

python
at.func.protocol()

生成随机协议

示例:

python
protocol = at.func.protocol().value  # 如:http、https、ftp

tld()

python
at.func.tld()

生成随机顶级域名

示例:

python
tld = at.func.tld().value  # 如:com、net、org

颜色相关

color()

python
at.func.color()

生成随机十六进制颜色值

示例:

python
color = at.func.color().value  # 如:#FF5733

hex()

python
at.func.hex()

生成随机十六进制颜色值(同 color)

示例:

python
hex_color = at.func.hex().value  # 如:#FF5733

rgb()

python
at.func.rgb()

生成随机 RGB 颜色值

示例:

python
rgb = at.func.rgb().value  # 如:rgb(255, 87, 51)

rgba()

python
at.func.rgba()

生成随机 RGBA 颜色值

示例:

python
rgba = at.func.rgba().value  # 如:rgba(255, 87, 51, 0.8)

hsl()

python
at.func.hsl()

生成随机 HSL 颜色值

示例:

python
hsl = at.func.hsl().value  # 如:hsl(120, 50%, 75%)

图像相关

dataimage()

python
at.func.dataimage(image_size: str, text: str, color: str)

生成 Data URI 格式的 SVG 图像

  • image_size: 图像尺寸(如 "200x100")
  • text: 图像中的文字
  • color: 背景颜色

示例:

python
img = at.func.dataimage("200x100", "Hello", "#FF5733").value

其他工具

guid()

python
at.func.guid()

生成随机 GUID(UUID v4)

示例:

python
guid = at.func.guid().value  # 如:550e8400-e29b-41d4-a716-446655440000

uuid()

python
at.func.uuid()

生成随机 UUID(UUID v4)

示例:

python
uuid = at.func.uuid().value  # 如:550e8400-e29b-41d4-a716-446655440000

pick()

python
at.func.pick(arr_str: str)

从 JSON 数组中随机选择一个元素

  • arr_str: JSON 格式的数组字符串

示例:

python
item = at.func.pick('["apple", "banana", "orange"]').value

shuffle()

python
at.func.shuffle(arr_str: str)

随机打乱 JSON 数组

  • arr_str: JSON 格式的数组字符串

示例:

python
shuffled = at.func.shuffle('[1, 2, 3, 4, 5]').value

regexp()

python
at.func.regexp(regex: str)

根据正则表达式生成随机字符串(简化版,仅支持基本字符集)

  • regex: 正则表达式(支持 [a-z]、[A-Z]、[0-9]、.)

示例:

python
char = at.func.regexp("[a-z]").value  # 随机小写字母

注意事项

  • 所有工具函数的返回值都需要通过 .value 访问原始值
  • 工具函数可以链式调用管道函数,但不能连续调用两个工具函数
  • 参数类型错误或超出范围时,函数通常返回空字符串 ""
  • 工具函数生成的数据仅用于测试,不保证完全符合真实业务规则

总结

工具函数提供了丰富的 Mock 数据生成能力:

  • 支持基础类型、字符串、日期时间、中英文数据、个人信息、地理位置、网络、颜色等多种类型
  • 可以链式调用管道函数进行数据转换
  • 通过 .value 访问生成的原始值
  • 适合快速生成测试数据,提升测试效率