Rodrigo Martins

I'm a Catholic Christian, software developer, Passionate about life, codes, questions, theories and theorems. :)

[Go] Simple Tests #testing #go

| Comments

Hi,

Let’s talk about simple tests using Go with testing package.

Let’s get a file simplemath.go by my gist:

Our test

With this file basic, go to our test.

Create test folder, and simplemath_test.go in this folder.

We will need of testing package.

Import testing
1
2
3
import (
  "testing"
)

And import the simplemath package.

Import simplemath package
1
2
3
4
import (
  "testing"
  "imartins/modulesimple/simplemodule/simplemath"
)

Remember of put in this file the first line the code: package simplemath.

Package simplemath in simplemath_test
1
package simplemath

Add()

Let’s test the Add() function.

Under the import of packets add the func.

Test Add() function
1
2
3
4
5
6
7
8
func TestAdd(t *testing.T) {
  t.Log("Called simplemath class and Add() method")
  c := simplemath.Add(5,8)

  if c != 13 {
      t.Errorf("Expected c of 13, but it was %d instead.", c)
  }
}

Running the test:

running
1
2
$ go test test/simplemath_test.go
ok    command-line-arguments  0.013s

Running the verbose format:

verbose
1
2
3
4
5
6
$ go test -v test/simplemath_test.go
=== RUN   TestAdd
--- PASS: TestAdd (0.00s)
  simplemath_test.go:9: Called simplemath class and Add() method
PASS
ok    command-line-arguments  0.011s

News tests with Multiply() and Subtract()

The simplemath_test.go file completed this down with the other test functions

Thanks guys! To the next post!

Comments