Skip to content

a question or a new idea about function:skip #13025

Answered by graingert
FrankWo18 asked this question in Q&A
Discussion options

You must be logged in to vote

with a context manager:

@contextlib.contextmanager
def _token():
    try:
        token = login()
    except LoginFailedError:
        token = None
    try:
        yield
    finally:
         if token is not None:
             logout(token)


def test_something():
    with _token() as token:
        ... # test here

with a fixture:

@pytest.fixture
def token():
    try:
        token = login()
    except LoginFailedError:
        token = None
    try:
        yield
    finally:
         # you don't technically need the try/finally in sync fixtures - but it is
         # needed in async fixtures so I use it everywhere
         if token is not None:
             logout(token)


def test_some…

Replies: 1 comment 16 replies

Comment options

You must be logged in to vote
16 replies
@FrankWo18
Comment options

@graingert
Comment options

@FrankWo18
Comment options

@graingert
Comment options

Answer selected by FrankWo18
@graingert
Comment options

@FrankWo18
Comment options

@FrankWo18
Comment options

@FrankWo18
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants