Skip to content
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
Closed
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@thakkardharmik
Copy link

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
Copy link
Contributor

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).

@pytestbot pytestbot added the type: question general question, might be closed after 2 weeks of inactivity label Mar 14, 2018
@RonnyPfannschmidt
Copy link
Member

  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
Copy link
Author

@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
Copy link
Member

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

@thakkardharmik
Copy link
Author

@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
Copy link
Member

@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
Copy link
Author

@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
Copy link
Member

please show that error

@akr8986
Copy link

akr8986 commented Mar 14, 2018 via email

tareqalayan added a commit to tareqalayan/pytest that referenced this issue Mar 14, 2018
@tareqalayan
Copy link
Contributor

@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
issue #3308: fix example in documentation
@nicoddemus
Copy link
Member

Fixed in #3309

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

6 participants