pytest-dev / pytest-stress

A Pytest plugin that allows you to loop tests for a user defined amount of time.
MIT License
41 stars 9 forks source link

Session getting closed after 1st loop #10

Open vij-m2 opened 3 years ago

vij-m2 commented 3 years ago

I have a set of asyncio pytest cases. They use event loop at session scope. I would like to run these tests for specified amount of time using pytest-stress plugin. Tests are getting executed in a loop, however after the first loop, all the fixtures are getting torn down. This closes the event loop required for asyncio test cases. I am guessing i will have to disable certain plugins that may be causing the teardown hooks be called. I don't know if that is what is required. Could someone kindly give some pointers. Thanks.

ImXron commented 3 years ago

Hey @vij-m2. I haven't tested this plugin with asyncio, but it should run your setup / teardown for each loop. So if you have a session scoped fixture that sets up your event loop, it should execute at the start of each loop. Let me know if I'm understanding you correctly. Feel free to post a small, but complete code example for us to work with. You can see and run a small example here.

Just in case that link above doesn't work, this is the code I just ran to test it out:

import pytest

@pytest.fixture(scope="session")
def my_session_fixture():
    print("Session fixture setup.")
    yield
    print("Session fixture teardown.")

@pytest.fixture(scope="function")
def my_function_fixture():
    print("Function fixture setup.")
    yield
    print("Function fixture teardown.")

@pytest.mark.usefixtures("my_session_fixture", "my_function_fixture")
def test_1():
    print("Test case number 1.")
    assert 1

@pytest.mark.usefixtures("my_session_fixture", "my_function_fixture")
def test_2():
    print("Test case number 2.")
    assert 1

Output:

$ pytest --seconds 5 --delay 3 -s -v
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /opt/virtualenvs/python3/bin/python
cachedir: .pytest_cache
rootdir: /home/runner/pytest-stress-10
plugins: stress-1.0.1
collected 2 items                                                              

=================================== Loop # 1 ===================================

test_test.py::test_1 Session fixture setup.
Function fixture setup.
Test case number 1.
PASSEDFunction fixture teardown.

test_test.py::test_2 Function fixture setup.
Test case number 2.
PASSEDFunction fixture teardown.
Session fixture teardown.

=================================== Loop # 2 ===================================

test_test.py::test_1 Session fixture setup.
Function fixture setup.
Test case number 1.
PASSEDFunction fixture teardown.

test_test.py::test_2 Function fixture setup.
Test case number 2.
PASSEDFunction fixture teardown.
Session fixture teardown.

=================================== Loop # 3 ===================================

test_test.py::test_1 Session fixture setup.
Function fixture setup.
Test case number 1.
PASSEDFunction fixture teardown.

test_test.py::test_2 Function fixture setup.
Test case number 2.
PASSEDFunction fixture teardown.
Session fixture teardown.

============================== 6 passed in 6.11s ===============================

Thanks!

vij-m2 commented 3 years ago

Hi ImXron, thanks for your response.

Prerequisite: pip install asyncio Here is the example:

import pytest
pytestmark = [pytest.mark.asyncio]

async def test_a():
    assert True

pytest --seconds 2 <test_path/test_file> Output is too long. Only the first iteration passes.

ImXron commented 3 years ago

Ah I see. I'm getting the same error as you now.

Deps:

python = "^3.8"
pytest-stress = "^1.0.1"
pytest = "^6.2.4"
pytest-asyncio = "^0.15.1"

This seems related to #4. There's clearly some state that's getting wiped after each session (loop). This plugin basically copies what Pytest is doing for the pytest_runtestloop hook, but throws it in a loop.

I don't have enough knowledge about Pytest's internals. I'll reach out to the Pytest community and see if someone is willing to take a look.

vij-m2 commented 3 years ago

Yes, pytest-asyncio is creating event loop only once in the first iteration. It gets closed and doesn't get recreated. Hope someone from pytest or pytest_asyncio can look into it. Thanks.