ignored test cases in unittest

Duncan Booth duncan.booth at invalid.invalid
Sun Aug 16 05:25:02 EDT 2009


Ben Finney <ben+python at benfinney.id.au> wrote:

> Terry <terry.yinzhe at gmail.com> writes:
> 
>> It seemed the to me that python unittest module does not support the
>> counting of ignored test cases directly. Is there any ready solution
>> for this?
> 
> One solution I've seen involves:
> 
> * a custom exception class, ‘TestSkipped’
> 
> * raising that exception at the top of test cases you want to
>   temporarily skip
> 
> * a custom ‘TestResult’ class that knows about a “skipped” result
> 
> * a custom reporter class that knows how you want to report that result
> 

I'd add to that a decorator so you can quickly mark a test case as ignored 
without editing the test itself. Also you could include a reason why it is 
ignored:

 @ignore("This test takes too long to run")
 def test_foo(self):
    ...

That also means you can redefine the decorator easily if you want to try 
running all the ignored tests.

Another decorator useful here is one that asserts that the test will fail. 
If the test passes then maybe someone fixed whatever was making it fail and 
if so you want to consider re-enabling it.

 @fails("Needs the frobnozz module to be updated")
 def test_whotsit(self):
    ...



More information about the Python-list mailing list