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


Groups > comp.lang.python > #64052

Dynamic generation of test cases for each input datum (was: Is it possible to get string from function?)

From Ben Finney <ben+python@benfinney.id.au>
Subject Dynamic generation of test cases for each input datum (was: Is it possible to get string from function?)
Date 2014-01-16 16:02 +1100
References <roy-406DCF.22465415012014@news.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.5569.1389848543.18130.python-list@python.org> (permalink)

Show all headers | View raw


Roy Smith <roy@panix.com> writes:

> I've got some unit tests that look like:
>
> class Foo(TestCase):
>   def test_t1(self):
>     RECEIPT = "some string"
>
>   def test_t2(self):
>     RECEIPT = "some other string"
>
>   def test_t3(self):
>     RECEIPT = "yet a third string"
>
> and so on.

That looks like a poorly defined class.

Are the test cases pretty much identical other than the data in those
strings? If so, use a collection of strings and generate separate tests
for each one dynamically.

In Python 2 and 3, you can use the ‘testscenarios’ library for that
purpose <URL:https://pypi.python.org/pypi/testscenarios>.

In Python 3, the ‘unittest’ module has “subtests” for the same purpose
<URL:http://docs.python.org/3.4/library/unittest.html#distinguishing-test-iterations-using-subtests>.

> and I could have also solved the problem by putting all the strings in
> a dict and having the functions pull them out of there. But, I'm still
> interested in exploring if there is any way to do this with
> introspection, as an academic exercise.

Since I don't think your use case is best solved this way, I'll leave
the academic exercise to someone else.

-- 
 \            “… Nature … is seen to do all things Herself and through |
  `\         herself of own accord, rid of all gods.” —Titus Lucretius |
_o__)                                                 Carus, c. 40 BCE |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is it possible to get string from function? Roy Smith <roy@panix.com> - 2014-01-15 22:46 -0500
  Dynamic generation of test cases for each input datum (was: Is it possible to get string from function?) Ben Finney <ben+python@benfinney.id.au> - 2014-01-16 16:02 +1100
    Re: Dynamic generation of test cases for each input datum (was: Is it possible to get string from function?) Roy Smith <roy@panix.com> - 2014-01-16 00:09 -0500
  Re: Is it possible to get string from function? Chris Angelico <rosuav@gmail.com> - 2014-01-16 16:25 +1100
    Re: Is it possible to get string from function? Roy Smith <roy@panix.com> - 2014-01-16 00:40 -0500
      Re: Is it possible to get string from function? Chris Angelico <rosuav@gmail.com> - 2014-01-16 16:47 +1100
  Re: Is it possible to get string from function? Steven D'Aprano <steve@pearwood.info> - 2014-01-16 07:16 +0000
    Re: Is it possible to get string from function? Roy Smith <roy@panix.com> - 2014-01-16 09:30 -0500
  Re: Is it possible to get string from function? Peter Otten <__peter__@web.de> - 2014-01-16 09:52 +0100

csiph-web