Testing โ Living Without RSpec
Go's testing is built into the standard library. No gem install needed.
Anyone who's set up a Ruby test environment knows. Install RSpec, configure spec_helper.rb, set up FactoryBot, add DatabaseCleaner, connect shoulda-matchers... 30 minutes before your first test line runs.
Go? Create a _test.go file and run go test. Done.
Filename Is the Convention
Tests for user.go go in user_test.go. Files ending in _test.go are only compiled during go test. Instead of Ruby's separate spec/ directory, test files sit next to source files.
RSpec's describe/it vs Go's Test Functions
Ruby:
RSpec.describe User do
it "has a name" do
expect(user.name).to eq("kim")
end
end
Go:
func TestUserName(t *testing.T) {
user := User{Name: "kim"}
if user.Name != "kim" {
t.Errorf("expected kim, got %s", user.Name)
}
}
If you're used to RSpec's DSL, Go's testing looks primitive. No expect/to matchers. Direct if-statement comparisons. This is Go's "standard library is enough" philosophy.
Table-Driven Tests โ Go's Core Pattern
Go defines test cases as struct slices for multiple cases:
tests := []struct{ input int; want int }{
{1, 2}, {2, 4}, {3, 6},
}
Similar role to RSpec's shared_examples.
go test -v, -run, -cover
go test -v โ verbose (like RSpec --format documentation)
go test -run TestUser โ run specific tests (like rspec spec/models/user_spec.rb)
go test -cover โ coverage check (like simplecov)
All built-in, no gems needed.
Ruby to Go
Ruby: spec/ directory + RSpec gem โ Go: _test.go files (built-in, no gems)
Ruby: describe/it/expect โ Go: func TestXxx(t *testing.T) + if + t.Errorf
Ruby: shared_examples โ Go: table-driven tests (struct slices)
Ruby: rspec --format doc โ Go: go test -v / -run / -cover (all built-in)
Pros
- ✓ Zero setup โ no gem install, spec_helper, or .rspec file needed
- ✓ go test -cover is built-in โ coverage check without SimpleCov
Cons
- ✗ No readable matchers like RSpec's expect().to eq() โ if statements are verbose
- ✗ No test data generation tool like FactoryBot in standard โ must implement manually