简体   繁体   English

Python doctest:多行结果

[英]Python doctest: result with multiple lines

I can't get a doctest to work with a result which contains multiple lines and may contain empty lines at the beginning. 我无法使用doctest来处理包含多行的结果,并且可能在开头包含空行。 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. 写所期望的结果到一个文件,并且doctest结果和文件内容之间的比较。
  • 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. 这种方法的主要缺点是doctest的读者很少了解所需的结果。
  • Find a way to make doctest work with multi line results. 找到一种方法,使doctest可以使用多行结果。
  • Use unittest instead of doctest . 使用unittest而不是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.

Put <BLANKLINE> in the expected output just like it shows in the error message. <BLANKLINE>放在预期的输出中,就像它在错误消息中显示的那样。 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 请参阅doctest文档,该文档解释了这一点: http//docs.python.org/library/doctest.html#how-are-docstring-examples-recognized

使用<BLANKLINE>指示输出中的空白行,就像失败输出所示;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM