News for hackers. Stuff that matters.
Re:How to write unit tests in C++ relying on non-code files? by bluGill on Thursday February 22, 2024 @09:23PM (#39476181)
I created helper functions in my test that create a full user directrory with whatever concents I want. then my tests can write whatever they want, the dir is deleted at the end of the test. Tempdir isn't safe againts an attacker, but it is good emough for tests. in the real world file systems are fast enough to use in tests.

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.

Re:How to write unit tests in C++ relying on non-code files? by MathMonkeyMan on Thursday February 22, 2024 @10:24PM (#39476594)
Yep, common use case for what I work on is BLAH_BLAH_THINGY_CONFIG_FILE in the environment being a path to some file that the library will open to read config, instead of having the config itself in the environment (maybe it's a large unwieldy JSON thing).

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.

Re:How to write unit tests in C++ relying on non-code files? by TeeWEE on Friday February 23, 2024 @12:19PM (#39483411)
> i'he become against mocks in tests. A usefull tool, but rarely needed. You can make tests reliable and fast without them

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.

Re:How to write unit tests in C++ relying on non-code files? by bluGill on Friday February 23, 2024 @12:24PM (#39483502)
Like I said there is a place for mocks. I haven't worked with the 3 tools you named, but I find that sqlite in-memory databases are easy to setup in my tests.

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.