[TIP] Building a unittest.TestCase from a doctest

Jim Fulton jim at zope.com
Tue Dec 18 11:09:24 PST 2007


On Dec 18, 2007, at 1:48 PM, Titus Brown wrote:

> -> >In my situation, I would like to load doctests from
> -> >/over/there/module.py into a file 'test.py' that is loaded with  
> other
> -> >unittest.TestCases, and have the doctest run together with the  
> other
> -> >test cases.
> ->
> -> A module may contain multiple tests.  I don't understand why you  
> would
> -> want a single test case?  This would be like taking any test  
> suite and
> -> saying you want to treat it as a single test case. I must be
> -> misunderstanding you.
>
> Hi, Jim,
>
> no, you understand me right.
>
> My real goal is to get the tests to *run* under the aegis of a  
> different
> module;

I don't know what you mean by that.

...

> I spent 15 minutes last night trying to understand unittest well  
> enough
> to import the various test cases in a doctest suite into a unittest,  
> but
> no joy ;(.  I just don't understand the unittest magic very well, I'm
> afraid.

The APIs are a little funky. (I assume this is some Java influence. :)

>  While I understand the distinction between a TestCase and a
> TestSuite, I can't understand why my unittest.TestCases run multiple
> tests!

I think is complicated by the excessive and odd use of inheritance in  
unittest and the odd class naming.  In unittest, when you subclass  
TestCase, you are actually usually defining a test suite.  You have  
methods whose names begin with "test" and makeSuite takes the class  
and produces a suite with a test for each of these methods. At lease  
that's how I've seen it used.

> (There's a reason I use nose: I've never understood unittest.)


I thought nose was a test runner for unittest. I guess not.

I don't care for writing tests in the unittest style.  I prefer  
doctest and just use the unittest framework and the zope.testing test  
runner to manage running them.  It was my understanding that nose can  
also run unittest suites.

Perhaps there is or should be a better integration of doctest and nose  
that bypasses the unittest framework.  I originally integrated doctest  
and unittest because unittest was part of the standard library.

I really need to learn about nose some day. :/

Jim

--
Jim Fulton
Zope Corporation





More information about the testing-in-python mailing list