
Testing
testing package, test tables, benchmarks, examples, coverage, testify, mocking
1What is the naming convention for a test file in Go?
What is the naming convention for a test file in Go?
Answer
In Go, a test file must have the _test.go suffix and be placed in the same package as the code being tested. For example, to test main.go, create main_test.go. This convention allows the go test command to automatically discover test files without additional configuration.
2What is the correct signature for a test function in Go?
What is the correct signature for a test function in Go?
Answer
A test function in Go must start with Test, take a single parameter of type *testing.T, and return nothing. The name must start with a capital letter to be exported. For example, func TestAdd(t *testing.T). The t parameter allows signaling failures with t.Error, t.Fail, t.Fatal, etc.
3What is the difference between t.Error and t.Fatal?
What is the difference between t.Error and t.Fatal?
Answer
t.Error marks the test as failed but continues test execution, allowing detection of multiple errors. t.Fatal marks the test as failed and immediately stops execution of the current test. Use t.Fatal when a critical precondition fails and continuing makes no sense, such as a setup failure or unexpected nil value.
How to run only a specific test named TestAdd?
What is a table-driven test in Go?
+19 interview questions
Other Go interview topics
Go Basics
Go Data Structures
Go Interfaces
Error Handling
Goroutines Basics
Channels
Go Modules
HTTP Server
HTTP Client
JSON Encoding
database/sql
Context Package
Concurrency Patterns
Sync Primitives
Go Web Frameworks
REST API Design
gRPC
Reflection
Memory Management
Performance Optimization
Generics
Go Design Patterns
Microservices
Security & Authentication
Docker & Containerization
Kubernetes Basics
Advanced Go
CLI Development
Master Go for your next interview
Access all questions, flashcards, technical tests, code review exercises and interview simulators.
Start for free