parametized unittest

2014-01-11 Thread 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,
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: parametized unittest

2014-01-12 Thread CraftyTech
On Saturday, January 11, 2014 11:34:30 PM UTC-5, Roy Smith wrote:
> In article ,
> 
>  "W. Trevor King"  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

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

On Saturday, January 11, 2014 11:34:30 PM UTC-5, Roy Smith wrote:
> In article ,
> 
>  "W. Trevor King"  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

-- 
https://mail.python.org/mailman/listinfo/python-list