Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest fixture is not getting called in class

I recently started working on a python project. In that project, I am writing test cases using pytest. In that, I tried using pytest-fixtures and understood the basic concepts of it.

But I found difficulty using fixture in a particular case, when I was trying to use these fixtures in a class.

Sample Code:

import pytest

from flask.ext.testing import TestCase

from flask_application import app

@pytest.fixture(scope="module")
def some_fix():
    return "yes"

class TestDirectoryInit(TestCase):
    def create_app(self):
        return app

    def test_one(self, some_fix):
        assert some_fix == "yes"

when I am running this test, it is throwing me an error:

TypeError: test_one() takes exactly 2 arguments (1 given)

But when I changed this code a little bit like this:

@pytest.fixture(scope="module")
def some_fix():
    return "yes"

class TestDirectoryInit():

    def test_one(self, some_fix):
        assert some_fix == "yes"

Now this test case is getting passed. I am not able to understand that why it is behaving differently due to extending a TestCase class.

Any useful suggestion will be appreciated! Thanks!

like image 200
yugantar kumar Avatar asked Sep 10 '25 18:09

yugantar kumar


1 Answers

flask.ext.testing.TestCase is a subclass of unittest.TestCase.

If you want to be able to use pytest fixtures with unittest, please read this:

Mixing pytest fixtures into unittest

like image 94
DevLounge Avatar answered Sep 13 '25 08:09

DevLounge