Pytest: [Question] Is there any known workaround for direct fixture injection when unittest.testcase is in the inheritance tree?

Created on 15 Oct 2019  路  2Comments  路  Source: pytest-dev/pytest

Hi, we use a common library for system level testing, unfortunately it is tightly coupled to unittest.testcase - we use pytest but obviously this presents some issue(s), primarily lack of full fixture capabilities and parametrization.

Is there any known work around to make this work? I am having a look at the makeitem hook, but I am unsure of how to actually solve it with that.

removing unittest.testcase from the inheritance would require us to fork the library (which we really don't want to do and replacing it out is not viable either as its such a massive piece at the core of our code). Can we hack something somehow to achieve this?

Thanks!

question

Most helpful comment

Hi @symonk,

For parametrization you are out of luck, there's no easy workaround for that.

For fixtures though you can use an autouse fixture to inject other fixtures you need:


class T(unittest.TestCase);

    @pytest.fixture(autouse=True)
    def _inject_fixtures(self, tmpdir, capsys):
        self.tmpdir = tmpdir
        self.capsys = capsys

    def test_foo(self):
        self.tmpdir.ensure(dir=1)
        ...

All 2 comments

Hi @symonk,

For parametrization you are out of luck, there's no easy workaround for that.

For fixtures though you can use an autouse fixture to inject other fixtures you need:


class T(unittest.TestCase);

    @pytest.fixture(autouse=True)
    def _inject_fixtures(self, tmpdir, capsys):
        self.tmpdir = tmpdir
        self.capsys = capsys

    def test_foo(self):
        self.tmpdir.ensure(dir=1)
        ...

@symonk I鈥檇 take a look at https://github.com/seleniumbase/SeleniumBase/blob/master/examples/test_usefixtures.py , which lets you use pytest fixtures with SeleniumBase, which also inherits from unittest.TestCase .

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomalkalak picture tomalkalak  路  3Comments

jurisbu picture jurisbu  路  3Comments

jacquerie picture jacquerie  路  3Comments

RonnyPfannschmidt picture RonnyPfannschmidt  路  3Comments

Sup3rGeo picture Sup3rGeo  路  3Comments