Separate Test Cases in Golang With Build Tags

Separate Test Cases in Golang With Build Tags

A build tag or a build constraint as described in the build package documentation, is a line comment that begins // +build that lists the conditions under which a file should be included in the package. You can provide build constraints for one file. The tags can then be passed to the go build command, […]

A build tag or a build constraint as described in the build package documentation, is a line comment that begins // +build that lists the conditions under which a file should be included in the package.

You can provide build constraints for one file. The tags can then be passed to the go build command, when building the package, like so

$ go build -tags <tags>
$ go test -tags=integration

Also This will allow us to separate test cases by separating them in different files and then giving each file a special build tag. here is an example:

// main.go file

package main

func main() {
    //
}

// Sum ...
func Sum(ar []int) int {
    result := 0

    for _, v := range ar {
        result += v
    }

    return result
}
// main_unit_test.go file

// +build unit

package main

import (
    "testing"

    "github.com/franela/goblin"
)

// TestUnitSum test cases
func TestUnitSum(t *testing.T) {
    g := goblin.Goblin(t)

    g.Describe("#SumFunc", func() {
        g.It("It should satisfy all provided test cases", func() {
            var tests = []struct {
                input  []int
                wantResult int
            }{
                {[]int{1, 2, 3, 4, 5}, 15},
                {[]int{3, 4, 5}, 12},
                {[]int{36, 98, 63, 3628, 2863, 27392, 883929}, 918009},
                {[]int{36, 21, 63, 12, 56, 90, 32}, 310},
                {[]int{123, 283, 345, 544, 332, 4455, 1233}, 7315},
            }

            for _, tt := range tests {
                g.Assert(Sum(tt.input)).Equal(tt.wantResult)
            }
        })
    })
}

// BenchmarkUnitSum benchmark
func BenchmarkUnitSum(b *testing.B) {
    var input []int

    for n := 0; n < b.N; n++ {
        input = append(input, n)
    }

    b.Logf("[Unit] Sum function input length %v \n", len(input))

    Sum(input)
}

golang testing testing

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Testing Microservices Applications

The shift towards microservices and modular applications makes testing more important and more challenging at the same time. Learn more here.

How to Do API Testing?

API endpoint when you send the get request to that URL it returns the JSON response. In this article, I am going to use postman assertions for all the examples since it is the most popular tool. But this article is not intended only for the postman tool.

Top 10 Automation Testing Tools: 2020 Edition

The demand for delivering quality software faster — or “Quality at Speed” — requires organizations to search for solutions in Agile, continuous integration (CI), and DevOps methodologies. Test automation is an essential part of these aspects.

Where To Learn Test Programming — July 2020 Edition

What do you do when you have lots of free time on your hands? Why not learn test programming strategies and approaches?

Testing Microservices Applications

In this article, look at different ways to test microservices and how you can have a suitable testing strategy to begin with.