Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unittest testing assert*() calls rather than methods?

Reply
Thread Tools

Unittest testing assert*() calls rather than methods?

 
 
Tim Chase
Guest
Posts: n/a
 
      09-28-2011
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


 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-29-2011
Tim Chase wrote:

> 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?


I used to ask the same question, but then I decided that if I wanted each
data point to get its own tick, I should bite the bullet and write an
individual test for each.

If you really care, you could subclass unittest.TestCase, and then cause
each assert* method to count how often it gets called. But really, how much
detailed info about *passed* tests do you need?

If you are writing loops inside tests, you might find this anecdote useful:

http://mail.python.org/pipermail/pyt...l/1270640.html



--
Steven

 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      09-29-2011
In article <4e83b8e0$0$29972$c3e8da3$ om>,
Steven D'Aprano <steve+> wrote:

> If you are writing loops inside tests, you might find this anecdote useful:
>
> http://mail.python.org/pipermail/pyt...l/1270640.html


On the other hand, the best test is one that gets written. I will often
write tests that I know do not meet the usual standards of purity and
wholesomeness. Here's a real-life example:

for artist in artists:
name = artist['name']
self.assertIsInstance(name, unicode)
name = name.lower()
# Due to fuzzy matching, it's not strictly guaranteed that the
# following assertion is true, but it works in this case.
self.assertTrue(name.startswith(term), (name, term))

Could I have written the test without the loop? Probably. Would it
have been a better test? I guess, at some level, probably. And, of
course, the idea of a "not strictly guaranteed" assertion is probably
enough to make me lose my Unit Tester's Guild Secret Decoder Ring
forever

But, the test was quick and easy to write, and provides value. I could
have spent twice as long writing a better test, and it would have
provided a little more value, but certainly not double. More
importantly, had I spent the extra time writing the better test, I might
have not had enough time to write all the other tests I wrote that day.

Sometimes good enough is good enough.
 
Reply With Quote
 
Devin Jeanpierre
Guest
Posts: n/a
 
      09-29-2011
> I used to ask the same question, but then I decided that if I wanted each
> data point to get its own tick, I should bite the bullet and write an
> individual test for each.


Nearly the entire re module test suite is a list of tuples. If it was
instead a bunch of TestCase classes, there'd be a lot more boilerplate
to write. (At a bare minimum, there'd be two times as many lines, and
all the extra lines would be identical...)

Why is writing boilerplate for a new test a good thing? It discourages
the authorship of tests. Make it as easy as possible by e.g. adding a
new thing to whatever you're iterating over. This is, for example, why
the nose test library has a decorator for generating a test suite from
a generator.

Devin

On Wed, Sep 28, 2011 at 8:16 PM, Steven D'Aprano
<steve+> wrote:
> Tim Chase wrote:
>
>> 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?

>
> I used to ask the same question, but then I decided that if I wanted each
> data point to get its own tick, I should bite the bullet and write an
> individual test for each.
>
> If you really care, you could subclass unittest.TestCase, and then cause
> each assert* method to count how often it gets called. But really, how much
> detailed info about *passed* tests do you need?
>
> If you are writing loops inside tests, you might find this anecdote useful:
>
> http://mail.python.org/pipermail/pyt...l/1270640.html
>
>
>
> --
> Steven
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      09-29-2011
In article <>,
Ben Finney <ben+> wrote:

> 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.


Which, depending on what you're doing, may or may not be important. In
many cases, there's only two states of interest:

1) All tests pass

2) Anything else
 
Reply With Quote
 
Eric Snow
Guest
Posts: n/a
 
      09-29-2011
On Wed, Sep 28, 2011 at 6:50 PM, Devin Jeanpierre
<> wrote:
>> I used to ask the same question, but then I decided that if I wanted each
>> data point to get its own tick, I should bite the bullet and write an
>> individual test for each.

>
> Nearly the entire re module test suite is a list of tuples. If it was
> instead a bunch of TestCase classes, there'd be a lot more boilerplate
> to write. (At a bare minimum, there'd be two times as many lines, and
> all the extra lines would be identical...)
>
> Why is writing boilerplate for a new test a good thing? It discourages
> the authorship of tests. Make it as easy as possible by e.g. adding a
> new thing to whatever you're iterating over. This is, for example, why
> the nose test library has a decorator for generating a test suite from
> a generator.


+1

>
> Devin
>
> On Wed, Sep 28, 2011 at 8:16 PM, Steven D'Aprano
> <steve+> wrote:
>> Tim Chase wrote:
>>
>>> 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?

>>
>> I used to ask the same question, but then I decided that if I wanted each
>> data point to get its own tick, I should bite the bullet and write an
>> individual test for each.
>>
>> If you really care, you could subclass unittest.TestCase, and then cause
>> each assert* method to count how often it gets called. But really, how much
>> detailed info about *passed* tests do you need?
>>
>> If you are writing loops inside tests, you might find this anecdote useful:
>>
>> http://mail.python.org/pipermail/pyt...l/1270640.html
>>
>>
>>
>> --
>> Steven
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>

> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      09-29-2011
In article <>,
Ben Finney <ben+> wrote:

> Roy Smith <> writes:
>
> > In article <>,
> > Ben Finney <ben+> wrote:
> >
> > > 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.

> >
> > Which, depending on what you're doing, may or may not be important. In
> > many cases, there's only two states of interest:
> >
> > 1) All tests pass
> >
> > 2) Anything else

>
> For the purpose of debugging, it's always useful to more specifically
> narrow down the factors leading to failure.


Well, sure, but "need to debug" is just a consequence of being in state
2. If a test fails and I can't figure out why, I can always go back and
add additional code to the test case to extract additional information.
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      09-29-2011
On 09/28/11 19:52, Roy Smith wrote:
> In many cases, there's only two states of interest:
>
> 1) All tests pass
>
> 2) Anything else


Whether for better or worse, at some places (such as a previous
employer) the number (and accretion) of test-points is a
marketing bullet-point for upgrades & new releases.

-tkc



 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      09-29-2011
In article <mailman.1552.1317266045.27778.python->,
Tim Chase <> wrote:

> On 09/28/11 19:52, Roy Smith wrote:
> > In many cases, there's only two states of interest:
> >
> > 1) All tests pass
> >
> > 2) Anything else

>
> Whether for better or worse, at some places (such as a previous
> employer) the number (and accretion) of test-points is a
> marketing bullet-point for upgrades & new releases.


Never attribute to malice that which is adequately explained by the
stupidity of the marketing department.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: unittest for system testing Mark Lawrence Python 1 10-18-2012 06:55 AM
Unittest - testing for filenames and filesize Tigerstyle Python 15 09-01-2012 05:08 AM
Unittest - adding a doctest suite to unittest.main Paul Moore Python 1 10-14-2008 03:32 PM
Are system calls sometimes costlier than library calls? Richard Tobin C Programming 24 11-11-2007 08:52 AM
Unittest - testing properties (read-only attributes) Paul Moore Python 9 02-21-2005 05:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57