[TIP] parametrized testing and unittest2.TestCase

holger krekel holger at merlinux.eu
Sun Dec 18 01:15:18 PST 2011


On Sat, Dec 17, 2011 at 16:25 +1300, Robert Collins wrote:
> On Sat, Dec 17, 2011 at 11:18 AM, holger krekel <holger at merlinux.eu> wrote:
> > Hi Hans,
> >
> > On Fri, Dec 16, 2011 at 13:00 -0800, Hans Sebastian wrote:
> >> I need help and some suggestions. I run tests using py.test (latest from
> >> pip) and use unittest2.TestCase as the base class for the tests. Today, I
> >> am trying to parametrized multiple calls to a test function by following
> >> the example in the online doc. I found that it only works with test class
> >> that extends from *object* but not with the one that extends from *
> >> unittest2.TestCase*. First of all, is this a bug or just not supported by
> >> py.test?
> >
> > Indeed, py.test parametrization does not work with unittest2.TestCase.
> > The unittest/unittest2 support of py.test aims at compatibility
> > and invokes some not-so-easy-to-customize unittest code.  I think it'd
> > be pretty hard to customize unittest2.TestCase to support
> > parametrization and dynamic setups of test function arguments.
> >
> >> If not possible, what are alternative ways of parametrizing
> >> testing but still want to reuse unittest2.TestCase's assert methods?
> >
> > Not sure but weren't there some libraries which provided these kind
> > of assert methods in a standalone manner?  It may be possible to
> > add a new way to run unittest.TestCases such that py.test's parametrized
> > testing and funcargs work - at the cost of more limited compatibility.
> 
> testscenarios may either inspire or even help you with this.

Since some time there is a quick port example of testscenarios to py.test:

    http://pytest.org/latest/example/parametrize.html#a-quick-port-of-testscenarios

However, the main issue i see with parametrizing tests and dependency injection
with unittest.TestCase is controling the life-cycle of a resource.
How can you implement resources (with testscenarios, say) which are
instantiated once per class or per test-run?

best,
holger



More information about the testing-in-python mailing list