python runtime_error ai_generated true

预期: {'id': 1, 'name': 'Alice', 'created': '2024-01-01T00:00:00'} 实际: {'id': 1, 'name': 'Alice', 'created': '2024-01-01T00:00:00.123456'}

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

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-09-14首次发现

版本兼容性

版本状态引入弃用备注
7.x active
8.x active

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

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

  2. 75% 失败

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