pytest-dev / pytest-randomly

:game_die: Pytest plugin to randomly order tests and control random.seed
MIT License
630 stars 30 forks source link

`-p no:randomly` causing tests to fail under Python 3.8 and 3.9 #647

Closed alexandrul closed 1 month ago

alexandrul commented 2 months ago

Python Version

3.9.20

pytest Version

8.3.2

Package Version

3.15.0

Description

As you can see in the following table, having pytest-randomly installed and using -p no:randomly results in failed tests but only under Python 3.8 and 3.9: Python 3.8 / 3.9 Python 3.10 / 3.11 / 3.12
pytest -p no:randomly tests/test_jsonreport.py::test_report_collectors fail pass
pytest tests/test_jsonreport.py::test_report_collectors pass pass

To replicate the issue, checkout this repo and run the above commands in a Python 3.9 virtualenv.

alexandrul commented 2 months ago

Output for passing tests:

❯ pytest tests/test_jsonreport.py::test_report_collectors
======================================== test session starts =========================================
platform linux -- Python 3.9.20, pytest-8.3.2, pluggy-1.5.0
Using --randomly-seed=2524090775
rootdir: ~/pytest-json-report-wip-3.9/repo
plugins: metadata-3.1.1, anyio-4.4.0, randomly-3.15.0, xdist-3.6.1, flaky-3.8.1, json-report-wip-1.5.1
collected 3 items                                                                                    

tests/test_jsonreport.py ...                                                                   [100%]

========================================= 3 passed in 0.87s ==========================================

Output for failing tests:

❯ pytest -p no:randomly tests/test_jsonreport.py::test_report_collectors
======================================== test session starts =========================================
platform linux -- Python 3.9.20, pytest-8.3.2, pluggy-1.5.0
rootdir: ~/pytest-json-report-wip-3.9/repo
plugins: metadata-3.1.1, anyio-4.4.0, xdist-3.6.1, flaky-3.8.1, json-report-wip-1.5.1
collected 3 items                                                                                    

tests/test_jsonreport.py .FF                                                                   [100%]

============================================== FAILURES ==============================================
...
==================================== 2 failed, 1 passed in 0.24s =====================================
adamchainz commented 1 month ago

This sounds to me like you have some flaky tests? I don't have time to work on your project. If you can produce a minimal reproduction that proves it's an issue in pytest-randomly, sure, I can look at it.

alexandrul commented 1 month ago

I couldn't find out the root cause yet but I've seen this conditional import and it's an interesting coincidence: https://github.com/pytest-dev/pytest-randomly/blob/main/src/pytest_randomly/__init__.py#L22-L25

adamchainz commented 1 month ago

If you can provide a minimal reproducer, I can look at that. But I don't have time to clone your whole project and try debug inside unfamiliar tools like Hatch.