DevOps Articles

Curated articles, resources, tips and trends from the DevOps World.

Writing Good Unit Tests; Don’t Mock Database Connections

4 years ago qvault.io

Summary: This is a summary of an article originally published by the source. Read the full original article here →

More importantly, unit tests allow us to make updates to our code base with the confidence that we haven’t broken anything. In our eagerness to get 100% code coverage, however, we often write tests for logic that perhaps we have no business testing.

In Go, unit tests can be executed using the go test command and some example source code looks something like this: Where we are testing the logPow function that looks like this: The goal of unit tests is to test a “unit”, or small portion, of code.

The only test we probably need here is to test our own business logic, the validateUser function.

I believe that the majority of database and API mocking is a result of bad code, and that to fix the problem, the best course of action is to refactor into smaller functions or “units” as we did a moment ago.

Made with pure grit © 2024 Jetpack Labs Inc. All rights reserved. www.jetpacklabs.com