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

Use a callable to define fixture scoped dynamically #1682

Closed
nicoddemus opened this issue Jun 27, 2016 · 14 comments

Comments

@nicoddemus
Copy link
Member

commented Jun 27, 2016

Allow scope parameter of fixtures to receive a callable instead of a string. When a callable, it will be called with the config object (passed as keyword argument) and should return a string, which will determine the scope of the fixture. This function will evaluate only once.

The idea is that developers might be interested to have different scopes for a fixture depending on a command line option, so that one might choose to change a fixture scope to session to avoid costly initialization during development, but opt to change it to function in a C.I. server.

Suggested by @hpk42

@nicoddemus nicoddemus added this to the 3.0 milestone Jun 27, 2016
@nicoddemus nicoddemus self-assigned this Jun 27, 2016
@The-Compiler The-Compiler changed the title Use a callable to define fixture scoped dinamically Use a callable to define fixture scoped dynamically Jun 27, 2016
@RonnyPfannschmidt

This comment has been minimized.

Copy link
Member

commented Jun 27, 2016

we should allow this to take the current request as well (so one can be function scoped, and defer module/class scope session for example)

@nicoddemus

This comment has been minimized.

Copy link
Member Author

commented Jun 27, 2016

The idea was to evaluate the scope function only once, in which case the request object would not be useful.

@RonnyPfannschmidt

This comment has been minimized.

Copy link
Member

commented Jun 27, 2016

i see

@nicoddemus

This comment has been minimized.

Copy link
Member Author

commented Jun 27, 2016

But don't let that stop you from making a suggestion 😄

@The-Compiler

This comment has been minimized.

Copy link
Member

commented Aug 5, 2016

While this is a nice feature, it shouldn't block 3.0. Changing the milestone to 3.1 for now.

@The-Compiler The-Compiler modified the milestones: 3.1.0, 3.0 Aug 5, 2016
@itaykatz

This comment has been minimized.

Copy link

commented Apr 24, 2017

Is there any workaround for the suggested mechanism (maybe scope changing using pytest_generate_tests() ?

@RonnyPfannschmidt RonnyPfannschmidt modified the milestones: 3.1, 3.2 Jul 21, 2017
@rajivgup

This comment has been minimized.

Copy link

commented Jan 24, 2018

I still see the status of this issue is open. Is this change committed?

@RonnyPfannschmidt RonnyPfannschmidt removed this from the 3.2 milestone Jan 24, 2018
@nicoddemus

This comment has been minimized.

Copy link
Member Author

commented Jan 24, 2018

I still see the status of this issue is open. Is this change committed?

It has not been implemented yet if I understood your question correctly (usually when something gets implemented we close the issue with a link to the related PR).

@nicoddemus

This comment has been minimized.

Copy link
Member Author

commented Jan 24, 2018

@itaykatz

Is there any workaround for the suggested mechanism (maybe scope changing using pytest_generate_tests() ?

Not that I'm aware of, I'm afraid.

@nicoddemus nicoddemus removed their assignment Apr 11, 2018
@aklajnert

This comment has been minimized.

Copy link
Contributor

commented Aug 16, 2019

@nicoddemus, I wanted to implement this, so I did some research. It seems to conflict with the direct decoration for the fixtures. The scope parameter already can be callable, I assume it is because it is the first argument for the fixture decorator. It allows defining fixture by executing the decorator as a function.

Any idea how to implement the dynamic scope without breaking the current functionality? Maybe the callable should have a specific name?

@RonnyPfannschmidt

This comment has been minimized.

Copy link
Member

commented Aug 16, 2019

def fixture(scope="function", params=None, autouse=False, ids=None, name=None):

should be turned into

def fixture(maybe_callable_or_scope=None, *, scope="function", params=None, autouse=False, ids=None, name=None):

there should be a warning for non-kwarg scope passover it should override while its in transition period

@NargiT

This comment has been minimized.

Copy link

commented Sep 30, 2019

@asottile

This comment has been minimized.

Copy link
Member

commented Oct 3, 2019

here's a fix for that -- thanks @NargiT! #5910

@asottile

This comment has been minimized.

Copy link
Member

commented Oct 3, 2019

closing this out since it landed in 5.2 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants
You can’t perform that action at this time.