Testing in golang

Testing in Go is built directly into the language, making it remarkably straightforward. You don't need external frameworks; you use the standard testing package. You need to:
1. Create a test file with name ending with _test.go
2. Write test functions that start with Test

Create Function to be tested


// Initialize a New Go Module
$ go mod init test_go
$ ls -ltr
 go.mod
$ cat go.mod
module test_go

go 1.24.3

//Create function to be tested
$ vim main.go
package test_go
func Add(a, b int) int {
    return a + b
}
      

Create test file and test


$ cat main_test.go
package test_go

import "testing"

func TestAdd(t *testing.T) {
    result := Add(2, 3)
    expected := 5

    if result != expected {
        t.Errorf("Add(2, 3) = %d; want %d", result, expected)
    }
}

// Running the test
$ go test
PASS
ok      test_go 0.002s