Writing Tests for the Markdown Preview Tool

When we tested the to-do tool previously, we wrote something similar to an integration test by compiling the tool and running it in the test cases. This was necessary as all the code was part of the main() function, which can’t be tested. For this application, we’ll take a different approach: we’ll write individual unit tests for each function, and use an integration test to test the run() function. We can do this now because the run() function returns values that can be used in tests.

This means we’re intentionally not testing some of the code that’s still in the main() function, such as the block that parses the command-line flags. We don’t have to write tests for that code because we can assume it’s already been tested by the Go team. When using external libraries and packages, trust that they’ve been tested by the developers who provided them. If we don’t trust the developers, we don’t use the library.

Get hands-on with 1200+ tech skills courses.