Proper structure for many test cases in Python with unittest
Posted
by mellort
on Stack Overflow
See other posts from Stack Overflow
or by mellort
Published on 2010-06-01T19:30:24Z
Indexed on
2010/06/01
19:33 UTC
Read the original article
Hit count: 250
I am looking into the unittest
package, and I'm not sure of the proper way to structure my test cases when writing a lot of them for the same method. Say I have a fact
function which calculates the factorial of a number; would this testing file be OK?
import unittest
class functions_tester(unittest.TestCase):
def test_fact_1(self):
self.assertEqual(1, fact(1))
def test_fact_2(self):
self.assertEqual(2, fact(2))
def test_fact_3(self):
self.assertEqual(6, fact(3))
def test_fact_4(self):
self.assertEqual(24, fact(4))
def test_fact_5(self):
self.assertFalse(1==fact(5))
def test_fact_6(self):
self.assertRaises(RuntimeError, fact, -1)
#fact(-1)
if __name__ == "__main__":
unittest.main()
It seems sloppy to have so many test methods for one method. I'd like to just have one testing method and put a ton of basic test cases (ie 4! ==24, 3!==6, 5!==120, and so on), but unittest doesn't let you do that.
What is the best way to structure a testing file in this scenario?
Thanks in advance for the help.
© Stack Overflow or respective owner