Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kyuff/testdata
Library to generate random and typed data for tests
https://github.com/kyuff/testdata
generator golang testing
Last synced: 12 days ago
JSON representation
Library to generate random and typed data for tests
- Host: GitHub
- URL: https://github.com/kyuff/testdata
- Owner: kyuff
- License: mit
- Created: 2024-02-16T13:24:42.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-02-18T12:18:44.000Z (9 months ago)
- Last Synced: 2024-09-06T09:14:20.099Z (2 months ago)
- Topics: generator, golang, testing
- Language: Go
- Homepage:
- Size: 29.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Testdata generator for Go
Library to generate random and typed data for tests.
## Use case
In some types of tests it is convenient to randomize input data in order to avoid a hidden reliance on specific values.
Especially in code bases that model domain specific values using named types, this can be cumbersome to do by hand.This library provides a simple and convenient way to configure value types and generate them for specific tests.
## FAQ
### Can I reuse a `testdata.Config` between tests?
Yes! There is even a convenient `testdata.DefaultConfig` you can use.
You can configure the default config using the convenient testdata.Values functions. Alternatively you can use options
that is prefixed using `With` when setting up your own `Config`.### Is it ok to use `testdata.Make()`?
Yes, the `testdata.MakeWith()` is only meant if you don't want to use the default config.
### Can I control how values are generated?
Yes, use the option `testdata.WithGenerator` which accepts a func that provides a specific type. This func will be
called each time there is a need to generate that specific type. This is a method to override the default generator.## Example
````go
var (
t = &testing.T{}
_ = testdata.MakeSticky[City](t)
a = testdata.Make[Person](t)
b = testdata.Make[Person](t, func(person Person) Person {
person.Age = 32
return person
})
)fmt.Printf("A: %#v\n", a)
fmt.Printf("B: %#v\n", b)// Output:
// A: testdata_test.Person{Name:"My own name: 73", Age:50, Dish:"SPAGHETTI", City:"City-LCMNe2ur8bFrW7oM", Note:"string-k3Dc1kHJPXsAFv0C"}
// B: testdata_test.Person{Name:"My own name: 94", Age:32, Dish:"SPAGHETTI", City:"City-LCMNe2ur8bFrW7oM", Note:"string-nUQnH3DqWyTPPTEi"}
````Find more detailed examples [here](example_make_test.go) and [here](example_makewith_test.go).