Unit testing in Go is performed using the built-in testing package. It ensures that functions and methods produce the expected results when executed.
Create a test file named calculator_test.go:
package main
import (
"testing"
)
func add(a int, b int) int {
return a + b
}
func TestAdd(t *testing.T) {
result := add(2, 3)
expected := 5
if result != expected {
t.Errorf("Expected %d but got %d", expected, result)
}
}
go test
BaseRock AI streamlines Go unit testing by:
🚀🚀🚀
You're good to go!
Want to automate your Go unit tests effortlessly? Try BaseRock AI today!