Using Interfaces to Automate Tests
Sometimes you need a way to test output printed out to STDOUT. In this instance, when executing the integration tests, by testing the run function, the name of the output file is created dynamically. The function prints this value to the screen so the user can use the file, but to automate tests, we need to capture this output from within the test case.
In Go, the idiomatic way to deal with this situation is by using interfaces, in this case io.Writer, to make your code more flexible. For this pattern, we update the function run so that it takes the interface as an input parameter. We do this so we can call run with different types that implement the interface depending on the situation: for the program, we ...
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