Skip to content

Go Test Short

DodaTech 1 min read

In this tutorial, you'll learn about Go Test: Skip Slow Tests. We cover key concepts, practical examples, and best practices.

Short mode -- Use testing.Short() to skip slow tests during development with go test -short.

The Problem

Integration tests that connect to databases or external services are too slow for frequent development cycles. Check testing.Short() to conditionally skip them.

Wrong

func TestDatabaseIntegration(t *testing.T) {
    db := connectToDatabase() // Slow!
    // ... test
}

Output:

// Runs every time, even during quick dev cycles
func TestDatabaseIntegration(t *testing.T) {
    if testing.Short() {
        t.Skip("Skipping database test in short mode")
    }
    db := connectToDatabase()
    // ... test
}

Output:

$ go test -short -v ./...
// SKIP TestDatabaseIntegration (0.00s)
$ go test -v ./...
// PASS TestDatabaseIntegration (3.25s)

Prevention

  • Check testing.Short() in slow tests
  • Use t.Skip("reason") to skip
  • Short tests run with go test -short
  • Normal tests need integration flags
  • Use build tags for very slow tests

Common Mistakes with test short

  1. Mixing let bindings with <- bindings in do notation, producing type errors
  2. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  3. Non-exhaustive pattern matches that compile with warnings then crash at runtime

These mistakes appear frequently in real-world GO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

**How to run only short tests?**

go test -short. Only tests not checking Short() run.

Should unit tests use Short()?

No. Unit tests should be fast enough to always run.

Can I use build tags instead?

Yes. //go:build integration is another approach.


Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tutorials help Go developers build production-ready software used by millions.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro