4 ms·
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 thos
by daemin 3y ago
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.