python runtime_error ai_generated true

Expected: {'id': 1, 'name': 'Alice', 'created': '2024-01-01T00:00:00'} Got: {'id': 1, 'name': 'Alice', 'created': '2024-01-01T00:00:00.123456'}

ID: python/pytest-doctest-ellipsis-mismatch

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-09-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7.x active
8.x active

Root Cause

Doctest compares the exact repr of the output by default. Non-deterministic values (timestamps, UUIDs, memory addresses, floats) differ between runs, so the doctest fails intermittently.

generic

中文

Doctest 默认比较输出的精确 repr。非确定性值(时间戳、UUID、内存地址、浮点数)在运行之间不同,因此 doctest 会间歇性失败。

Workarounds

  1. 95% success
    >>> get_user(1)  # doctest: +ELLIPSIS
    {'id': 1, 'name': 'Alice', 'created': '...'}
  2. 92% success
    # In conftest.py or pytest.ini:
    # doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE
    
    >>> get_user(1)  # doctest: +ELLIPSIS
    {'id': 1, 'name': 'Alice', 'created': '...'}

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Skips the whole example, including the parts that are deterministic and worth testing.

  2. 75% fail

    Doctest does not support regex natively; you'd need a custom OutputChecker, which is heavy for one example.