Skip to content

madflojo/testlazy

Repository files navigation

TestLazy 😴

TestLazy Logo

An encyclopedia of test values, fakes, helpers, and validators for Go - so you can type less and test more.

GitHub go.mod Go version codecov Go Reference Go Report Card


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.


🧪 What is 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.


🚀 Getting Started

URLs without the boilerplate | github.com/madflojo/testlazy/things/testurl

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(),
}

Counters for async tests | github.com/madflojo/testlazy/helpers/counter

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)
}

Contexts without manual cancellation | github.com/madflojo/testlazy/fakes/fakectx

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 closed

🧱 Structure

TestLazy 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.

Package Description Go Package Reference
github.com/madflojo/testlazy/things/testurl Pre-built URLs for common use cases Go Reference
github.com/madflojo/testlazy/helpers/counter Test-focused, thread-safe counter Go Reference
github.com/madflojo/testlazy/fakes/fakectx Ready-made contexts for cancellation/deadline tests Go Reference

🦥 Stay Lazy, Test More!

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. 🏝️

About

An encyclopedia of test values and fakes for Go. Type less, test more.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors