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

Setting a fixture scope of "package" will not teardown the fixture #3796

Closed
hayleyday opened this Issue Aug 9, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@hayleyday

hayleyday commented Aug 9, 2018

I am using Windows 10, Python 3.6, and pytest 3.7.1 or 3.7.0 (tested with both).

Here are the libraries I have installed:
atomicwrites 1.1.5
attrs 18.1.0
colorama 0.3.9
more-itertools 4.3.0
pip 9.0.3
pluggy 0.7.1
psutil 5.4.6
py 1.5.4
pyserial 3.4
pytest 3.7.1
setuptools 40.0.0
six 1.11.0
websocket-server 0.4

It appears as though when a fixture is setup with a scope of package, the teardown code does not execute.

Simple example:

@pytest.fixture(scope="package")
def example():
   setup_code()
   yield something
   teardown_code()

For this example, "teardown_code()" is never executed for me.

@nicoddemus

This comment has been minimized.

Show comment
Hide comment
@nicoddemus

nicoddemus Aug 9, 2018

Member

Hi @hayleyday,

Thanks for reporting it. I could not reproduce it with this example though:

import pytest

@pytest.fixture(scope='package')
def fix():
    print('setup')
    yield 1
    print('teardown')

def test(fix):
    assert fix == 1
λ pytest -s -q .tmp\test_pkg_fix.py
setup
.teardown

1 passed in 0.01 seconds

Can you please provide a reproducible example?

Member

nicoddemus commented Aug 9, 2018

Hi @hayleyday,

Thanks for reporting it. I could not reproduce it with this example though:

import pytest

@pytest.fixture(scope='package')
def fix():
    print('setup')
    yield 1
    print('teardown')

def test(fix):
    assert fix == 1
λ pytest -s -q .tmp\test_pkg_fix.py
setup
.teardown

1 passed in 0.01 seconds

Can you please provide a reproducible example?

@hayleyday

This comment has been minimized.

Show comment
Hide comment
@hayleyday

hayleyday Aug 14, 2018

@nicoddemus It appears that the issue I reported is invalid and that this is not the actual issue I am experiencing. My apologies for that. I might need some clarification on the intent/design of the package scope. Here is an explanation of the issue I am experiencing:

If I have multiple packages in my full overall test package, say test_foo; test_bar; test_baz, each with their own conftest.py and test modules, the tests execute in this order when I run the full overall test package:

  1. test_bar package - setup code, asserts for bar modules
  2. test_baz package - setup code, asserts for baz modules
  3. test_foo package - setup code, asserts for foo modules
  4. foo package teardown code
  5. baz package teardown code
  6. bar package teardown code

See attached screenshot. Please advise if this is intended behavior. Thank you very much for your time!

pytest_issue_3796

hayleyday commented Aug 14, 2018

@nicoddemus It appears that the issue I reported is invalid and that this is not the actual issue I am experiencing. My apologies for that. I might need some clarification on the intent/design of the package scope. Here is an explanation of the issue I am experiencing:

If I have multiple packages in my full overall test package, say test_foo; test_bar; test_baz, each with their own conftest.py and test modules, the tests execute in this order when I run the full overall test package:

  1. test_bar package - setup code, asserts for bar modules
  2. test_baz package - setup code, asserts for baz modules
  3. test_foo package - setup code, asserts for foo modules
  4. foo package teardown code
  5. baz package teardown code
  6. bar package teardown code

See attached screenshot. Please advise if this is intended behavior. Thank you very much for your time!

pytest_issue_3796

@nicoddemus

This comment has been minimized.

Show comment
Hide comment
@nicoddemus

nicoddemus Aug 24, 2018

Member

Hi @hayleyday sorry for the delay.

Hmm I would expect the tear down of the packages to occur right after the last test of the module inside of the package to finish. It is late now and I need to go to bed, but I will ping @jonozzz in case he has any comments. 👍

Member

nicoddemus commented Aug 24, 2018

Hi @hayleyday sorry for the delay.

Hmm I would expect the tear down of the packages to occur right after the last test of the module inside of the package to finish. It is late now and I need to go to bed, but I will ping @jonozzz in case he has any comments. 👍

jonozzz pushed a commit to jonozzz/pytest that referenced this issue Aug 24, 2018

@jonozzz jonozzz referenced this issue Aug 25, 2018

Merged

Fix #3854 #3861

@nicoddemus nicoddemus closed this in #3861 Aug 25, 2018

cmccandless added a commit to cmccandless/multisite that referenced this issue Aug 30, 2018

Update pytest to 3.7.4 (#10)
This PR updates [pytest](https://pypi.org/project/pytest) from **3.7.2** to **3.7.4**.



<details>
  <summary>Changelog</summary>
  
  
   ### 3.7.4
   ```
   =========================

Bug Fixes
---------

- `3506 &lt;https://github.com/pytest-dev/pytest/issues/3506&gt;`_: Fix possible infinite recursion when writing ``.pyc`` files.


- `3853 &lt;https://github.com/pytest-dev/pytest/issues/3853&gt;`_: Cache plugin now obeys the ``-q`` flag when ``--last-failed`` and ``--failed-first`` flags are used.


- `3883 &lt;https://github.com/pytest-dev/pytest/issues/3883&gt;`_: Fix bad console output when using ``console_output_style=classic``.


- `3888 &lt;https://github.com/pytest-dev/pytest/issues/3888&gt;`_: Fix macOS specific code using ``capturemanager`` plugin in doctests.



Improved Documentation
----------------------

- `3902 &lt;https://github.com/pytest-dev/pytest/issues/3902&gt;`_: Fix pytest.org links
   ```
   
  
  
   ### 3.7.3
   ```
   =========================

Bug Fixes
---------

- `3033 &lt;https://github.com/pytest-dev/pytest/issues/3033&gt;`_: Fixtures during teardown can again use ``capsys`` and ``capfd`` to inspect output captured during tests.


- `3773 &lt;https://github.com/pytest-dev/pytest/issues/3773&gt;`_: Fix collection of tests from ``__init__.py`` files if they match the ``python_files`` configuration option.


- `3796 &lt;https://github.com/pytest-dev/pytest/issues/3796&gt;`_: Fix issue where teardown of fixtures of consecutive sub-packages were executed once, at the end of the outer
  package.


- `3816 &lt;https://github.com/pytest-dev/pytest/issues/3816&gt;`_: Fix bug where ``--show-capture=no`` option would still show logs printed during fixture teardown.


- `3819 &lt;https://github.com/pytest-dev/pytest/issues/3819&gt;`_: Fix ``stdout/stderr`` not getting captured when real-time cli logging is active.


- `3843 &lt;https://github.com/pytest-dev/pytest/issues/3843&gt;`_: Fix collection error when specifying test functions directly in the command line using ``test.py::test`` syntax together with ``--doctest-modules``.


- `3848 &lt;https://github.com/pytest-dev/pytest/issues/3848&gt;`_: Fix bugs where unicode arguments could not be passed to ``testdir.runpytest`` on Python 2.


- `3854 &lt;https://github.com/pytest-dev/pytest/issues/3854&gt;`_: Fix double collection of tests within packages when the filename starts with a capital letter.



Improved Documentation
----------------------

- `3824 &lt;https://github.com/pytest-dev/pytest/issues/3824&gt;`_: Added example for multiple glob pattern matches in ``python_files``.


- `3833 &lt;https://github.com/pytest-dev/pytest/issues/3833&gt;`_: Added missing docs for ``pytester.Testdir``.


- `3870 &lt;https://github.com/pytest-dev/pytest/issues/3870&gt;`_: Correct documentation for setuptools integration.



Trivial/Internal Changes
------------------------

- `3826 &lt;https://github.com/pytest-dev/pytest/issues/3826&gt;`_: Replace broken type annotations with type comments.


- `3845 &lt;https://github.com/pytest-dev/pytest/issues/3845&gt;`_: Remove a reference to issue `568 &lt;https://github.com/pytest-dev/pytest/issues/568&gt;`_ from the documentation, which has since been
  fixed.
   ```
   
  
</details>


 

<details>
  <summary>Links</summary>
  
  - PyPI: https://pypi.org/project/pytest
  - Changelog: https://pyup.io/changelogs/pytest/
  - Homepage: https://docs.pytest.org/en/latest/
</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment