doctest

Python: Why does this doc test fail?

This code that's in the doctest works when run by itself, but in this doctest it fails in 10 places. I can't figure out why it does though. The following is the entire module: class requireparams(object): """ >>> @requireparams(['name', 'pass', 'code']) >>> def complex_function(params): >>> print(params['name']) ...

Can I unit test an inner function in python?

Is there any way to write unittests or doctests for innerfunc? def outerfunc(): def innerfunc(): do_something() return innerfunc() ...

Mock Y of (from X import Y) in doctest (python)

I'm trying to create a doctest with mock of function that resides in a separate module and that is imported as bellow from foomodule import foo def bar(): """ >>> from minimock import mock >>> mock('foo', nsdicts=(bar.func_globals,), returns=5) >>> bar() Called foo() 10 """ return foo() * 2 import doct...

Why does nose finds tests in files with only 644 permission?

Today I ran a bunch of doctests using Python 2.6 on a Ubuntu 9.10 with nose : nosetests --with-doctest Ran 0 tests in 0.001s OK WTF? I had tests in that files, why didn't that work? I changed permission to 644: sudo chmod 644 * -R nosetests --with-doctest Ran 11 test in 0.004s FAILED (errors=1) Changing it back to 777: sudo chm...

Django doctests in views.py

The Django documentation on tests states: For a given Django application, the test runner looks for doctests in two places: The models.py file. You can define module-level doctests and/or a doctest for individual models. It's common practice to put application-level doctests in the module docstring and model-level doctests in...

doctest locally defined functions

Hi, is there any way to doctest locally defined functions? As an example I would want def foo(): """ >>> foo() testfoo""" def foo2(): """ >>> 1/0 """ print 'testfoo' foo2() to NOT pass the test. But still I would not want to make foo2 global for the entire module... ...

Running doctests from Pydev?

Is there any straightforward way or should I use an external tool like Nose? ...

Testing warnings with doctest

I'd like to use doctests to test the presence of certain warnings. For example, suppose I have the following module: from warnings import warn class Foo(object): """ Instantiating Foo always gives a warning: >>> foo = Foo() testdocs.py:14: UserWarning: Boo! warn("Boo!", UserWarning) >>> """ def __i...

How to test floats resoults with doctest?

Hi I'm developing a program that makes some floating points calculus. is there any way to test the my functions (which deliver floats) with doctest? ...

Mocking ImportError in Python

I'm trying this for almost two hours now, without any luck. I have a module that looks like this: try: from zope.component import queryUtility # and things like this except ImportError: # do some fallback operations <-- how to test this? Later in the code: try: queryUtility(foo) except NameError: # do some fallback ...

Passing string with (accidental) escape character loses character even though it's a raw string

I have a function with a python doctest that fails because one of the test input strings has a backslash that's treated like an escape character even though I've encoded the string as a raw string. My doctest looks like this: >>> infile = [ "Todo: fix me", "/** todo: fix", "* me", "*/", r"""//\todo stuff to fix""", "TODO f...

Handling import errors when using doctest.

Hi there, every now and then when I code in Python, I have to do without certain third-party modules. Eg. when I'm writing user authentication, it can be done in several ways and one of them is by using LDAP. However if the user does not want to use LDAP auth., he can choose a different option in a config file and in that case he shou...

Does Python doctest remove the need for unit-tests?

Hi all, A fellow developer on a project I am on believes that doctests are as good as unit-tests, and that if a piece of code is doctested, it does not need to be unit-tested. I do not believe this to be the case. Can anyone provide some solid, ideally cited, examples either for or against the argument that doctests replace the need for...

Python: using doctests for classes

Hi, Is it possible to use Python's doctest concept for classes, not just functions? If so, where shall I put the doctests - at the class' docstring, or at the constructor's docstring? To clarify, I'm looking for something like: class Test: """ >>> a=Test(5) >>> a.multiply_by_2() 10 """ def __init__(self, numbe...

use doctest and logging in python program

#!/usr/bin/python2.4 import logging import sys import doctest def foo(x): """ >>> foo (0) 0 """ print ("%d" %(x)) _logger.debug("%d" %(x)) def _test(): doctest.testmod() _logger = logging.getLogger() _logger.setLevel(logging.DEBUG) _formatter = logging.Formatter('%(message)s') _handler = l...

Doctesting functions that receive and display user input - Python (tearing my hair out)

Howdy! I am currently writing a small application with Python (3.1), and like a good little boy, I am doctesting as I go. However, I've come across a method that I can't seem to doctest. It contains an input(), an because of that, I'm not entirely sure what to place in the "expecting" portion of the doctest. Example code to illustrate ...

pdb is not working in django doctests

So I created the following file (testlib.py) to automatically load all doctests (throughout my nested project directories) into the __tests__ dictionary of tests.py: # ./testlib.py import os, imp, re, inspect from django.contrib.admin import site def get_module_list(start): all_files = os.walk(start) file_list = [(i[0], (i[1], ...

Python doctests / sphinx : style guide, how to use those and have a readable code ?

Hi ! I love doctests, it is the only testing framwork I use, because it is so quick to write, and because used with sphinx it makes such great documentations with almost no effort... However, very often, I end-up doing things like this : """ Descriptions ============= bla bla bla ... >>> test 1 bla bla bla + tests tests test...

Python doctest error

Hi I recently started experimenting with python currently reading "Think like a computer scientist: Learning python v2nd edition" I have been having some trouble with doctest. I use a windows 7 machine and Eclipse IDE with pydev. My question is when i run the script below i get the error below. Said script is below the the error messag...

Is there a Matlab tool similar to Python's Doctest?

Hi, In my Python development, doctest has really helped both to make writing unit tests less annoying, and integrate usage examples with documentation. I was wondering, is there anything like this available in the Matlab world? It doesn't have to literally use code comments as a test, but if it had those two desirable qualities, t...