ArkTS 测试数据生成利器:getRandomStr 和 getRandomChinese 实战指南

ArkTS 测试数据生成利器:getRandomStr 和 getRandomChinese 实战指南

文章目录

    • 背景
      • 总览方法
      • getRandomChineseChar / getRandomChinese:随机汉字
      • getRandomStr:自定义字符池随机字符串
      • getRandomColor:随机十六进制颜色
      • 完整示例:生成随机用户信息
      • 写在最后

背景

近期发现一款很有意思的HarmonyOS 三方库, 地址 @pura/harmony-utils(V1.4.0) , 作者是"桃花镇童长老", 我这里也是直接通过该作者公布的源码进行案例编写进行,写了到目前写了一部分demo ,感觉确实很有帮助,这里呢也是开始写一个系列的演示demo 供大家参考。如有帮助可以在OpenHarmony中进行下载安装进行使用哦

案例demo导航展示

↓↓↓↓↓↓接下来言归正传 ↓↓↓↓
上一篇讲了数字类随机方法,这篇来讲RandomUtil里更有意思的三类:

  1. 随机汉字:生成随机中文字符
  2. 随机字符串:自定义字符池生成字符串
  3. 随机颜色:生成随机十六进制颜色值

总览方法

先来看下这个三方库的方法有哪些

getRandomChineseChar / getRandomChinese:随机汉字

getRandomChineseChar()生成单个随机汉字,getRandomChinese(n)生成 n 个汉字组成的字符串:

this.Btn('getRandomChineseChar() 单个汉字','#E74C3C',()=>{constv=RandomUtil.getRandomChineseChar();this.addLog(`getRandomChineseChar() → "${v}"`);})Row({space:8}){Text('长度:').fontSize(13).fontColor('#666')TextInput({text:this.chineseCount.toString()}).layoutWeight(1).height(38).fontSize(13).type(InputType.Number).onChange(v=>{this.chineseCount=parseInt(v)||4;})}this.Btn(`getRandomChinese(${this.chineseCount})`,'#E74C3C',()=>{constv=RandomUtil.getRandomChinese(this.chineseCount);this.addLog(`getRandomChinese(${this.chineseCount}) → "${v}"`);})

实际运行结果

  • getRandomChineseChar()→ 每次返回一个随机汉字,比如"驿""梓""铃"
  • getRandomChinese(4)→ 4个随机汉字,比如"幻轩青瓷"

生成的汉字来自常用汉字范围(CJK 统一汉字,Unicode\u4e00\u9fa5),共 20902 个汉字。不会生成生僻字,适合生成测试用的中文内容。

使用场景

  • 生成随机的中文昵称(可以配合数字)
  • UI 测试时填充中文占位内容
  • 生成随机的中文标题测试排版

getRandomStr:自定义字符池随机字符串

getRandomStr(length, strPool)从你指定的字符池里随机抽取字符,生成指定长度的字符串:

Row({space:8}){Text('长度:').fontSize(13).fontColor('#666')TextInput({text:this.strLength.toString()}).layoutWeight(1).height(38).fontSize(13).type(InputType.Number).onChange(v=>{this.strLength=parseInt(v)||8;})}this.Btn(`getRandomStr(${this.strLength}, 数字池)`,'#27AE60',()=>{constv=RandomUtil.getRandomStr(this.strLength,'0123456789');this.addLog(`getRandomStr(${this.strLength}, "0-9") → "${v}"`);})this.Btn(`getRandomStr(${this.strLength}, 字母池)`,'#27AE60',()=>{constv=RandomUtil.getRandomStr(this.strLength,'abcdefghijklmnopqrstuvwxyz');this.addLog(`getRandomStr(${this.strLength}, "a-z") → "${v}"`);})this.Btn(`getRandomStr(${this.strLength}, 字母数字混合池)`,'#27AE60',()=>{constv=RandomUtil.getRandomStr(this.strLength,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');this.addLog(`getRandomStr(${this.strLength}, alphanumeric) → "${v}"`);})

实际运行结果(length=8 时):

  • 数字池:"34719826"这样的纯数字字符串
  • 字母池:"kxmvtpwj"这样的纯小写字母
  • 字母数字混合:"aB3kZ7xM"这样的大小写字母加数字混合

三个典型场景

场景1 - 生成短信验证码(纯数字,6位):

constsmsCode=RandomUtil.getRandomStr(6,'0123456789');// 例如:'847291'

场景2 - 生成邀请码(大写字母+数字,8位):

constinviteCode=RandomUtil.getRandomStr(8,'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');// 例如:'K7P3NZ2A'

场景3 - 生成临时密码(字母数字混合,12位):

consttempPassword=RandomUtil.getRandomStr(12,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');// 例如:'kzP7nXmQ3rBv'

getRandomColor:随机十六进制颜色

生成随机的 CSS 十六进制颜色值(#RRGGBB格式):

Row({space:12}){Button('getRandomColor()').layoutWeight(1).height(40).fontSize(14).fontColor('#fff').backgroundColor('#F39C12').borderRadius(8).onClick(()=>{constc=RandomUtil.getRandomColor();this.randomColor=c;this.addLog(`getRandomColor() → "${c}"`);})Row().width(40).height(40).backgroundColor(this.randomColor).borderRadius(8).border({width:1,color:'#ddd'})}

实际运行结果

  • getRandomColor()→ 类似"#3A7FD5"这样的随机颜色值

this.randomColor存的就是颜色字符串,直接传给backgroundColor属性就能用,ArkTS 支持直接用十六进制字符串设置颜色。

使用场景

  • 生成随机头像背景色
  • 数据可视化里给不同系列分配颜色
  • UI 主题测试

完整示例:生成随机用户信息

把以上方法组合起来,生成一套随机测试用户数据:

import{RandomUtil}from'../Utils/RandomUtil';functiongenerateRandomUser(){constname=RandomUtil.getRandomChinese(2)+RandomUtil.getRandomChinese(1);// 2-3个字姓名constinviteCode=RandomUtil.getRandomStr(8,'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');constavatarColor=RandomUtil.getRandomColor();constage=RandomUtil.getRandomInt(18,60);return{name,inviteCode,avatarColor,age};}// 调用示例constuser=generateRandomUser();// 结果类似:// { name: "林青云", inviteCode: "K3NP7ZA2", avatarColor: "#5E8BC7", age: 32 }

写在最后

随机字符串的关键是字符池设计——根据你的业务需求选合适的字符集。验证码用数字池,邀请码用字母数字,密码用更丰富的字符集。

getRandomColor直接返回可用的颜色字符串,在 ArkTS 里赋值给颜色属性无需任何转换,用起来特别顺手。

下一篇讲RandomUtil的 UUID 生成——包括 36 位标准格式、32 位无横杠格式、以及二进制 UUID。