工具函数(Mock Functions)
什么是工具函数?
工具函数是 AsyncTest 在自定义脚本中提供的一组 Mock 数据生成函数,用于快速生成各种类型的随机测试数据。
你可以通过 at.func 访问工具函数对象,也可以直接通过 at.func.函数名() 的方式调用。
基本用法
直接调用工具函数
# 生成随机布尔值
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}")链式调用(工具函数 + 管道函数)
工具函数的返回值支持链式调用管道函数:
# 工具函数 -> 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()
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")
示例:
# 30% 概率返回 True,40% 概率返回 False,其余返回 True
result = at.func.boolean(30, 40, "true").valuenatural()
at.func.natural(min_val: int, max_val: int)生成随机自然数(非负整数)
min_val: 最小值(≥0)max_val: 最大值(≥min_val)
示例:
# 生成 1 到 100 之间的随机数
num = at.func.natural(1, 100).valueinteger()
at.func.integer(min_val: int, max_val: int)生成随机整数(可以为负数)
min_val: 最小值max_val: 最大值(≥min_val)
示例:
# 生成 -100 到 100 之间的随机整数
num = at.func.integer(-100, 100).valuefloat()
at.func.float(min_val: int, max_val: int, min_decimal: int, max_decimal: int)生成随机浮点数
min_val: 整数部分最小值max_val: 整数部分最大值min_decimal: 小数位数最小值max_decimal: 小数位数最大值
示例:
# 生成 0.00 到 100.0000 之间的随机浮点数
num = at.func.float(0, 100, 2, 4).value字符串类型
string()
at.func.string(chars: str, min_length: int, max_length: int)生成随机字符串
chars: 字符集(从中随机选择字符)min_length: 最小长度max_length: 最大长度
示例:
# 生成 5-10 位的随机字母字符串
s = at.func.string("abcdefghijklmnopqrstuvwxyz", 5, 10).valuecharacter()
at.func.character(type_str: str)从指定字符集中随机选择一个字符
type_str: 字符集字符串
示例:
# 从 "abc123" 中随机选择一个字符
char = at.func.character("abc123").value日期时间类型
date()
at.func.date(format_str: str)生成随机日期
format_str: 日期格式(支持 yyyy、MM、dd)
示例:
# 生成格式为 "2023-05-15" 的随机日期
date = at.func.date("yyyy-MM-dd").valuetime()
at.func.time(format_str: str)生成随机时间
format_str: 时间格式(支持 HH、mm、ss)
示例:
# 生成格式为 "14:30:25" 的随机时间
time = at.func.time("HH:mm:ss").valuedatetime()
at.func.datetime(format_str: str)生成随机日期时间(仅时间部分)
format_str: 时间格式(支持 HH、mm、ss)
示例:
# 生成随机时间部分
datetime = at.func.datetime("HH:mm:ss").valuenow()
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" 为结束时间)
示例:
# 获取今天的开始时间
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").valuetimestamp()
at.func.timestamp(precision: str, offset: str = "")生成时间戳
precision: 精度("s" 为秒,"ms" 为毫秒)offset: 时间偏移(如 "+1 day"、"-2 hour")
示例:
# 获取当前时间戳(秒)
ts = at.func.timestamp("s", "").value
# 获取明天的时间戳(毫秒)
tomorrow_ts = at.func.timestamp("ms", "+1 day").value中文数据
cname()
at.func.cname()生成随机中文姓名
示例:
name = at.func.cname().value # 如:张伟、李娜cfirst()
at.func.cfirst()生成随机中文姓氏
示例:
surname = at.func.cfirst().value # 如:张、李、王clast()
at.func.clast()生成随机中文名字
示例:
given_name = at.func.clast().value # 如:伟、娜ctitle()
at.func.ctitle(min_length: int, max_length: int)生成随机中文标题
min_length: 最小长度max_length: 最大长度
示例:
title = at.func.ctitle(5, 10).valuecword()
at.func.cword(min_length: int, max_length: int)生成随机中文词语
min_length: 最小长度max_length: 最大长度
示例:
word = at.func.cword(2, 4).valuecsentence()
at.func.csentence(min_length: int, max_length: int)生成随机中文句子
min_length: 最小长度max_length: 最大长度
示例:
sentence = at.func.csentence(10, 20).valuecparagraph()
at.func.cparagraph(min_sentences: int, max_sentences: int)生成随机中文段落
min_sentences: 最小句子数max_sentences: 最大句子数
示例:
paragraph = at.func.cparagraph(3, 5).value英文数据
name()
at.func.name(generate_middle_name: str)生成随机英文姓名
generate_middle_name: 是否生成中间名("true" 或 "false")
示例:
name = at.func.name("false").value # 如:John Smith
full_name = at.func.name("true").value # 如:John Michael Smithfirst()
at.func.first()生成随机英文名
示例:
first_name = at.func.first().value # 如:John、Sophialast()
at.func.last()生成随机英文姓
示例:
last_name = at.func.last().value # 如:Smith、Johnsonword()
at.func.word(min_length: int, max_length: int)生成随机英文单词
min_length: 最小长度max_length: 最大长度
示例:
word = at.func.word(3, 8).valuetitle()
at.func.title(min_words: int, max_words: int)生成随机英文标题
min_words: 最小单词数max_words: 最大单词数
示例:
title = at.func.title(3, 6).valuesentence()
at.func.sentence(min_words: int, max_words: int)生成随机英文句子
min_words: 最小单词数max_words: 最大单词数
示例:
sentence = at.func.sentence(10, 20).valueparagraph()
at.func.paragraph(min_sentences: int, max_sentences: int)生成随机英文段落
min_sentences: 最小句子数max_sentences: 最大句子数
示例:
paragraph = at.func.paragraph(3, 5).value个人信息
id()
at.func.id()生成随机身份证号(18 位,符合校验规则)
示例:
id_card = at.func.id().valueqq()
at.func.qq()生成随机 QQ 号(5-11 位)
示例:
qq = at.func.qq().valuephone()
at.func.phone()生成随机手机号(11 位,符合中国手机号规则)
示例:
phone = at.func.phone().valuelandline()
at.func.landline()生成随机座机号(带区号)
示例:
landline = at.func.landline().value # 如:010-12345678gender()
at.func.gender(gender: str = None)生成随机性别
gender: 指定性别("male" 或 "female"),不指定则随机
示例:
gender = at.func.gender().value # 随机返回 "male" 或 "female"
male = at.func.gender("male").value # 固定返回 "male"地理位置
region()
at.func.region()生成随机中国大区
示例:
region = at.func.region().value # 如:华北、华东province()
at.func.province()生成随机中国省份
示例:
province = at.func.province().value # 如:北京、上海city()
at.func.city(include_province: str)生成随机城市
include_province: 是否包含省份("true" 或 "false")
示例:
city = at.func.city("false").value # 如:北京
city_with_province = at.func.city("true").value # 如:北京 (北京)county()
at.func.county(include_province_city: str)生成随机区县
include_province_city: 是否包含省市("true" 或 "false")
示例:
county = at.func.county("false").value # 如:朝阳区
full = at.func.county("true").value # 如:朝阳区 (北京 北京)zip()
at.func.zip()生成随机邮政编码(6 位)
示例:
zip_code = at.func.zip().value网络相关
email()
at.func.email()生成随机邮箱地址
示例:
email = at.func.email().value # 如:abc123@example.comip()
at.func.ip()生成随机 IP 地址
示例:
ip = at.func.ip().value # 如:192.168.1.100url()
at.func.url(protocol: str)生成随机 URL
protocol: 协议(如 "http"、"https")
示例:
url = at.func.url("https").value # 如:https://www.example.com/pathdomain()
at.func.domain(top_level_domain: str)生成随机域名
top_level_domain: 顶级域名(如 "com"、"net")
示例:
domain = at.func.domain("com").value # 如:example.comprotocol()
at.func.protocol()生成随机协议
示例:
protocol = at.func.protocol().value # 如:http、https、ftptld()
at.func.tld()生成随机顶级域名
示例:
tld = at.func.tld().value # 如:com、net、org颜色相关
color()
at.func.color()生成随机十六进制颜色值
示例:
color = at.func.color().value # 如:#FF5733hex()
at.func.hex()生成随机十六进制颜色值(同 color)
示例:
hex_color = at.func.hex().value # 如:#FF5733rgb()
at.func.rgb()生成随机 RGB 颜色值
示例:
rgb = at.func.rgb().value # 如:rgb(255, 87, 51)rgba()
at.func.rgba()生成随机 RGBA 颜色值
示例:
rgba = at.func.rgba().value # 如:rgba(255, 87, 51, 0.8)hsl()
at.func.hsl()生成随机 HSL 颜色值
示例:
hsl = at.func.hsl().value # 如:hsl(120, 50%, 75%)图像相关
dataimage()
at.func.dataimage(image_size: str, text: str, color: str)生成 Data URI 格式的 SVG 图像
image_size: 图像尺寸(如 "200x100")text: 图像中的文字color: 背景颜色
示例:
img = at.func.dataimage("200x100", "Hello", "#FF5733").value其他工具
guid()
at.func.guid()生成随机 GUID(UUID v4)
示例:
guid = at.func.guid().value # 如:550e8400-e29b-41d4-a716-446655440000uuid()
at.func.uuid()生成随机 UUID(UUID v4)
示例:
uuid = at.func.uuid().value # 如:550e8400-e29b-41d4-a716-446655440000pick()
at.func.pick(arr_str: str)从 JSON 数组中随机选择一个元素
arr_str: JSON 格式的数组字符串
示例:
item = at.func.pick('["apple", "banana", "orange"]').valueshuffle()
at.func.shuffle(arr_str: str)随机打乱 JSON 数组
arr_str: JSON 格式的数组字符串
示例:
shuffled = at.func.shuffle('[1, 2, 3, 4, 5]').valueregexp()
at.func.regexp(regex: str)根据正则表达式生成随机字符串(简化版,仅支持基本字符集)
regex: 正则表达式(支持 [a-z]、[A-Z]、[0-9]、.)
示例:
char = at.func.regexp("[a-z]").value # 随机小写字母注意事项
- 所有工具函数的返回值都需要通过
.value访问原始值 - 工具函数可以链式调用管道函数,但不能连续调用两个工具函数
- 参数类型错误或超出范围时,函数通常返回空字符串
"" - 工具函数生成的数据仅用于测试,不保证完全符合真实业务规则
总结
工具函数提供了丰富的 Mock 数据生成能力:
- 支持基础类型、字符串、日期时间、中英文数据、个人信息、地理位置、网络、颜色等多种类型
- 可以链式调用管道函数进行数据转换
- 通过
.value访问生成的原始值 - 适合快速生成测试数据,提升测试效率