Go Testing Examples 🎯

beginner
8 min

Go Testing Examples 🎯

Welcome to our comprehensive guide on Go Testing! In this lesson, we'll explore Go's testing framework and learn how to write effective tests for your Go applications. By the end of this tutorial, you'll be able to write, run, and understand Go tests. 💡 Pro Tip: Go testing is a powerful tool that helps you write reliable, maintainable code.

Table of Contents

  1. Why Testing is Important?
  2. Basic Testing Concepts 1.1. testing Package 1.2. Test Functions 1.3. Testing Variables
  3. Writing Your First Test
  4. Assertions 4.1. Equal Assertion 4.2. NotEqual Assertion 4.3. Error Assertion
  5. Test Benchmarks
  6. Testing Structs and Methods
  7. Test Fixtures
  8. Running Tests
  9. Test Coverage
  10. Quiz

1. Why Testing is Important? 📝

Testing is crucial for any software project. It helps ensure the reliability and quality of your code by identifying and fixing bugs, reducing the chances of errors, and making your code more maintainable.

2. Basic Testing Concepts

1.1. testing Package

The testing package is Go's built-in testing framework. It provides functions for writing and running tests.

1.2. Test Functions

Test functions are functions prefixed with Test that live inside the testing package. They are executed by the testing framework.

1.3. Testing Variables

Testing variables are temporary variables that are only accessible within the test function. They are prefixed with test and are used to store test data.

3. Writing Your First Test

Let's create a simple function that adds two numbers and write a test for it.

go
package main import "testing" func Add(x, y int) int { return x + y } func TestAdd(t *testing.T) { testCases := []struct { x, y int expected int }{ {1, 1, 2}, {2, 3, 5}, } for _, testCase := range testCases { result := Add(testCase.x, testCase.y) if result != testCase.expected { t.Errorf("Add(%d, %d) returned %d, expected %d", testCase.x, testCase.y, result, testCase.expected) } } }

In the above example, we define a simple function Add and a test function TestAdd. The TestAdd function contains a slice of test cases (testCases) and a loop that iterates through them. For each test case, it calls the Add function with the provided inputs and compares the result with the expected output using the t.Errorf function.

4. Assertions

Assertions are used to compare the actual result with the expected result. Go provides several built-in assertion functions.

4.1. Equal Assertion

The Equal assertion checks if two values are equal.

go
t.Equal(expected, actual)

4.2. NotEqual Assertion

The NotEqual assertion checks if two values are not equal.

go
t.NotEqual(expected, actual)

4.3. Error Assertion

The Error assertion checks if an error occurred.

go
err := SomeFunction() if err != nil { t.Error(err) }

5. Test Benchmarks

Benchmarks are used to measure the performance of your Go code. They are prefixed with Benchmark.

go
func BenchmarkAdd(b *testing.B) { for n := 0; n < b.N; n++ { Add(1, 1) } }

6. Testing Structs and Methods

Testing structs and methods involves creating test instances and calling the methods under test.

7. Test Fixtures

Test fixtures are reusable pieces of test setup and teardown logic. They help ensure that your tests are isolated from each other.

8. Running Tests

Go tests are run using the go test command.

9. Test Coverage

Test coverage measures the percentage of code that is covered by tests. Go provides tools to calculate test coverage.

10. Quiz

Quick Quiz
Question 1 of 1

Which Go function is used to run tests?