pytest fixture - get value and avoid error Fixture X called directly

2024/10/11 2:23:38

I have updated pytest to 4.3.0 and now I need to rework test code since calling fixtures directly is deprecated.

I have an issue with fixtures used in an unittest.TestCase, how do I get the value returned from the fixture and not a reference to the function itself ?

Example :

@pytest.fixture
def test_value():return 1@pytest.mark.usefixtures("test_value")
class test_class(unittest.TestCase):def test_simple_in_class(self):print(test_value)    # prints the function reference and not the valueprint(test_value())  # fails with Fixtures are not meant to be called directlydef test_simple(test_value):print(test_value)  # prints 1

How can I get test_value in the test_simple_in_class() method ?

Answer

The solution to my simple example if anyone is interested.

def my_original_fixture():return 1@pytest.fixture(name="my_original_fixture")
def my_original_fixture_indirect():return my_original_fixture()@pytest.mark.usefixtures("my_original_fixture")
class test_class(unittest.TestCase):def test_simple_in_class(self):print(my_original_fixture())def test_simple(my_original_fixture):print(my_original_fixture)
https://en.xdnf.cn/q/69822.html

Related Q&A

Django limit the number of requests per minute

Im trying to limit the number of requests from an IP in case I get too many requests from it. For example: if I will get more than 50 requests per minute I want to block that IP for 5 minutes. When I u…

How to add template variable in the filename of an EmailOperator task? (Airflow)

I cant seem to get this to work.I am trying to send daily a given file, whose name is like file_{{ds_nodash}}.csv.The problem is that I cant seem to add this name as the filename, since it seems it can…

Disadvantage of Python eggs?

Are there any disadvantages about using eggs through easy-install compared to the "traditional" packages/modules/libs?

Does Google App Engine Flex support Pipfile?

For App Engine Standard the explicitly state that they do not support Pipfiles and immediately block you from pushing your project if it contains a Pipfile. In searching the documentation, I dont see a…

Keras sees my GPU but doesnt use it when training a neural network

My GPU is not used by Keras/TensorFlow.To try to make my GPU working with tensorflow, I installed tensorflow-gpu via pip (I am using Anaconda on Windows)I have nvidia 1080tiprint(tf.test.is_gpu_availab…

Identify if there are two of the same character adjacent to eachother

Ive been asked to create a program that identifies if a password is valid or not. The one part I am struggling with is identifying whether there are two of the same character adjacent to each other. He…

Extract lined table from scanned document opencv python

I want to extract the information from a scanned table and store it a csv. Right now my table extraction algorithm does the following steps.Apply skew correction Apply a gaussian filter for denoising. …

Nested Python C Extensions/Modules?

How do I compile a C-Python module such that it is local to another? E.g. if I have a module named "bar" and another module named "mymodule", how do I compile "bar" so th…

ImportError: No module named sysconfig--cant get pip working

Im really struggling with pip on a RedHat 6.9 system. Every time I tried to use pip, I got ImportError: No module named sysconfigI tried Googling for solutions. I dont have apt-get and cant seem to get…

Convert Dataframe to a Dictionary with List Values

Suppose I have a Dataframe df :Label1 Label2 Label3 key1 col1value1 col2value1 key2 col1value2 col2value2 key3 col1value3 col2value3dict1 = df.set_index(Label1).to_dic…