Switching from nose to unittest2 - how to continue after an error?

Terry Reedy tjreedy at udel.edu
Mon Aug 25 17:47:40 EDT 2014


On 8/25/2014 2:13 PM, Skip Montanaro wrote:
> It appears that unittest in Python 2.7 should be capable enough that I
> can abandon nose in favor of python -m unittest. How do I get it to
> continue past the first failure?

Unittest normally stops with the first failure in a test_function. If 
the asserts within a function are dependent (or normally passing), that 
may be what you want.

I know of two ways to collect multiple failures within a test function:

1. Do it yourself. For instance, let iopairs be an iterable of input, 
expected-output pairs for a function f.

   def test_f(self):
     failures = []
     for inp, expect in iopairs:
       actual = f(inp)
       if actual != expect:
         failures.append((inp, expect, actual))
     assertFalse(failures, 'inp, expect, actual triples'

2. Use subtests, new in 3.4, and I suspect backported in unittest2 on 
PyPI. This can be combined with the 1. above.  Suppose two functions 
should both pass the test above.

   def test_g_h(self):
     for f in (g, h):
       with self.subTest(f=f):
         <body of test_f>

As for asserts (from next message): self.assertTrue is equivalent to 
assert, except it does not disappear with a compile flag. The other 
assertX methods resolve to assertTrue, but for some, such as assert 
raises, the equivalent in non-trivial. Even when the equivalent is 
trivial, the specific methods usually gives better diagnostic messages. 
This is especially true when comparing two non-empty collection for 
equality. Knowing what is a one but the the other is very helpful.

-- 
Terry Jan Reedy





More information about the Python-list mailing list