TestCraft is a Go library for filling your structs with test data. You can either define attributes for your structs or randomize them.
To use TestCraft, you can install it using the go get command:
go get github.com/trapajim/testcraftExample of how to use TestCraft:
package main
import (
"fmt""github.com/trapajim/testcraft/datagen""github.com/trapajim/testcraft"
)
typeUserstruct {
IDintNamestringBooks []string
}
funcmain() {
// Create a new factory for the User structuserFactory:=testcraft.NewFactory(User{})
// Create a sequencer to generate unique IDsuserSeq:=testcraft.NewSequencer(1)
// Define attributes for the User structuserFactory.Attr(func(u*User) error {
u.ID=userSeq.Next()
u.Books=testcraft.Multiple(5, func(iint) string {
returnfmt.Sprintf("book %d", i)
})
u.Name=datagen.AlphanumericBetween(5,10)
returnnil
})
// Build a new User structuser1, err:=userFactory.Build()
fmt.Println("ID:", user1.ID, "Name:", user1.Name, "Books:", user1.Books)
iferr!=nil {
// Handle the error
}
// Build a new User struct MustBuild panics on erroruser2:=userFactory.MustBuild()
fmt.Println("ID:", user2.ID, "Name:", user2.Name, "Book:", user2.Books)
}Output:
ID: 1 Name: yUKW9 Books: [book 0 book 1 book 2 book 3 book 4]
ID: 2 Name: jXZ816KH Books: [book 0 book 1 book 2 book 3 book 4]TestCraft can create random data for your structs
randUser, err:=userFactory.Randomize()
iferr!=nil {
// Handle the error
}
fmt.Println("ID:", randUser.ID, "Name:", randUser.Name, "Books:", randUser.Books)Output:
ID: 32 Name: agree pedal Books: [cool egg fish apple advise rich]if you need some control over the randomized data, you can use the RandomizerWithAttrs this will apply the attributes you defined and randomize the rest of the struct.
userSeq:=testcraft.NewSequencer(1)
userFact:=testcraft.NewFactory(User{}).Attr(func(u*User) error {
u.ID=userSeq.Next()
returnnil
})
randomUser1, _:=userFact.RandomizeWithAttrs()
randomUser2, _:=userFact.RandomizeWithAttrs()
fmt.Println("ID:", randomUser1.ID, "Name:", randomUser1.Name, "Books:", randomUser1.Books)
fmt.Println("ID:", randomUser2.ID, "Name:", randomUser2.Name, "Books:", randomUser2.Books)Output:
ID: 1 Name: plain lucky Books: [new embarrass plain best analyse hum]
ID: 2 Name: analyse new Books: [quince soft lemon new rich mild]Each of the functions can be prefixed with Must to panic on error.
Traits allow you to create factory variants with predefined attribute overrides. Use With() to apply traits without modifying the original factory:
typePersonstruct {
NamestringAgeintRolestring
}
// Define traits as attribute generatorsvar (
Senior=func(p*Person) error {
p.Age=65returnnil
}
Admin=func(p*Person) error {
p.Role="admin"returnnil
}
)
// Create base factorypersonFactory:=testcraft.NewFactory(Person{}).Attr(func(p*Person) error {
p.Name="John Doe"p.Role="user"returnnil
})
// Apply traits to create variantsseniorPerson:=personFactory.With(Senior).MustBuild()
fmt.Println(seniorPerson) // {John Doe 65 user}adminSenior:=personFactory.With(Senior, Admin).MustBuild()
fmt.Println(adminSenior) // {John Doe 65 admin}// Original factory remains unchangedregularPerson:=personFactory.MustBuild()
fmt.Println(regularPerson) // {John Doe 0 user}Traits are applied after base attributes, so they can override any previously set values. You can combine multiple traits in a single With() call or chain them.
The randomizer has a set of default rules for various types:
| Type | Rule |
|---|---|
| string | random two words |
| int | random number between 0 and 100 |
| float | random number between 0 and 100 |
| bool | random bool |
| time.Time | random time between 1970 and 2070 |
If you find a bug or want to suggest a new feature for testcraft, please open an issue on GitHub or submit a pull request. We welcome contributions from the community.