News for hackers. Stuff that matters.
Re:How to write unit tests in C++ relying on non-code files? by daemin on Friday February 23, 2024 @04:48AM (#39478763)
As others have suggested the code should be refactored for testing so that the opening and reading of the file contents is separated from the processing of those contents, and futher separated from opening and writing out the converted text. Then each of these can be tested in isolation, with the conversion tests just specifying the input and output text inline in each test.

If you're relying on external files or an external environment then you're not writing unit tests but integration tests, since you're integrating multiple components together.

Edit: Another option to test this sort of code is to just write a simple command line program where you pass the input and output file as parameters. Then you can use an external testing framework (like CTest) or tool (like a simple script) to run through the input text files and compare them to expected output text files.

There are no comments on this comment yet.