Python forum for Python programmers

Unittest testing assert*() calls rather than methods?

Sep 28, 2011 2:16 pm
Tim Chase

While I asked this on the Django list as it happened to be with
some Django testing code, this might be a more generic Python
question so I'll ask here too.

When performing unittest tests, I have a number of methods of the
form

def test_foo(self):
data = (
(item1, result1),
... #bunch of tests for fence-post errors
)
for test, result in data:
self.assertEqual(process(test), result)

When I run my tests, I only get a tick for running one the one
test (test_foo), not the len(data) tests that were actually
performed. Is there a way for unittesting to report the number
of passed-assertions rather than the number of test-methods run?

-tkc

Sep 28, 2011 2:29 pm
Ben Finney
Re: Unittest testing assert*() calls rather than methods?

Tim Chase <python.list@tim.thechases.com> writes:

> def test_foo(self):
> data = (
> (item1, result1),
> ... #bunch of tests for fence-post errors
> )
> for test, result in data:
> self.assertEqual(process(test), result)

The sets of data for running the same test we might call “scenarios”.

> When I run my tests, I only get a tick for running one the one test
> (test_foo), not the len(data) tests that were actually performed.

Worse, if one of the scenarios causes the test to fail, the loop will
end and you won't get the results for the remaining scenarios.

> Is there a way for unittesting to report the number of
> passed-assertions rather than the number of test-methods run?

You can use the third-party ‘testscenarios’ library
<URL:http://pypi.python.org/pypi/test-scenarios> to generate test cases
at run time, one for each combination of scenarios with test cases on
the class. They will all be run and reported as distinct test cases.

There is even another library integrating this with Django
<URL:http://pypi.python.org/pypi/django-testscenarios>.

\ “Books and opinions, no matter from whom they came, if they are |
`\ in opposition to human rights, are nothing but dead letters.” |
_o__) —Ernestine Rose |
Ben Finney
Sep 28, 2011 2:53 pm
Ben Finney
Re: Unittest testing assert*() calls rather than methods?

Ben Finney <ben+python@benfinney.id.au> writes:

> You can use the third-party ‘testscenarios’ library

The URL got a bit mangled. The proper PyPI URL for that library is
<URL:http://pypi.python.org/pypi/testscenarios>.

> to generate test cases at run time, one for each combination of
> scenarios with test cases on the class. They will all be run and
> reported as distinct test cases.
>
> There is even another library integrating this with Django
> <URL:http://pypi.python.org/pypi/django-testscenarios>.

\ “Don't worry about people stealing your ideas. If your ideas |
`\ are any good, you'll have to ram them down people's throats.” |
_o__) —Howard Aiken |
Ben Finney


Previous Thread: Question about speed of sequential string replacement vs regex or
Next Thread: Is there any way to access attributes from an imported module?

Related Forum Topics
Unittest - testing for filenames and filesize
Hi.

I need help with an assignment and I hope you guys can guide me in the right direction.

This is the code:

------------------
"""
Demostration of setUp and tearDown.
The tests do not actually test anything - this is a demo.
"""
import unittest
import tempfile
import...
How to simulate tar filename substitution across pipedsubprocess.Popen() calls?
Hi All
i am trying to build up a set of subprocess.Ponen calls to
replicate the effect of a horribly long shell command. I'm not clear
how I can do one part of this and wonder if anyone can advise. I'm on
Linux, fairly obviously.

I have a command which (simplified) is a tar -c command...
Assert expressions
If I use the code

assert False, "unhandled option"

I get output like:

option -q not recognized
for help use --help

What other expressions can I use other than "unhandled option"? Is there a list somewhere?

Thanks



Duck typing assert

People who come from strongly typed languages that offer interfaces often are confused by lack of one in Python. Python, being dynamic typing programming language, follows duck typing principal. It can as simple as this:

assert looks(Foo).like(IFoo)

The post below shows how programmer can...
Unittest and threading
Is it safe to use unittest with threads?

In particular, if a unit test fails in some thread other than the one
that launched the test, will that information be captured properly?

A search of the net shows a suggestion that all failures must be
reported in the main thread, but I couldn't...
Subclassing from unittest
Hi,

I'd like to subclass from unittest.TestCase. I observed something
interesting and wonder if anyone can explain what's going on... some
subclasses create null tests.

I can create this subclass and the test works:

class StdTestCase (unittest.TestCase):
blahblah

and I...
Unittest. customizing tstloaders / discover()
Hi,

I'd like to have a custom test loder, which will filter out certain
tests or which just searches tests in certain directories.

I'd like to use regular expresions as include / exclude rules
and I would like to have another filter function, which would check for
the existence of...
Publish unittest results from test discovery
So I've started using unittest, and I love it. I use testdiscovery (python -m unittest discover) so that I can distribute my tests and don't have to manage them all manually. I wanted to start publishing my test results to xml, though. I found xmlrunner by googling around, but it requires me to...
Testing tools classification
There is this nice page of testing tools taxonomy:
http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy

But it does not list staf: http://staf.sourceforge.net/index.php.


Re: simple web/html testing
For static html testing, I'd avoid using Selenium. Even though Selenium is *the* tool for RIA and javascript intensive environments, feels like bringing up a browser with all the coordination and resources that it takes just to crawl the website and find 404s is an overkill.

What we...