If you should only have one assertion per test; how to test multiple inputs?

Posted by speg on Programmers See other posts from Programmers or by speg
Published on 2012-11-21T13:17:27Z Indexed on 2012/11/21 17:15 UTC
Read the original article Hit count: 220

I'm trying to build up some test cases, and have read that you should try and limit the number of assertions per test case.

So my question is, what is the best way to go about testing a function w/ multiple inputs. For example, I have a function that parses a string from the user and returns the number of minutes. The string can be in the form "5w6h2d1m", where w, h, d, m correspond to the number of weeks, hours, days, and minutes.

If I wanted to follow the '1 assertion per test rule' I'd have to make multiple tests for each variation of input? That seems silly so instead I just have something like:

self.assertEqual(parse_date('5m'), 5)
self.assertEqual(parse_date('5h'), 300)
self.assertEqual(parse_date('5d') ,7200)
self.assertEqual(parse_date('1d4h20m'), 1700)

In the one test case. Is there a better way?

© Programmers or respective owner

Related posts about testing

Related posts about unit-testing