-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcode_test.go
More file actions
65 lines (39 loc) · 1.6 KB
/
Copy pathcode_test.go
File metadata and controls
65 lines (39 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package unittests
import "testing"
/*
Files containing unit tests must have the suffix _test.go
By convention, the files have the same name as the file containing the code under test so this package
contains the files
code.go
code_test.go
Any code in files with the _test.go suffix will not be included in your final executable (likewise code that
is only imported and used by your tests)
Go includes a testing and benchmarking suite in the testing package:
https://golang.org/pkg/testing/
But it is very raw. The vast majority of projects use a third-party unit testing framework which builds on Go's
internal framework.
This article is a decent overview of the currently popular test frameworks
https://bmuschko.com/blog/go-testing-frameworks/
This file shows some of the features available in the basic testing package
*/
//Individual tests must have the method name func TestXxx(*testing.T)
//The testing package's support function are provided in the *testing.T object passed into the test
func TestSquare(t *testing.T) {
m := Math{}
result := m.Square(4)
//There are no built-in assert methods!
if result != 16 {
//t.Fail() marks this test failed without a message and continues execution
//t.FailNow() marks this test failed and exits this test immediately
//t.Fatalf calls FailNow _and_ logs an error message
t.Errorf("Expected 16 but found %d", result) //Calls Fail() and logs an error message
}
}
func TestBrokenSquare(t *testing.T) {
m := Math{}
result := m.BrokenSquare(1)
expected := 1
if result != expected {
t.Fatalf("Expected %d, found %d", expected, result)
}
}