Skip to content

Fixture scope documentation #538

Closed

Description

@pytestbot

Originally reported by: Mark Stacey (BitBucket: Gudahtt, GitHub: Gudahtt)


The fixture documentation briefly mentions the possibility of a fixture with class scope in the beginning summary, but it isn't mentioned anywhere else in the document. I had to check the source code to see if this was possible.

Also, I didn't see any mention that the default scope was "function" - it was implied, but not explicit.

I think it would be helpful to include a full list of available scopes, and to explicitly say that function scope is default. Both of these things are included in the docstring for the fixture function in the source code:

#!python

:arg scope: the scope for which this fixture is shared, one of
                "function" (default), "class", "module", "session".

Activity

  1. pytestbot commented on Jul 19, 2014

    @pytestbot
    ContributorAuthor

    Original comment by yuliang shao (BitBucket: markshao, GitHub: markshao):


    agree to enhance the documentation

  2. pytestbot commented on Jul 19, 2014

    @pytestbot
    ContributorAuthor

    Original comment by Sorin Sbarnea (BitBucket: sorin, GitHub: sorin):


    I don't know I I should raise this as a different bug or keep it here. I wasn't able to find a solution for this in the documentaiton. I do have a set of unittests written using the python's unittest module. They are using the setUpModule() function to load a global variable with the shared "stuff" that is required to run the tests (including some http sesions).

    When run normally my tests was running fine but with py.test they are failing. I patched it a little bit to make it run using the old pytest fixture functions (which happen not to have same names as unittest ones). Worked but only when not executed on multiple threads, and that's a feature I do want to use.

    The documentation examples are useless in my case because I do have like 20 classes (unittest.TestCase) with 10 tests inside each class. Obviously I do not want to add a new parameter to each test.

    Until now I used the class setUp() method to load the shared dictionare inside self.

  3. pytestbot commented on Jul 20, 2014

    @pytestbot
    ContributorAuthor

    Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


    On Sat, Jul 19, 2014 at 07:49 -0000, yuliang shao wrote:

    If someone can submit a PR for enhanced docs, i'd happily review and merge
    them.

    best,
    holger

  4. added
    type: enhancementnew feature or API change, should be merged into features branch
    type: docsdocumentation improvement, missing or needing clarification
    on Jun 15, 2015
  5. DuncanBetts commented on Nov 26, 2016

    @DuncanBetts
    Contributor

    I've read the documentation, and think the Sharing a fixture across tests in a module (or class/session) section makes this clear enough. Otherwise (this is a rough draft) how about:

    Extending the previous example, we can add a scope='module' parameter to the @pytest.fixture invocation to cause the decorated smtp fixture function's default behaviour of invoking once per function, to instead only be invoked once per test module.

  6. nicoddemus commented on Nov 26, 2016

    @nicoddemus
    Member

    Where would you add that paragraph @DuncanBetts? Perhaps we can discuss this over the PR itself? 😁

  7. bilderbuchi commented on Oct 16, 2017

    @bilderbuchi
    Contributor

    I plan to take care of this issue. Most of it has been added to the docs in the meantime, just needs some rounding out of the relevant sections.

  8. nicoddemus commented on Oct 17, 2017

    @nicoddemus
    Member

    Awesome @bilderbuchi, thanks!

  9. added 2 commits that reference this issue on Oct 17, 2017
    b52636c
    baadd56
  10. bilderbuchi commented on Oct 17, 2017

    @bilderbuchi
    Contributor

    Done, see #2848.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: docsdocumentation improvement, missing or needing clarificationtype: enhancementnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions