A program prints PASS, exits with code 0, and fails a test expecting PASS\n. The result looks correct in a terminal. The difference may be in the bytes the terminal uses to format the text.

When a test reads stdout, the formatting mode becomes part of its input conditions. That mode must reach the process producing the output. A variable in the shell environment is not enough when a container sits between the shell and the CLI.

Inspect the bytes first

These two sequences can display the same word:

b"PASS\n"
b"\x1b[32mPASS\x1b[0m\n"

The second sequence enables green before the word and resets formatting after it. These codes are documented in the xterm control sequence reference. A byte comparison sees different strings. Even output.startswith(b"PASS") fails: the first byte is 0x1b.

Printing repr(output) is enough to expose this distinction. It separates a change in content from a change in formatting before you edit the expected values in the test.

Removing control sequences before each comparison can be appropriate when testing only the content of a colored report. But that test no longer detects a violation of the promise to produce plain text. For a no-color mode, test the original bytes.

Set a no-color mode

The NO_COLOR convention calls for disabling default color when NO_COLOR exists and is nonempty. The value 0 also disables color. This applies to programs that support the convention. It does not remove ANSI sequences already printed or promise to disable bold and other styling.

The following status.py is an independent teaching example. The --fail flag only selects the sample result. The program deliberately adds color even when stdout is redirected, so the difference is visible without an interactive terminal.

import argparse
import os

parser = argparse.ArgumentParser()
parser.add_argument("--fail", action="store_true")
args = parser.parse_args()

label = "ERROR" if args.fail else "PASS"
if not os.environ.get("NO_COLOR"):
    color = "31" if args.fail else "32"
    label = f"\x1b[{color}m{label}\x1b[0m"

print(label)
raise SystemExit(1 if args.fail else 0)

This is a simplified formatting policy. In an existing CLI, also check the precedence of explicit options such as --color=always. Supporting NO_COLOR does not mean it overrides every option a program accepts.

The check_status.py check starts the CLI as a separate process and reads stdout without decoding it. It preserves the parent environment but sets NO_COLOR separately for each case:

import os
from pathlib import Path
import subprocess
import sys

cli = Path(__file__).with_name("status.py")


def run(*args, no_color=None):
    env = os.environ.copy()
    env.pop("NO_COLOR", None)
    if no_color is not None:
        env["NO_COLOR"] = no_color
    return subprocess.run(
        [sys.executable, str(cli), *args],
        env=env,
        capture_output=True,
        check=False,
    )


colored = run()
assert (colored.returncode, colored.stdout) == (
    0, b"\x1b[32mPASS\x1b[0m\n"
)
for value in ("1", "0"):
    passed = run(no_color=value)
    failed = run("--fail", no_color=value)
    assert (passed.returncode, passed.stdout) == (0, b"PASS\n")
    assert (failed.returncode, failed.stdout) == (1, b"ERROR\n")
assert run(no_color="").stdout == colored.stdout
print("6 cases passed")

Run it with:

python3 check_status.py

The checks cover two independent properties: the exit status and the exact message bytes. Missing and empty values leave color enabled. Nonempty values disable it for both successful and failed results.

Pass the setting through Compose

The command NO_COLOR=1 docker compose run ... passes the variable to the Compose process on the host. The container environment needs a separate setting. The Compose documentation describes environment, env_file, and run --env for passing variables.

Save compose.yaml next to the two Python files:

services:
  status:
    image: python:3.13-slim
    working_dir: /work
    volumes:
      - .:/work:ro
    environment:
      NO_COLOR: "${NO_COLOR:-}"
    command: ["python", "status.py"]

The value now passes through an explicit configuration field. Check the environment inside the container first, then the output of the target program:

NO_COLOR=1 docker compose run --rm -T status \
  python -c 'import os; print(repr(os.environ.get("NO_COLOR")))'

NO_COLOR=1 docker compose run --rm -T status > status.out
python3 -c 'from pathlib import Path; assert Path("status.out").read_bytes() == b"PASS\n"'

The first command should print '1'. If the variable is missing, investigate the container boundary. If it is present but color remains, check whether the CLI supports it and whether color was explicitly enabled. The -T option disables pseudo-terminal allocation. It is not a universal switch for color.

A test service that always needs plain output can use NO_COLOR: "1" directly. Host-variable interpolation is needed only when the service must accept that choice from outside.

The Python example and its six checks were run locally on Python 3.12.14. The Compose commands are provided to check the same boundary in a container; they were not run while preparing this draft.

When a program consumes stdout, the visible word does not describe the whole result. A testable CLI mode includes text, exit status, and a formatting policy. Pass that policy to the final process and verify it at the point where the test receives the bytes.