Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py.test session level fixtures in setup_method

Tags:

python

pytest

Is there a way to somehow use pytest fixtures from conftest.py in a test class's setup? I need to initialize an object when the session starts and use it in some test classes' setup.

Something like this:

# conftest.py:

import pytest

@pytest.fixture(scope="session", autouse=True)
def myfixture(request):
    return "myfixture"

# test_aaa.py

class TestAAA(object):

    def setup(self, method, myfixture):
        print("setup myfixture: {}".format(myfixture))

    ...
like image 319
adi Avatar asked May 25 '16 12:05

adi


People also ask

Can a pytest fixture be a test?

Those are the things that need to test a certain action. In pytest the fixtures are functions that we define to serve these purpose, we can pass these fixtures to our test functions (test cases) so that they can run and set up the desired state for you to perform the test.

What is pytest fixture ()?

Pytest fixtures are functions that can be used to manage our apps states and dependencies. Most importantly, they can provide data for testing and a wide range of value types when explicitly called by our testing software. You can use the mock data that fixtures create across multiple tests.

How many times will a fixture of class scope run for the tests of a class?

Class: With Class scope, one fixture will be created per class object. Session: With the Session scope, the fixture will be created only once for entire test session.

What gives the use of the Setup_method () method in pytest?

pytest teardown method class. setup_method is invoked for every test method of a class.

What is a test fixture in Python?

Summary: in this tutorial, you’ll learn about Python test fixtures including setUp () and tearDown () methods. By definition, a test fixture is a function or method that runs before and after a block of test code executes. In other words, it is a step carried out before or after a test. Suppose you have a test module called test_my_module.py.

What is session scope in pytest?

In pytest fixtures nuts and bolts, I noted that you can specify session scope so that a fixture will only run once per test session and be available across multiple test functions, classes, and modules. In this post, I’m going to show a simple example so you can see it in action.

Where to put fixture code in pytest?

With function, class, and module scope, it is completely reasonable for the fixture code to be in the same file as the tests. But now suddenly, with session, that doesn’t make sense anymore. We can put them in conftest.py. This is a special named file that pytest looks for.

How do I get the current test function in pytest?

request.cls.db = db This is done using the request parameter provided by pytest to give the request of the current test function. To use this fixture on a class, we simply deocarate the class as follows:


2 Answers

I used this kind of a setup for a test class with pytest<=3.7.0 (it stopped working with pytest 3.7.1 release):

# conftest.py:

import pytest

# autouse=True does not work for fixtures that return value
# request param for fixture function is only required if the fixture uses it:
# e.g. for a teardown or parametrization. Otherwise don't use it.
@pytest.fixture(scope="session")
def myfixture():
    return "myfixture"

# test_aaa.py

import pytest

class TestAAA(object):
    @classmethod
    @pytest.fixture(scope="class", autouse=True)
    def setup(self, myfixture):
        self.myfixture = myfixture

    def test_function1(self):
        # now you can use myfixture without passing it as param to each test function   
        assert self.myfixture == "myfixture"
like image 117
Dmitry Tokarev Avatar answered Sep 21 '22 15:09

Dmitry Tokarev


I don't think you can do it directly. However, you can decorate the whole class with pytest.mark.usefixtures, if that helps:

@pytest.mark.usefixtures(['myfixture'])
class TestAAA(object):
    ...

IIRC, setup_method will be called before any of the automatically applied fixtures.

You can also utilize autouse for class-level fixtures like so:

class TestAAA(object):
    @pytest.fixture(autouse=True)
    def init_aaa(self, myfixture):
        ...
like image 28
aldanor Avatar answered Sep 20 '22 15:09

aldanor