【问题标题】:Python doctest: result with multiple linesPython doctest:多行结果
【发布时间】:2011-06-23 07:22:48
【问题描述】:

我无法让doctest 处理包含多行并且开头可能包含空行的结果。这可能是由缩进和解析问题引起的。我想出了一些解决方案:

  • 将想要的结果写入文件,doctest 比较结果和文件内容。
  • 将结果的哈希值与已知哈希值进行比较。这种方法的主要缺点是doctest 的读者对所需结果了解甚少。
  • 找到一种方法使 doctest 能够处理多行结果。
  • 使用unittest 而不是doctest。

有什么想法吗?

代码:

    >>> data_lists=[ {"Average execution" : [1, 2, 3, 2, 3]},
    ...                              {"Top execution"     : [3, 4, 5, 7, 8, 11, 6]},
    ...                              {"Current execution" : [1, 2, 1, 2, 1, 5]}       ]
    >>> c=Chart(data_lists,
    ...         ("Large<br>rooster", "Rsync rooster", "Pool<br>Chicken", "Disease"),
    ...         width=700, height=300)
    >>> print c.html.strip()
    <div id="placeholder3" style="width:700px;height:300px"></div>

    <script id="source" language="javascript" type="text/javascript">
    $(function () {

    var d0 = [[0, 1], [4, 2], [8, 3], [12, 2], [16, 3]];
    var d1 = [[1, 3], [5, 4], [9, 5], [13, 7], [17, 8], [21, 11], [25, 6]];
    var d2 = [[2, 1], [6, 2], [10, 1], [14, 2], [18, 1], [22, 5]];

        $.plot($("#placeholder3"), [

        {   label: "Average execution",  data: d0,   bars: { show: true }  },
        {   label: "Top execution",  data: d1,   bars: { show: true }  },
        {   label: "Current execution",  data: d2,   bars: { show: true }  }

        ],
        {
            xaxis: { ticks: [[1.5, "Large<br>rooster"], [5.5, "Rsync<br>rooster"], [9.5, "Pool<br>Chicken"], [13.5, "Disease"]] }
        }
        );
    });
    </script>

错误:

**********************************************************************
File "HTML.py", line 28, in __main__.Chart.__init__
Failed example:
    print c.html.strip()
Expected:
    <div id="placeholder3" style="width:700px;height:300px"></div>
Got:
    <div id="placeholder3" style="width:700px;height:300px"></div>
    <BLANKLINE>
        <script id="source" language="javascript" type="text/javascript">
        $(function () {
    <BLANKLINE>
        var d0 = [[0, 1], [4, 2], [8, 3], [12, 2], [16, 3]];
        var d1 = [[1, 3], [5, 4], [9, 5], [13, 7], [17, 8], [21, 11], [25, 6]];
        var d2 = [[2, 1], [6, 2], [10, 1], [14, 2], [18, 1], [22, 5]];
    <BLANKLINE>
            $.plot($("#placeholder3"), [
    <BLANKLINE>
            {   label: "Average execution",  data: d0,   bars: { show: true }  },
            {   label: "Top execution",  data: d1,   bars: { show: true }  },
            {   label: "Current execution",  data: d2,   bars: { show: true }  }
    <BLANKLINE>
            ],
            {
                xaxis: { ticks: [[1.5, "Large<br>rooster"], [5.5, "Rsync rooster"], [9.5, "Pool<br>Chicken"], [13.5, "Disease"]] }
            }
            );
        });
        </script>
**********************************************************************
1 items had failures:
   1 of   3 in __main__.Chart.

__init__
***Test Failed*** 1 failures.

【问题讨论】:

    标签: python multiline doctest


    【解决方案1】:

    将&lt;BLANKLINE&gt; 放入预期的输出中,就像它在错误消息中显示的那样。然后测试应该可以正常工作。预期的输入在第一行仅空白行终止,这就是您必须特别标记它的原因:

    >>> data_lists=[ {"Average execution" : [1, 2, 3, 2, 3]},
    ...                              {"Top execution"     : [3, 4, 5, 7, 8, 11, 6]},
    ...                              {"Current execution" : [1, 2, 1, 2, 1, 5]}       ]
    >>> c=Chart(data_lists,
    ...         ("Large<br>rooster", "Rsync rooster", "Pool<br>Chicken", "Disease"),
    ...         width=700, height=300)
    >>> print c.html.strip()
    <div id="placeholder3" style="width:700px;height:300px"></div>
    <BLANKLINE>
    <script id="source" language="javascript" type="text/javascript">
    $(function () {
    <BLANKLINE>
    var d0 = [[0, 1], [4, 2], [8, 3], [12, 2], [16, 3]];
    var d1 = [[1, 3], [5, 4], [9, 5], [13, 7], [17, 8], [21, 11], [25, 6]];
    var d2 = [[2, 1], [6, 2], [10, 1], [14, 2], [18, 1], [22, 5]];
    <BLANKLINE>
        $.plot($("#placeholder3"), [
    <BLANKLINE>
        {   label: "Average execution",  data: d0,   bars: { show: true }  },
        {   label: "Top execution",  data: d1,   bars: { show: true }  },
        {   label: "Current execution",  data: d2,   bars: { show: true }  }
    <BLANKLINE>
        ],
        {
            xaxis: { ticks: [[1.5, "Large<br>rooster"], [5.5, "Rsync<br>rooster"], [9.5, "Pool<br>Chicken"], [13.5, "Disease"]] }
        }
        );
    });
    </script>
    

    请参阅解释此问题的 doctest 文档:http://docs.python.org/library/doctest.html#how-are-docstring-examples-recognized

    【讨论】:

    • +1 ACC 有效,但仍然有很多缩进不匹配导致的错误。也许 doctest 不是这项工作的最佳工具。
    【解决方案2】:

    使用&lt;BLANKLINE&gt; 指示输出中的空白行,就像失败输出中显示的;-)

    【讨论】:

      【解决方案3】:

      除了在其他答案中建议使用 &lt;BLANKLINE&gt; 之外,此选项也很有用:

      >>> print c.html.strip()  # doctest: +NORMALIZE_WHITESPACE
      

      特别是如果预期的输出包含\r(回车)字符,在这种情况下&lt;BLANKLINE&gt; 将不匹配没有NORMALIZE_WHITESPACE 设置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-20
        相关资源
        最近更新 更多