前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >每日一库:测试数据生成 -- faker

每日一库:测试数据生成 -- faker

作者头像
孟斯特
发布2023-10-16 20:04:24
2410
发布2023-10-16 20:04:24
举报
文章被收录于专栏:code人生code人生

faker 是一个用于生成随机测试数据的 Go 语言库。它能够帮助开发者快速生成模拟数据,用于测试、演示、样本数据生成等场景。

faker支持一下类型数据:

int, int8, int16, int32 & int64

[]int, []int8, []int16, []int32 & []int64

bool & []boolstring & []string

float32, float64, []float32 &[]float64

time.Time & []time.Time

•嵌套结构

作者提供了丰富的例子以供参考:

•使用结构体的标签:

•基本标签:example_with_tags_test.go[1]

•长度和边界:example_with_tags_lenbounds_test.go[2]

•语言:example_with_tags_lang_test.go[3]

•唯一性:example_with_tags_unique_test.go[4]

•切片长度:example_with_tags_slicelength_test.go[5]

•自定义结构体的标签(定义你自己的faker数据):example_custom_faker_test.go[6]

•不使用结构体的标签:example_without_tag_test.go[7]

•单个假数据函数:example_single_fake_data_test.go[8]

faker在使用过程中也存在一些限制:

•它不支持私有字段。确保你打算生成假数据的结构体字段是公开的,否则会触发panic。你可以在你的私有字段上使用标签skip faker:"-"来忽略字段。

•它不支持 interface{} 数据类型。如果我们不知道其数据类型,我们怎么能生成任何东西呢?

•它不支持 map[interface{}]interface{}map[any_type]interface{}map[interface{}]any_type 数据类型。

•自定义类型并未完全支持。然而,一些自定义类型已经得到支持:我们仍在研究如何正确地做到这一点。目前,如果你使用 faker,最安全的做法是不使用任何自定义类型,以避免panic。

•如果并且仅当使用 AddProvider()[9] 扩展时,一些额外的自定义类型可以得到支持,请参见 示例[10]

oneof 标签目前只支持 stringint 类型,以及 float32float64。更多的支持即将到来(例如,十六进制数字等)。请参见 示例[11] 了解如何使用。

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)[12]进行许可,使用时请注明出处。 Author: mengbin[13] blog: mengbin[14] Github: mengbin92[15] cnblogs: 恋水无意[16]

References

[1] 基本标签:example_with_tags_test.go: https://github.com/go-faker/faker/blob/main/example_with_tags_test.go [2] 长度和边界:example_with_tags_lenbounds_test.go: https://github.com/go-faker/faker/blob/main/example_with_tags_lenbounds_test.go [3] 语言:example_with_tags_lang_test.go: https://github.com/go-faker/faker/blob/main/example_with_tags_lang_test.go [4] 唯一性:example_with_tags_unique_test.go: https://github.com/go-faker/faker/blob/main/example_with_tags_unique_test.go [5] 切片长度:example_with_tags_slicelength_test.go: https://github.com/go-faker/faker/blob/main/example_with_tags_slicelength_test.go [6] example_custom_faker_test.go: https://github.com/go-faker/faker/blob/main/example_custom_faker_test.go [7] example_without_tag_test.go: https://github.com/go-faker/faker/blob/main/example_without_tag_test.go [8] example_single_fake_data_test.go: https://github.com/go-faker/faker/blob/main/example_single_fake_data_test.go [9] AddProvider(): https://github.com/go-faker/faker/blob/7473ac7d8d0440d24addac302c73e13c08895764/faker.go#L303 [10] 示例: https://github.com/go-faker/faker/blob/main/example_custom_faker_test.go#L46 [11] 示例: https://github.com/go-faker/faker/blob/main/example_with_tags_test.go#L53 [12] 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0): https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh [13] mengbin: mengbin1992@outlook.com [14] mengbin: https://mengbin.top [15] mengbin92: https://mengbin92.github.io/ [16] 恋水无意: https://www.cnblogs.com/lianshuiwuyi/

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-08-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 孟斯特 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • References
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档