How to write unit tests in C++ relying on non-code files?

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • dd-trace-cpp

    Datadog distributed tracing for C++

  • 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.

  • Jimfs

    An in-memory file system for Java 7+

  • Java has in-memory file systems that are essentially geared for this exact use case, eg jimfs[0]. You create your filesystem and any files you need when your tests are starting up, and your classes talk to them rather than the “real” ones. Maybe a similar project exists for the C++ ecosystem?

    [0] https://github.com/google/jimfs

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • Bazel

    a fast, scalable, multi-language and extensible build system

  • This is a problem that Bazel (https://bazel.build) solves in a very convenient way. You can just keep using the paths relative to the repository root, and as long as you properly declare your test needs that file it will access it without problems. Or you can use the runfile libraries to access them too.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts