[TIP] doctests with either-or outcome

Pete pfein at pobox.com
Fri Sep 26 07:00:35 PDT 2008

On Sep 26, 2008, at 2:51 AM, Rob Kirkpatrick wrote:
> Is there a way of having a doctest pass if it has EITHER output A or  
> output B?  In, this case, we need to construct some pickled  
> resources, then use them for the test.  The first time the resource  
> is generated, it prints out a "generated resource x..." message.  If  
> the resource already exists, it generates no output.  Both outputs  
> are ok.  Running the tests twice ensures that the resources will all  
> be there and no generation messages are printed, which is what the  
> doctests currently expect, but this seems hacky...

Hmm, something sounds fishy here.  Your tests should be repeatable -  
they shouldn't depend on whether they've been run before. Are you  
trying to test the resource generation itself?  If yes, then you want  
to be running the generation in your tests twice - once to make sure  
they work and the the second time to make sure they don't repeat  
themselves. If not, this sounds more like a fixture and shouldn't be  
part of your tests themselves.  Remember, doctests run in the context  
of their module, so you could do the resource generation there (either  
in __main__ or by putting the doctests in a separate file entirely).


More information about the testing-in-python mailing list