Sobes.tech
Senior

What types of tests have you used? Unit, Mock, Integration, Test Containers?

sobes.tech AI

Answer from AI

In Go projects, I used several types of tests:

  • Unit tests — check individual functions or methods. In Go, this is usually done using the testing package. For example:
func Add(a, b int) int {
    return a + b
}

func TestAdd(t *testing.T) {
    result := Add(2, 3)
    if result != 5 {
        t.Errorf("expected 5, got %d", result)
    }
}
  • Mock tests — used to isolate dependencies, for example, when you need to mock a database interface or an external API. Libraries like gomock or testify/mock are used.

  • Integration tests — check the interaction of multiple components together. For example, testing with a real database or external service.

  • Test Containers — used to spin up temporary containers with databases or message brokers in tests to ensure an isolated environment. For example, testcontainers-go can be used to run a PostgreSQL container for integration tests.

This approach allows covering the code with different levels of testing and increases the reliability of the application.