Testing Commands with Mock Resources
So far, you’ve been testing the external commands by executing them directly. This is a perfectly valid approach. But sometimes it’s not desirable or possible to execute a command directly on the machine where you’re testing the code. For these cases, you’ll mock the external commands by using Go function during tests. This approach also allows you to use Go code to simulate abnormal conditions, such as timeouts, which are harder to simulate using external services. Go’s standard library applies this approach to test the function from the os/exec package. For more information check the source code for the exec_test.go file from the standard library.[36]
To use this approach you’ll write a test function that ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access