Dynamic generation of test cases for each input datum (was: Is it possible to get string from function?)
Ben Finney
ben+python at benfinney.id.au
Thu Jan 16 00:02:10 EST 2014
Roy Smith <roy at 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
More information about the Python-list
mailing list