A Go library for filling struct fields with test data using struct tags.
// Define test data in struct tags
type User struct {
Name string `testfill:"John Doe"`
Age int `testfill:"30"`
Email string `testfill:"[email protected]"`
Address Address `testfill:"fill"`
}
// Fill struct with tagged values
user, _ := testfill.Fill(User{})
go get github.com/fabioelizandro/testfill
type Product struct {
Name string `testfill:"Widget"`
Price float64 `testfill:"99.99"`
InStock bool `testfill:"true"`
Quantity int `testfill:"100"`
}
product, _ := testfill.Fill(Product{})
// Result: {Name:Widget Price:99.99 InStock:true Quantity:100}
Only zero-valued fields are filled. Existing values are preserved:
config, _ := testfill.Fill(Config{Port: 3000}) // Port already set
// Result: {Host:localhost Port:3000} // Host filled, Port preserved
type User struct {
Name string `testfill:"Jane Doe"`
Address Address `testfill:"fill"`
}
user, _ := testfill.Fill(User{})
// Recursively fills Address fields
type TestData struct {
Tags []string `testfill:"go,testing,automation"`
Users []User `testfill:"fill:3"`
Variants []User `testfill:"variants:admin,user,guest"`
}
type User struct {
Name string `testfill:"John" testfill_admin:"Jane" testfill_guest:"Bob"`
Role string `testfill:"user" testfill_admin:"admin" testfill_guest:"guest"`
}
adminUser, _ := testfill.FillWithVariant(User{}, "admin")
// Result: {Name:Jane Role:admin}
testfill.RegisterFactory("uuid", func() string {
return uuid.New().String()
})
type Document struct {
ID string `testfill:"factory:uuid"`
}
type Config struct {
Settings map[string]interface{} `testfill:"unmarshal:{\"theme\":\"dark\"}"`
Tags []string `testfill:"unmarshal:[\"go\",\"testing\"]"`
}
// Fill struct with default values
user, err := testfill.Fill(User{})
// Fill with specific variant
adminUser, err := testfill.FillWithVariant(User{}, "admin")
// Panic versions
user := testfill.MustFill(User{})
adminUser := testfill.MustFillWithVariant(User{}, "admin")
testfill:"value"
- Basic valuetestfill:"fill"
- Fill nested structtestfill:"val1,val2,val3"
- Slice valuestestfill:"fill:3"
- Generate 3 structstestfill:"variants:admin,user"
- Use variantstestfill:"factory:name:arg1:arg2"
- Factory functiontestfill:"unmarshal:{\"key\":\"value\"}"
- JSON data
Supported: primitives, slices, maps, pointers, nested structs, time.Time
Not supported: interfaces, channels, functions, unexported fields
_, err := testfill.Fill(Invalid{})
// Returns descriptive error messages for type conversion failures