How to write unit tests in C++ relying on non-code files?
sandordargo.com
sandordargo.com
we use sqlite as our datatbase so it is simple enough for helpers to apply a schema to an in memory database and again the database can be used in tests.
i'he become against mocks in tests. A usefull tool, but rarely needed. You can make tests reliable and fast without them.
https://github.com/DataDog/dd-trace-cpp/blob/6dacac2922d08f4...
It's not secure, but I'm not worried about bad actors monitoring /tmp in my test container so they can make my tests fail anyway.
It would be nice if there were a "file paths that start with a null character belong to an ad hoc in memory file system specific to the process," or something. /tmp/tmp.eJaVgxtV0x/ is close enough.
Open to code golfing suggestions on the code linked above.
Good luck with that when you’re dealing with external APIs. And numerous systems such as Postgres, redis, Kafka.
Stil possible to boot a complete environment up with docker test containers, but then your doing an integration test.
In my experience they are useful but there is no replacements for fakes inside unit tests.
I do not mock stuff (with things like mock into). I use fake implementations and interfaces.
I don't consider the distinction between unit and integration tests useful. If the bug is in Postgress then I want to find that out, and I care if the test is technically an integration test, I care that my software doesn't work.
Great for quick fixes though, it's crazy to be able to locally mount a disk image that sits on the other side of the world on an HTTP server.
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.
This works so well, we actually package a library with our binary that contains the unit tests so the end user can run them locally without having to download/build anything. This has been super useful in identifying calculation differences between Linux/Windows/MacOS and processors. When a user reports an issue, we can ask them to run the unit tests just by running (./binary --test) and then give us the results.
1. https://cmake.org/cmake/help/latest/prop_test/WORKING_DIRECT...
2. https://cmake.org/cmake/help/latest/command/configure_file.h...
3. https://cmake.org/cmake/help/latest/prop_test/FIXTURES_SETUP...
I have also seen horrific but effective test harness code that override open/close/read/write from libc.