Search⌘ K
AI Features

Testing: A Concrete Example

Explore how to create and run test functions in Go using practical examples that verify even and odd number logic. Learn to handle test failures, use Go's testing package effectively, and apply best practices including normal, abnormal, and boundary case testing to ensure robust Go code.

We'll cover the following...

Here is a concrete example for you to ...

package even
import "testing"

func TestEven(t *testing.T) {
    if !Even(10) {
        t.Log(" 10 must be even!")
        t.Fail()
    }
    if Even(7) {
        t.Log(" 7 is not even!")
        t.Fail()
    }
}

func TestOdd(t *testing.T) {
    if !Odd(11) {
        t.Log(" 11 must be odd!")
        t.Fail()
    }
    if Odd(10) {
        t.Log(" 10 is not odd!")
        t.Fail()
    }
}

This is our test program: it imports the even package at line 4, which will contain the ...