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?)

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'exercise': 0.04; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'subject:test': 0.09; 'python': 0.11; 'def': 0.12; '"some': 0.16; '8bit%:32': 0.16; 'dict': 0.16; 'finney': 0.16; 'herself': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'subject:?)': 0.16; 'subject:generation': 0.16; 'subject:possible': 0.16; 'library': 0.18; 'module': 0.19; 'putting': 0.22; 'separate': 0.22; 'tests': 0.22; 'header:User-Agent:1': 0.23; 'rid': 0.24; 'looks': 0.24; "i've": 0.25; 'academic': 0.26; 'class.': 0.26; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'leave': 0.29; 'nature': 0.30; "i'm": 0.30; 'writes:': 0.31; 'class': 0.32; 'there.': 0.32; 'url:python': 0.33; 'cases': 0.33; 'third': 0.33; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'subject: (': 0.35; 'test': 0.35; 'there': 0.35; "i'll": 0.36; 'url:org': 0.36; 'unit': 0.37; 'so,': 0.37; 'ben': 0.38; 'url:library': 0.38; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'url:3': 0.61; 'skip:\xe2 10': 0.65; 'smith': 0.68; 'url:4': 0.69; '8bit%:43': 0.74; 'subject:get': 0.81; 'exercise.': 0.84; '\xe2\x80\xa6': 0.84; 'poorly': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
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 Thu, 16 Jan 2014 16:02:10 +1100
References <roy-406DCF.22465415012014@news.panix.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host rasputin.madmonks.org
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-gpg.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:lNAZhv/hu999GsCRIL2yJRv7kbM=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5569.1389848543.18130.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389848543 news.xs4all.nl 2955 [2001:888:2000:d::a6]:50551
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64052

Show key headers only | 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