parametized unittest

C

CraftyTech

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,
 
W

W. Trevor King

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIcBAEBAgAGBQJS0hnyAAoJEKKfehoaNkbttOsP/iI5ld0aK0KHFi7mm1MjJr66
ZAq3wCaG7TVJ7gmsw3k+9qCaNPWX0mTFsxRyNyJbu4ThzcmWCM9ygmIM4lF+Z3ea
hxu+oZSJH4Cs6E4o3q57h+cSEHqiyJwcflGYWm2lYHIZgtGdbyutQG5SkPVoCWTV
/6eHBYXrDQExrKj5QTlZk3K2zcMjCbluWYTchwwWJ/feV0i28nqatAioCWtVMKi+
LaSbvJHeqUqyQrilBlY5XVwnqHHBfQsMbOiu6UHmTYUbIg6twIGG/re54ud3k8Vj
l2WWs3GiQCoS8T4Nd2+0pUnCsxutxRiKQzq9p37uHyvqQjU3GrO9U7NOJ6ONqkDG
lv2ka1CsRJwsCqvIgPvxfOHlcidqa2CAheHMpScqJtD3B0jz/wtSt8dZEG2Sb5q0
VjM97uGW3PLCOhSiT2HuII7wU00rP0DGpnVz2gj6vQ6QRYWamO/tjCCGK+LStSPR
6tawasLdn/425uAjhJGHmajfrT6stk44NFjhgHxwHcDCetrtK3o9k3tQxj84GGny
/n4O+z2V21QuNS8wXoKwSQIV3HzIdqqzH3Bbge/YE3vCFHSBd7eba4J68cL8njsN
F28N8H4QZeVM83CP7u+S/Gad7xoZ8HNpvrxQV6hM5Lv1uNfo+3mS0kqiKgjXvlKK
kphs9nLkj7dEbdVofDWk
=P5p4
-----END PGP SIGNATURE-----
 
R

Roy Smith

W. Trevor King said:
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
 
C

CraftyTech

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

Thank you all for the feedback. I now have what I need. Cheers

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top