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

Getting "missing 1 required positional argument:" on describes_ #9

Closed
pablomolnar opened this issue Apr 18, 2016 · 2 comments
Closed

Comments

@pablomolnar
Copy link

pablomolnar commented Apr 18, 2016

Hi, could you take a look to the following code?

import pytest


def describe_books():
    @pytest.fixture
    def user():
        return ...

    @pytest.fixture
    def valid_book():
        return ...

    def invalid_book():
        return ...

    def describe_create_book(user):

        def with_valid_book(valid_book):
            ... use user + valid_book fixtures ...

        def with_invalid_book(invalid_book):
            ... use user + invalid_book fixtures ...

Getting this error:

venv/lib/python3.5/site-packages/pytest_describe/plugin.py:73: in collect
    return super(DescribeBlock, self).collect()
venv/lib/python3.5/site-packages/_pytest/python.py:465: in collect
    l.sort(key=lambda item: item.reportinfo()[:2])
venv/lib/python3.5/site-packages/_pytest/python.py:465: in <lambda>
    l.sort(key=lambda item: item.reportinfo()[:2])
venv/lib/python3.5/site-packages/_pytest/python.py:387: in reportinfo
    obj = self.obj
venv/lib/python3.5/site-packages/_pytest/python.py:351: in fget
    self._obj = obj = self._getobj()
venv/lib/python3.5/site-packages/pytest_describe/plugin.py:76: in _getobj
    return self._memoizedcall('_obj', self._importtestmodule)
venv/lib/python3.5/site-packages/_pytest/main.py:315: in _memoizedcall
    res = function()
venv/lib/python3.5/site-packages/pytest_describe/plugin.py:84: in _importtestmodule
    module = make_module_from_function(self.funcobj)
venv/lib/python3.5/site-packages/pytest_describe/plugin.py:29: in make_module_from_function
    module.__dict__.update(trace_function(funcobj))
venv/lib/python3.5/site-packages/pytest_describe/plugin.py:19: in trace_function
    funcobj(*args, **kwargs)
E   TypeError: describe_create_book() missing 1 required positional argument: 'user'

describe_create_book needs to share the user fixture to the internal use cases. What I'm doing wrong?

@ropez
Copy link
Collaborator

ropez commented Apr 19, 2016

Because the describe_ functions are evaluated during the test loading/discovery phase, they do not have access to fixtures. You need to do this instead:

def describe_books():
    @pytest.fixture
    def user():
        return ...

    @pytest.fixture
    def valid_book():
        return ...

    @pytest.fixture
    def invalid_book():
        return ...

    def describe_create_book():

        def with_valid_book(user, valid_book):
            ... use user + valid_book fixtures ...

        def with_invalid_book(user, invalid_book):
            ... use user + invalid_book fixtures ...

It would be nice to be able to do it like in your code, but that is currently not possible. It would require some real "magic" behind the scenes though. The 'user' argument would need to be some kind of proxy that resolved to the actual fixture value inside each test.

@ropez ropez closed this as completed Apr 19, 2016
@pablomolnar
Copy link
Author

"It has the additional advantage that you can have marks and fixtures that apply locally to each group of test function." gave me the impression of sharing fixtures like my example.

Definitively it would be great having the ability to group fixtures. It adds a lot of semantic value.

Anyway, thanks for getting back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants