My google search here only shows this most-related answer here; though I can only have my code working for test method BUT NOT with unittest TestCase class method.
My question is how to get fixture value from test method declared within a unittest TestCase class?
I quote the code snippet here
import pytest
from unittest import TestCase
@pytest.fixture()
def fx_return_value():
yield 'some returned value'
@pytest.mark.usefixtures(fx_return_value.__name__)
def test0(fx_return_value):
print(fx_return_value)
class Test1(TestCase):
@pytest.mark.usefixtures(fx_return_value.__name__)
def test1(self, fx_return_value): #TODO Error here > TypeError: test1() missing 1 required positional argument: 'fx_return_value'
print(fx_return_value)
pytestandunittestare separate test frameworks and are not intended to be mixed. You can use test classes inpytestalongside withsetupandteardownmethods without the need of a specific base class.So to fix your problem you just have to remove the
unittest.TestCasebase from your test class.Edit:
The statement about not mixing
pytestandunittestmay be a bit misleading. For clarification: you can always rununittesttests usingpytest, just not the other way around.Pytestis specifically written with compatibility tounittest(andnosetest) in mind, maing the conversion topytesteasier.In contrast,
unittestcannot know aboutpytest, sopytestfeatures like fixtures cannot be used inunittest.