How to get the request for an item in a setup hook? #9919
Answered
by
lexicalunit
lexicalunit
asked this question in
Q&A
-
Clearly from typing import Generator
import pytest
from my_app.command.base import BaseCommand
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_setup(item: pytest.Item) -> Generator[None, None, None]:
if marks := item.iter_markers(name="command"):
if mark := next(marks, None):
cls = mark.kwargs["cls"]
command: BaseCommand = cls()
if item._request.cls: # type: ignore[attr-defined]
item._request.cls.command = command # type: ignore[attr-defined]
yield To facilitate code like: from my_app.command import MyCommand
@pytest.mark.command(cls=MyCommand)
class TestMyCommand:
command: MyCommand
def test_my_command(self) -> None:
# do something with self.command ... This code is very paired down from the real thing. Hopefully it makes sense as a minimal example. |
Beta Was this translation helpful? Give feedback.
Answered by
lexicalunit
May 5, 2022
Replies: 2 comments
-
I actually just wanted the |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
lexicalunit
-
the common suggestion is a autouse fixture |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I actually just wanted the
cls
instance which I can get fromitem.cls
!