how to make all assertions in a unit test execute

Ben Finney ben+python at benfinney.id.au
Sat Oct 16 19:51:59 EDT 2010


Terry Reedy <tjreedy at udel.edu> writes:

> The temptation to write unfactored duplicate code like this is a
> negative of unittest.

I don't see that it's especially attributable to the ‘unittest’ module.
It's a common problem to be solved, and solutions are available.

> The question is whether get_filenames gets the proper number of names.
> One way to do it with unittest is something like
>
> def test_get_filenames_return_number(self):
>   inputs = ('myself', 'tom', 'jerry')
>   outputs= (      0,     1,       3)
>   self.assertEquals(outputs,
>    [len(get_filenames(self.dirname,f) for f in inputs])
>
> Listing inputs and outputs first makes it easy to check pairs and
> revise if needed.

These are sometimes called “test scenarios”: tuples, or other complex
data structures, that define parameters of a general test to be run over
all the scenarios.

> With hundreds or thousands of i/o pairs, something different would be
> needed.

For this purpose, the ‘testscenarios’ package is useful
<URL:http://pypi.python.org/pypi/testscenarios/>.

-- 
 \      “It is difficult to get a man to understand something when his |
  `\   salary depends upon his not understanding it.” —Upton Sinclair, |
_o__)                                                             1935 |
Ben Finney



More information about the Python-list mailing list