Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.
like image 677
Adam Matan Avatar asked Feb 08 '11 09:02

Adam Matan


People also ask

Can we handle unpredictable output using doctest in Python?

When the tests include values that are likely to change in unpredictable ways, and where the actual value is not important to the test results, you can use the ELLIPSIS option to tell doctest to ignore portions of the verification value.

What is the correct way to run all the doctest in Python?

Right click on a blank space in the python code, and there is a menu option to run all the Doctests found in the file, not just the tests for one function.

How does doctest work Python?

The doctest module programmatically searches Python code for pieces of text within comments that look like interactive Python sessions. Then, the module executes those sessions to confirm that the code referenced by a doctest runs as expected.


1 Answers

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

like image 166
Duncan Avatar answered Nov 08 '22 16:11

Duncan