計算機科学のブログ

Go - code quality assurance - Automated Testing - Testing actual return values

Head First GoJay McGavren(著)、O’Reilly Media)の Chapter 14(code quality assurance - Automated Testing)、p.409(Exercise)の解答を求めてみる。

コード

arithmetic/math.go

package arithmetic

func Add(a float64, b float64) float64 {
	return a + b
}
func Subtract(a float64, b float64) float64 {
	return a - b
}

コード

arithmetic/math_test.go

package arithmetic

import "testing"

func TestAdd(t *testing.T) {
	if Add(1, 2) != 3 {
		t.Error("1 + 2 did not equal 3")
	}
}
func TestSubtract(t *testing.T) {
	if Subtract(8, 4) != 4 {
		t.Error("8 - 4 did not equal 4")
	}
}

入出力結果(Terminal, Zsh)

% go test
PASS
ok  	arithmetic	0.204s
% go test -v
=== RUN   TestAdd
--- PASS: TestAdd (0.00s)
=== RUN   TestSubtract
--- PASS: TestSubtract (0.00s)
PASS
ok  	arithmetic	0.190s
% cat go.mod 
module arithmetic

go 1.23.4
%