From charlesreid1

(Created page with "Testing in Go: Introduction to testing in Go: https://tutorialedge.net/golang/intro-testing-in-go/ Advanced Go testing tutorial: https://tutorialedge.net/golang/advanced-go-...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
Testing in Go:
=Basics=
 
==How testing works in Go==
 
For each file <code>X.go</code> a corresponding test named <code>X_test.go</code> should contain unit tests for that code.
 
To run tests, just use the built-in test command in go: <code>go test</code>
 
==Example==
 
===Example function to test===
 
Take this simple function as an example:
 
<pre>
package main
 
import (
"fmt"
)
 
// Return the square of a number
func square(x int) (result int) {
return x * x
}
 
func main() {
fmt.Println("Hello World")
}
</pre>
 
===Simple test===
 
We can write a corresponding test:
 
<pre>
// main_test.go
package main
 
import (
"testing"
)
 
func test_square(t *testing.T) {
if test_square(2) != 4 {
t.Error("Error testing square(): did not return correct result")
}
}
</pre>
 
===Test matrix===
 
We can implement a matrix of tests using a for loop:
 
<pre>
// main_test.go
package main
 
import (
"testing"
)
 
func test_squares(t *testing.T) {
var tests = []struct {
input    int
expected int
}{
{2, 4},
{-1, 1},
{0, 0},
{-5, 25},
{1000, 1000000},
}
 
for _, test := range tests {
output := Calculate(test.input)
                if output != test.expected {
t.Error("Test Failed: {} inputted, {} expected, received: {}", test.input, test.expected, output)
}
}
}
</pre>
 
=Resources=


Introduction to testing in Go: https://tutorialedge.net/golang/intro-testing-in-go/
Introduction to testing in Go: https://tutorialedge.net/golang/intro-testing-in-go/
Line 7: Line 90:




=Flags=


{{GoFlag}}
{{GoFlag}}

Latest revision as of 03:33, 13 December 2018

Basics

How testing works in Go

For each file X.go a corresponding test named X_test.go should contain unit tests for that code.

To run tests, just use the built-in test command in go: go test

Example

Example function to test

Take this simple function as an example:

package main

import (
	"fmt"
)

// Return the square of a number
func square(x int) (result int) {
	return x * x
}

func main() {
	fmt.Println("Hello World")
}

Simple test

We can write a corresponding test:

// main_test.go
package main

import (
	"testing"
)

func test_square(t *testing.T) {
	if test_square(2) != 4 {
		t.Error("Error testing square(): did not return correct result")
	}
}

Test matrix

We can implement a matrix of tests using a for loop:

// main_test.go
package main

import (
	"testing"
)

func test_squares(t *testing.T) {
	var tests = []struct {
		input    int
		expected int
	}{
		{2, 4},
		{-1, 1},
		{0, 0},
		{-5, 25},
		{1000, 1000000},
	}

	for _, test := range tests {
		output := Calculate(test.input)
                if output != test.expected {
			t.Error("Test Failed: {} inputted, {} expected, received: {}", test.input, test.expected, output)
		}
	}
}

Resources

Introduction to testing in Go: https://tutorialedge.net/golang/intro-testing-in-go/

Advanced Go testing tutorial: https://tutorialedge.net/golang/advanced-go-testing-tutorial/


Flags