Go Testing: Difference between revisions
Jump to navigation
Jump to search
Line 8: | Line 8: | ||
Go comes with a lightweight test framework that includes the [[Go_Tool#test|<code>go test</code>]] command and the <code>[[Go Package testing#Overview|testing]]</code> package. The tests live in <code>*_test.go</code> files. | Go comes with a lightweight test framework that includes the [[Go_Tool#test|<code>go test</code>]] command and the <code>[[Go Package testing#Overview|testing]]</code> package. The tests live in <code>*_test.go</code> files. | ||
=Write a Unit Test= | =<span id='Writing_a_Unit_Test'></span>Write a Unit Test= | ||
Write a module as shown here: {{Internal|Go_Modules#Declaring_Modules|Declaring a Module}} | Write a module as shown here: {{Internal|Go_Modules#Declaring_Modules|Declaring a Module}} |
Revision as of 01:20, 14 September 2023
External
Internal
Overview
Go comes with a lightweight test framework that includes the go test
command and the testing
package. The tests live in *_test.go
files.
Write a Unit Test
Write a module as shown here:
TO DEPLETE
Writing a Unit Test
- Write a _test.go test file. They should belong to the same package as the tested code. These files are ignored by the compiler and only compiled and executed when go test is run.
- Import "testing"
- Tests are identified as functions starting with Test_... and taking an argument (t *testing.T).
package blue import "testing" func TestBlue(t *testing.T) { ... t.Error("expected this, got ", ...) }
Then
go test
The command will look for any tests in any of the files in the current folder and run them.
Also see external test packages.
TODO testing idiom "Introducing Go" page 96.