[Go] Simple Tests #testing #go


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

import (

And import the simplemath package.

Import simplemath package

import (

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

Package simplemath in simplemath_test

package simplemath


Let’s test the Add() function.

Under the import of packets add the func.

Test Add() function

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:

$ go test test/simplemath_test.go
ok command-line-arguments 0.013sRunning the verbose format:

Running the verbose format:

$ go test -v test/simplemath_test.go
=== RUN TestAdd
— — PASS: TestAdd (0.00s)
simplemath_test.go:9: Called simplemath class and Add() method
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!

Originally published at rrmartins.herokuapp.com on April 19, 2016.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.