testlazy

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2025 License: Apache-2.0 Imports: 0 Imported by: 0

README

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

Documentation

Overview

Package testlazy is your test helper sidekick—an ever-growing collection of canonical values, handy fakes, and smart validators so you can write tests faster and with less boilerplate.

Why waste precious brainpower on repetitive setup when testlazy has your back? Pick only the packages you need and focus on your test logic, not the plumbing.

Current modules:

  • things/testurl: Pre-built URL helpers for every HTTP scenario.
  • fakes/fakectx: Easy-to-use cancelled or timed-out contexts.
  • validators: Common checkers for HTTP status codes, headers, and more. (coming soon)

Quick Start

URLs without the boilerplate (things/testurl):

Instead of manual parsing...

url, err := url.Parse("https://example.com")
if err != nil {
    t.Fatalf("failed to parse URL: %v", err)
}
req := &http.Request{
    Method: "GET",
    URL:    url,
}

...just grab a ready-made URL:

req = &http.Request{
    Method: "GET",
    URL:    testurl.URLHTTPS(),
}

And for a malformed URL to test error handling:

req = &http.Request{
    Method: "GET",
    URL:    testurl.URLInvalidHost(),
}

Contexts without the hassle (fakes/fakectx):

Instead of manually cancelling contexts...

ctx, cancel := context.WithCancel(context.Background())
cancel()

err := doSomething(ctx)
if err == nil {
    t.Fatal("expected error for canceled context, got nil")
}

...use a one-liner fake:

err := doSomething(fakectx.Cancelled())
if err == nil {
    t.Fatal("expected error for canceled context, got nil")
}

For more details and examples, see the docs in each package.

Directories

Path Synopsis
fakes
fakectx module
helpers
counter module
things
testurl module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL