Is it possible to write a doctest unit test that will check that an exception is raised? For example, if I have a function foo(x) that is supposed to raise an exception if x<0, how would I write the doctest for that?
...
I'm trying to get started with unit testing in Python and I was wondering if someone could inform me of the advantages and disadvantages of doctest and unittest. What conditions would you use each for?
...
I think the concept of Python's doctests is brilliant, and as a C++ programmer at a real-time shop, I'm quite jealous. We basically have no unit test capability, which is a severe hindrance. I've seen C++Unit, etc, but is there anything that can extract test cases out of comments like Python's doctests rather than putting them in the c...
What is the best way to test code like this (the one below obviously fails while object is created in different block every time):
def get_session(db_name, verbose, test):
"""Returns current DB session from SQLAlchemy pool.
>>> get_session('Mmusc20090126', False, True)
<sqlalchemy.orm.session.Session object at 0xfb5ff0>
"""
if test:
...
I like doctest but when you have complex arguments that you need to
set before you pass to a function it become really hard to read..
Hence, you start using multiple lines assigning then calling the
function that you would like to test.. This approach however, will
report that you have multiple tests rather then the real number of
tests ...
I recently faced a problem about combining unit tests and doctests in Python. I worked around this problem in other way, but I still have question about it.
Python's doctest module parses docstrings in a module and run commands following ">>> " at the beginning of each line and compare the output of it and those in docstrings.
I wonder...
Hi!
I'd like to write a doctest like this:
"""
>>> print a.string()
foo : a
bar : b
date : <I don't care about the date output>
baz : c
"""
Is there any way to do this? I think it would make more sense to switch to unittest, but I'm curious whether it's possible to specify a range of output tha...
I'm trying to parse ouput from the Python doctest module and store it in an HTML file.
I've got output similar to this:
**********************************************************************
File "example.py", line 16, in __main__.factorial
Failed example:
[factorial(n) for n in range(6)]
Expected:
[0, 1, 2, 6, 24, 120]
Got:
...
When I run doctests on different Python versions (2.5 vs 2.6) and different plattforms (FreeBSD vs Mac OS) strings get quoted differently:
Failed example:
decode('{"created_by":"test","guid":123,"num":5.00}')
Expected:
{'guid': 123, 'num': Decimal("5.00"), 'created_by': 'test'}
Got:
{'guid': 123, 'num': Decimal('5.00'), 'cre...
Python's doctest module has a section for advanced API. Has anyone found a particular use for this API?
A quick look at the API structure (from the docs):
list of:
+------+ +---------+
|module| --DocTestFinder-> | DocTest | --DocTestRunner-> results
+------+ | ^ +---------+ ...
In a bit of Python I'm writing (a command line and filter testing tool: claft) I wanted a simple way to invoke the built-in test suite (doctest) and I decided on the following:
if 'DOCTEST' in os.environ and os.environ['DOCTEST']==sys.argv[0]:
_runDocTests()
sys.exit()
Thus if the DOCTEST variable is set for some other program...
I try to use doctest from example from http://docs.python.org/library/doctest.html
But when I run
python example.py -v
I get this
Traceback (most recent call last):
File "example.py", line 61, in <module>
doctest.testmod()
AttributeError: 'module' object has no attribute 'testmod'
But I can import doctest in python interacti...
If I run the following command:
>python manage.py test
Django looks at tests.py in my application, and runs any doctests or unit tests in that file. It also looks at the __ test __ dictionary for extra tests to run. So I can link doctests from other modules like so:
#tests.py
from myapp.module1 import _function1, _function2
__test__...
My function is
def validate_latitude(lat):
"""Enforce latitude is in range
>>> validate_latitude(65)
65
>>> validate_latitude(91)
90
>>> validate_latitude(-91)
-90
"""
lat = min(lat, 90)
lat = max(lat, -90)
return lat
And the test fails with this output
********************************...
In my Django app, I have a mix of doctests (defined in models.py and views.py) and unit tests (defined in tests.py. I can invoke an individual unit test by doing:
manage.py test app.TestCase
However, this doesn't seem to work for the doctests. Is there some way to run a single doctest (defined in models.py or views.py)?
...
I am working on some code that has to manipulate unicode strings. I am trying to write doctests for it, but am having trouble. The following is a minimal example that illustrates the problem:
# -*- coding: utf-8 -*-
def mylen(word):
"""
>>> mylen(u"áéíóú")
5
"""
return len(word)
print mylen(u"áéíóú")
First we run the code t...
I am playing with Google Calendar API, creating some useful function.
I another hand, I want to do it right puting some useful doctest and starting agile development.
How to write doctest since the result of each function is not really predictible (Depending of what is new on the server) :
>>> calendar = GoogleCalendar(user='blabla',...
I've got a Python module with docstrings in class methods, and a real-world example in the module docstring. The distinction is that the method-docstrings have been carefully crafted to be utterly repeatable tests, while the real-world example is just a copy'n'paste of the history from a Linux shell - which happened to invoke the python ...
I'm interested in learning how to Doctests and Unit tests in a more Agile / BDD way.
I've found a few tutorials that seem reasonable, but they are just thumbnails.
What I would really like to see is the source code of some Django projects that were developed BDD style.
The things I'm unclear about are how do you handle request objects e...
Please help me get an embedded ipython console to run inside a doctest. The example code demonstrates the problem and will hang your terminal. On bash shell I type ctrl-Z and then kill %1 to break out and kill, since ctrl-C won't work.
def some_function():
"""
>>> some_function()
'someoutput'
"""
# now try to drop ...