Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63746 > unrolled thread

parametized unittest

Started byCraftyTech <hmmedina@gmail.com>
First post2014-01-11 20:00 -0800
Last post2014-01-12 08:57 -0800
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  parametized unittest CraftyTech <hmmedina@gmail.com> - 2014-01-11 20:00 -0800
    Re: parametized unittest Ben Finney <ben+python@benfinney.id.au> - 2014-01-12 15:22 +1100
    Re: parametized unittest "W. Trevor King" <wking@tremily.us> - 2014-01-11 20:28 -0800
      Re: parametized unittest Roy Smith <roy@panix.com> - 2014-01-11 23:34 -0500
        Re: parametized unittest CraftyTech <hmmedina@gmail.com> - 2014-01-12 08:57 -0800

#63746 — parametized unittest

FromCraftyTech <hmmedina@gmail.com>
Date2014-01-11 20:00 -0800
Subjectparametized unittest
Message-ID<c11a3b62-2053-4b6f-afda-d2df305f58e5@googlegroups.com>
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,

[toc] | [next] | [standalone]


#63747

FromBen Finney <ben+python@benfinney.id.au>
Date2014-01-12 15:22 +1100
Message-ID<mailman.5354.1389500578.18130.python-list@python.org>
In reply to#63746
CraftyTech <hmmedina@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

[toc] | [prev] | [next] | [standalone]


#63748

From"W. Trevor King" <wking@tremily.us>
Date2014-01-11 20:28 -0800
Message-ID<mailman.5355.1389500996.18130.python-list@python.org>
In reply to#63746

[Multipart message — attachments visible in raw view] — view raw

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

[toc] | [prev] | [next] | [standalone]


#63749

FromRoy Smith <roy@panix.com>
Date2014-01-11 23:34 -0500
Message-ID<roy-49C7C9.23343011012014@news.panix.com>
In reply to#63748
In article <mailman.5355.1389500996.18130.python-list@python.org>,
 "W. Trevor King" <wking@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

[toc] | [prev] | [next] | [standalone]


#63786

FromCraftyTech <hmmedina@gmail.com>
Date2014-01-12 08:57 -0800
Message-ID<11a152c3-edfb-4552-b296-cfb5e03c0f73@googlegroups.com>
In reply to#63749
On Saturday, January 11, 2014 11:34:30 PM UTC-5, Roy Smith wrote:
> In article <mailman.5355.1389500996.18130.python-list@python.org>,
> 
>  "W. Trevor King" <wking@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

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 <mailman.5355.1389500996.18130.python-list@python.org>,
> 
>  "W. Trevor King" <wking@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web