An encyclopedia of test values, fakes, helpers, and validators for Go - so you can type less and test more.
Are you tired of writing the same three lines of code to create a url.URL for example.com?
How about repeatedly copying the same "canceled context" setup to check timeout logic?
Or maybe you want to generate a random set of bytes quickly?
Same here, and that's why I got lazy and created TestLazy.
TestLazy is a growing collection of pre-built test values, fakes, and validators for Go - made to reduce boilerplate in your tests. Instead of writing the same four lines every time you need a malformed URL or a canceled context, you can use TestLazy to get those values with a single function call.
- Use canonical values (
url.URL,net.IP, etc.) without the setup. - Simulate broken things (like a canceled context) with a single function call.
- Validate common responses (like HTTP status codes) with built-in validators.
Whether you're testing APIs or database interactions or don't want to think about test values, TestLazy has you covered.
Instead of this:
// Create a URL for example.com
url, err := url.Parse("https://example.com")
if err != nil {
t.Fatalf("Failed to parse URL: %v", err)
}
// Create HTTP Request
req := &http.Request{
Method: "GET",
URL: url,
}You can now just do this:
req := &http.Request{
Method: "GET",
URL: testurl.URLHTTPS(),
}Or, maybe you want a malformed URL:
req := &http.Request{
Method: "GET",
URL: testurl.URLInvalidHost(),
}Coordinating background goroutines without brittle sleeps? Use a tiny, thread-safe counter that can signal when a condition is met.
// Create a counter.
c := counter.New()
// Kick off work in the background.
go func() {
for i := 0; i < 5; i++ {
c.Increment()
time.Sleep(5 * time.Millisecond)
}
}()
// Wait until value >= 5 or time out.
if err := <-c.WaitAbove(5, time.Second); err != nil {
t.Fatalf("timed out waiting for counter: %v", err)
}Force cancel-aware code paths without wiring up context.WithCancel every time.
ctx := fakectx.Cancelled()
if err := doSomething(ctx); err == nil {
t.Fatalf("expected failure for canceled context")
}
<-ctx.Done() // already closedTestLazy is a set of individual packages; each focused on a specific category of test value or functionality. It allows you to take on only the dependencies you need.
More is on the way - TestLazy is just getting started. But if you cannot bear to write the same test setup code repeatedly, contributions (pull requests) and suggestions (issues) are welcome! Let's make testing less of a chore and more of a breeze. 🏝️
