[TIP] Parametrize tests for unit or integration testing

Andres Riancho andres.riancho at gmail.com
Mon Feb 3 04:01:57 PST 2014

Doesn't sound really hard. This code won't work but will give you the
basic idea:

class BaseTest(object):
    ClassUnderTest = None
    def test_foo(self):
        inst = self.ClassUnderTest()
        x = foo(inst)
        self.assertEqual(x, -1)

class TestWithMock(TestCase, BaseTest):
    ClassUnderTest = Mock()

class TestWithReal(TestCase, BaseTest):
    ClassUnderTest = RealStuff()

On Sat, Feb 1, 2014 at 6:16 PM, Mateusz Łoskot <mateusz at loskot.net> wrote:
> Hi,
> This is my first post here, so hello everyone.
> I was searching through the list archives and I stumbled upon this post
> http://lists.idyll.org/pipermail/testing-in-python/2007-May/000298.html
> in which Benji mentions about writing
> "a parameterized test and apply the same test twice.  Once in an
> environment where the "real" thing is used and once where the "stub" is used"
> AFAIU, the idea is to write test(s) once, then parametrize for use in both
> modes, as unit tests or integration tests.
> Could anyone explain or give example how to implement
> such parametrization?
> Perhaps Benji is still around and could explain the original technique?
> I'm using Python 3.2 or 3.3 and I write my tests using
> unittest and mock (or unittest.mock) modules.
> So, I'm interested in using those to to achieve the parametrized
> tests for unit/integration testing modes.
> Best regards,
> --
> Mateusz  Łoskot, http://mateusz.loskot.net
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python

Andrés Riancho
Project Leader at w3af - http://w3af.org/
Web Application Attack and Audit Framework
Twitter: @w3af
GPG: 0x93C344F3

More information about the testing-in-python mailing list