Junior — Middle
What amount of code testing do you usually implement with unit tests?
sobes.tech AI
Answer from AI
Unit tests in Go typically cover individual functions or methods, verifying their correctness in isolation from the rest of the system. I aim to cover critical and frequently used parts of the code with unit tests, as well as all business logic branches. Usually, this accounts for 70-90% of the code if the architecture allows for easy modular testing. For example, for a function that processes data, I write tests for different input values, including boundary cases and errors.
Here's a simple example of a unit test in Go:
func Add(a, b int) int {
return a + b
}
func TestAdd(t *testing.T) {
result := Add(2, 3)
if result != 5 {
t.Errorf("Expected 5, got %d", result)
}
}
Thus, unit tests help quickly identify errors and maintain code quality.