Skip to content

Document self-contained headers for inline doctest tests on MSVC #64

Closed

Description

@sean-parent

Documentation request

Document that each translation unit containing inline tests must include the
headers needed by its assertions and diagnostic formatting. The generated test
runner's includes cannot supply definitions to other translation units.

Observed with cpp-library 6.2.0

While migrating cel-runtime's private tests from standalone doctest executables
into implementation files, Windows/MSVC compilation failed when string and
std::string_view assertion diagnostics instantiated stream insertion with an
incomplete std::basic_ostream.

The standalone doctest implementation-with-main path had supplied stream
definitions transitively. Inline tests use doctest declarations in separate
translation units, so that incidental dependency disappeared. Adding <ostream>
to one affected translation unit did not fix the other: both value_type.cpp
and callable.cpp needed their own include.

The fix keeps the include inside the testing selector:

#include <cpp-library/testing.hpp>
#if CPP_LIBRARY_TESTING()
#include <doctest/doctest.h>
#include <ostream>
#endif

This is the include pattern used by the repair, not a standalone reproducer.
Production builds remain free of these test-only includes.

Suggested guidance

Add a migration note and an inline-testing example explaining:

  • Keep assertion and diagnostic dependencies local to each test translation unit.
  • Include <ostream> directly when diagnostics require standard stream insertion,
    even if the old standalone tests compiled without it.
  • Keep test-only headers behind #if CPP_LIBRARY_TESTING().
  • Do not move doctest implementation/main macros into production source files.

This request is for documentation, not for unconditionally adding <ostream>
to all production translation units.

Evidence

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions