New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Can someone explain the below uses of @pytest.mark.usefixtures #3308

Closed
thakkardharmik opened this Issue Mar 14, 2018 · 11 comments

Comments

Projects
None yet
6 participants
@thakkardharmik

thakkardharmik commented Mar 14, 2018

Question: Can someone explain the below two uses of @pytest.mark.usefixtures and how does each of them work. I would like to understand the difference in the marker and fixture definition/usage.

@pytest.fixture()
def populateList(f):
        assert 1 == 0

@pytest.mark.usefixtures(populateList)
def invokeFixture():
        pass
@pytest.fixture()
def populateList(f):
        assert 1 == 0

@pytest.mark.usefixtures("populateList")
def test_1():
        assert 0 == 0
@pytestbot

This comment has been minimized.

pytestbot commented Mar 14, 2018

GitMate.io thinks possibly related issues are #378 (@pytest.mark.usefixtures take no effects on fixtures), #121 (make pytest.mark available outside pytest), #2401 (pytest.mark not evaluated with pytest_generate_tests), #1111 (pytest.mark.parametrize fails with lambdas), and #666 (pytest.mark.skipif on class method uses "reason" from class level pytestmark).

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Mar 14, 2018

  1. is not supported
  2. looks like a example that should work but breaks without more context

usefixtures uses the fixture names as strings

@thakkardharmik

This comment has been minimized.

thakkardharmik commented Mar 14, 2018

@RonnyPfannschmidt I see the usage of 1 in pytest doc.
https://docs.pytest.org/en/latest/usage.html#creating-junitxml-format-files

import pytest

@pytest.fixture(scope="session")
def log_global_env_facts(f):

    if pytest.config.pluginmanager.hasplugin('junitxml'):
        my_junit = getattr(pytest.config, '_xml', None)

    my_junit.add_global_property('ARCH', 'PPC')
    my_junit.add_global_property('STORAGE_TYPE', 'CEPH')

@pytest.mark.usefixtures(log_global_env_facts)
def start_and_prepare_env():
    pass

class TestMe(object):
    def test_foo(self):
        assert True
@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Mar 14, 2018

@thakkardharmik thanks for pointing that out, that seems like a mistake - to be specific that code is completely incorrect

@thakkardharmik

This comment has been minimized.

thakkardharmik commented Mar 14, 2018

@RonnyPfannschmidt though if I try to use similar idea in 1 it does work for me. Can you elaborate on what its trying to do there in scenario 1 though its not supported and how its working.

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Mar 14, 2018

@tareqalayan the code paths im aware of support only function names there - if having a function there does actually have an effect thats an accident and should actually trigger an error

@thakkardharmik

This comment has been minimized.

thakkardharmik commented Mar 14, 2018

@RonnyPfannschmidt Thanks. Also you can see there is a slight change in fixture definition. When we pass function as an argument to usesfixture, the fixture too takes an parameter. I didnt quiet really understand what is happening there.

@pytest.fixture()
def populateList(f):
        assert 1 == 0

@pytest.mark.usefixtures(populateList)
def invokeFixture():
        pass

If we dont define 'f' as an parameter to the fixture it throws an error.

@RonnyPfannschmidt

This comment has been minimized.

Member

RonnyPfannschmidt commented Mar 14, 2018

please show that error

@akr8986

This comment has been minimized.

akr8986 commented Mar 14, 2018

tareqalayan added a commit to tareqalayan/pytest that referenced this issue Mar 14, 2018

@tareqalayan

This comment has been minimized.

Contributor

tareqalayan commented Mar 14, 2018

@RonnyPfannschmidt yes it should be log_global_env_facts.name

tareqalayan added a commit to tareqalayan/pytest that referenced this issue Mar 14, 2018

RonnyPfannschmidt added a commit that referenced this issue Mar 14, 2018

Merge pull request #3309 from tareqalayan/issue-3308
issue #3308: fix example in documentation
@nicoddemus

This comment has been minimized.

Member

nicoddemus commented Mar 14, 2018

Fixed in #3309

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment