Go Testing: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 13: Line 13:


=TO DEPLETE=
=TO DEPLETE=
<font color=darkkhaki>To deplete, merge and delete this: [[Go Concepts - Testing]]</font>
<font color=darkkhaki>
 
 
==Writing a Unit Test==
 
* Write a <tt>_test.go</tt> 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 <tt>go test</tt> is run.
 
* Import "testing"
 
* Tests are identified as functions starting with <tt>Test_...</tt> and taking an argument <tt>(t *testing.T)</tt>.
 
<pre>
package blue
 
import "testing"
 
func TestBlue(t *testing.T) {
    ...
    t.Error("expected this, got ", ...)
}
</pre>
 
Then
 
<pre>
go test
</pre>
 
The command will look for any tests in any of the files in the current folder and run them.
 
<font color=red>'''TODO''' testing idiom "Introducing Go" page 96.</font>

Revision as of 21:27, 8 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.

Also see external test packages.

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.

TODO testing idiom "Introducing Go" page 96.