Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77005

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

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Switching from nose to unittest2 - how to continue after an error?
Date 2014-08-25 17:47 -0400
References <CANc-5UwXqeSbzgBQbWXEvJu-3j1bY+RU76Hz0w36_QaGt+RBaA@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.13433.1409003278.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Switching from nose to unittest2 - how to continue after an error? Terry Reedy <tjreedy@udel.edu> - 2014-08-25 17:47 -0400

csiph-web