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: 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 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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 Roy Smith 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 . In Python 3, the ‘unittest’ module has “subtests” for the same purpose . > 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