2

I am writing some unit tests for a function that reads in and parses data from text files, and it got me wondering: what is the best way to specify paths for external text files in the Catch2 unit testing framework?

I couldn't find it in the docs, and, even though they are a few questions similar to this one (e.g. here), none pertain to Catch2 specifically.

Taylor
  • 1,797
  • 4
  • 26
  • 51

1 Answers1

0

How about having a subdirectory where the files in question live? However if your build tree is separate from your source tree then you need to get your build system to either copy them into the build tree, or somehow magically figure out the path where they are. The first thing that pops into my head for achieving the latter would be to add the source tree location as a command line argument by brewing your own main in Catch 2 and using your build system to populate it when invoking tests.

One way to avoid having to couple your test with external files would be to adhere to the single responsibility principle and split the function in two modules: one that reads the files and then "feeds" the data to a module that parses it.

Testing the parsing logic is them simple: you can just have random strings in your unit test source file removing the need to manage external files.

In that case you can also design your classes/logic to allow you to stub/fake/mock the calls that deal with the file system.

Pesho_T
  • 814
  • 1
  • 6
  • 18