Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

parametized unittest

24 views
Skip to first unread message

CraftyTech

unread,
Jan 11, 2014, 11:00:05 PM1/11/14
to
hello all,

I'm trying parametize my unittest so that I can re-use over and over, perhaps in a for loop. Consider the following:

'''
import unittest

class TestCalc(unittest.TestCase):
def testAdd(self):
self.assertEqual(7, 7, "Didn't add up")

if __name__=="__main__":
unittest.main()

'''


Simple and straight forward, but I'm trying to get to a place where I can run it this way:

import unittest

class TestCalc(unittest.TestCase):
def testAdd(self,var):
self.assertEqual(var, 7, "Didn't add up")

if __name__=="__main__":
unittest.main(testAdd(7))

is this possible? I'm finding it hard to use unittest in a for loop. Perhaps something like:

for val in range(25):
self.assertEqual(val,5,"not equal)

The loop will break after the first failure. Anyone have a good approach for this? please advise.

cheers,

Ben Finney

unread,
Jan 11, 2014, 11:22:47 PM1/11/14
to pytho...@python.org
CraftyTech <hmme...@gmail.com> writes:

> I'm trying parametize my unittest so that I can re-use over and over,
> perhaps in a for loop.

The ‘testscenarios’ <URL:https://pypi.python.org/pypi/testscenarios>
library allows you to define a set of data scenarios on your
FooBarTestCase and have all the test case functions in that class run
for all the scenarios, as distinct test cases.

e.g. a class with 5 scenarios defined, and 3 test case functions, will
run as 15 distinct test cases with separate output in the report.

> The loop will break after the first failure. Anyone have a good
> approach for this? please advise.

Yes, this is exactly the problem addressed by ‘testscenarios’. Enjoy it!

--
\ “Program testing can be a very effective way to show the |
`\ presence of bugs, but is hopelessly inadequate for showing |
_o__) their absence.” —Edsger W. Dijkstra |
Ben Finney

W. Trevor King

unread,
Jan 11, 2014, 11:28:38 PM1/11/14
to CraftyTech, pytho...@python.org
On Sat, Jan 11, 2014 at 08:00:05PM -0800, CraftyTech wrote:
> I'm finding it hard to use unittest in a for loop. Perhaps something like:
>
> for val in range(25):
> self.assertEqual(val,5,"not equal)
>
> The loop will break after the first failure. Anyone have a good
> approach for this? please advise.

If Python 3.4 is an option, you can stick to the standard library and
use subtests [1].

Cheers,
Trevor

[1]: http://docs.python.org/3.4/library/unittest.html#distinguishing-test-iterations-using-subtests

--
This email may be signed or encrypted with GnuPG (http://www.gnupg.org).
For more information, see http://en.wikipedia.org/wiki/Pretty_Good_Privacy
signature.asc

Roy Smith

unread,
Jan 11, 2014, 11:34:30 PM1/11/14
to
In article <mailman.5355.1389500...@python.org>,
"W. Trevor King" <wk...@tremily.us> wrote:

> On Sat, Jan 11, 2014 at 08:00:05PM -0800, CraftyTech wrote:
> > I'm finding it hard to use unittest in a for loop. Perhaps something like:
> >
> > for val in range(25):
> > self.assertEqual(val,5,"not equal)
> >
> > The loop will break after the first failure. Anyone have a good
> > approach for this? please advise.
>
> If Python 3.4 is an option, you can stick to the standard library and
> use subtests [1].

Or, as yet another alternative, if you use nose, you can write test
generators.

https://nose.readthedocs.org/en/latest/writing_tests.html#test-generators

CraftyTech

unread,
Jan 12, 2014, 11:57:16 AM1/12/14
to
Thank you all for the feedback. I now have what I need. Cheers
0 new messages