Python doctest:多行结果

发布于 2024-10-16 08:48:49 字数 3178 浏览 2 评论 0原文

我无法让 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.

I can't get a doctest to work with a result which contains multiple lines and may contain empty lines at the beginning. This is probably caused by indentation and parsing issues. I've figured out some solutions:

  • Write the desired result to a file, and doctest the comparison between the result and the file contents.
  • Compare a hash of the result to a known hash. The main disadvantage of this approach is that the reader of the doctest learns very little about the desired result.
  • Find a way to make doctest work with multi line results.
  • Use unittest instead of doctest.

Any ideas?

Code:

    >>> 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>

Error:

**********************************************************************
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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆躺在深渊里 2024-10-23 08:48:49

放入预期输出中,就像错误消息中显示的那样。那么测试应该可以正常工作。预期的输入在第一个仅空白的行处终止,这就是为什么您必须专门标记它:

>>> 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

Put <BLANKLINE> in the expected output just like it shows in the error message. Then the test should work just fine. The expected input terminates at the first whitespace only line which is why you have to mark it specially:

>>> 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>

See the doctest documentation which explains this: http://docs.python.org/library/doctest.html#how-are-docstring-examples-recognized

哭泣的笑容 2024-10-23 08:48:49

使用 指示输出中的空行,就像失败输出中的 ;-)

Use <BLANKLINE> for indicating blank lines in output, just like the failure output says ;-)

在你怀里撒娇 2024-10-23 08:48:49

除了按照其他答案中的建议使用 之外,此选项也很有用:

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

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

Besides using <BLANKLINE> as suggested in the other answers, this option can be useful:

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

Especially if the expected output contains \r (carriage return) characters, in which case <BLANKLINE> won't match without NORMALIZE_WHITESPACE set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文