Writing Tests for the Markdown Preview Tool

Powerful Command-Line Applications in Go — by Ricardo Gerardi (31 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Creating a Basic Markdown Prev iew Tool | TOC | Adding Temporary Files to the Markdown Preview Tool 👉

When you tested the todo tool in ​Testing the Initial CLI Implementation​, you 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, you’ll take a different approach; you’ll write individual unit tests for each function, and use an integration test to test the run function. You can do this now because the run function returns values that can be used in tests.

This means you’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. You don’t have to write tests for that code because you can assume it’s already been tested by the Go team. When using external libraries and packages, trust that they have been tested by the developers who provided them. If you don’t trust the developers, don’t use the library.

This also means you don’t need to write unit tests for the saveHTML function since it’s essentially a wrapper around a function from Go’s standard library. Its behavior is assured in the integration test you’ll write.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.