Reputation: 1750
This question is a follow up on my previous question, here
I would like to define a fixture that will run a function for each of it's parameters, before returning it to me.
However, the function cannot run without another validation function being mocked, because the validation cannot be performed without a file system.
Here is what it looks like currently:
mocked_validation = mock.Mock() mocked_validation.return_value = True
@pytest.fixture(params = GAMELIST_ROOT_VALUES) def basic_parser(request):
return GameListParser(request.param)
@pytest.fixture @mock.patch('organizer.parser.game_list_parser.GameListParser.validate', mocked_validation) def ready_parser(basic_parser):
basic_parser.parse()
return basic_parser
However the fixture will fail at the basic_parser.parser() call, during the validation.
I use this mock for tests, it works well, so it must have to do with the fact that it's in a fixture.
Is there anyway to do that ? I do not wish to repeat this line of code for every test
This can be tested with the following class:
class GameListParser(object):
def __init__(self, value):
self.value = value
def parse(self):
if not self.validate():
raise Exception("Parsing fail")
def validate(self):
return False
Edit: Adding another try, but still failing:
@pytest.fixture
def ready_parser(basic_parser):
with mock.patch('organizer.parser.game_list_parser.GameListParser.validate') as validation:
validation.return_value = True
basic_parser.parse()
return basic_parser
Upvotes: 1
Views: 4445
Reputation: 1750
Found solution with yield:
@pytest.fixture()
def ready_parser(basic_parser):
with mock.patch('organizer.parser.game_list_parser.GameListParser.validate') as validation:
validation.return_value = True
basic_parser.parse()
yield basic_parser
But I feel sad I cannot reuse the definition I already have for mocked_validation, I tried again as a decorator and it's not working
Upvotes: 2