pytest-dev / pytest-cov

Coverage plugin for pytest.
MIT License
1.72k stars 211 forks source link

Pytest and pytest-cov reload numpy #620

Closed albertino87 closed 5 months ago

albertino87 commented 8 months ago

Please go over all the sections and search https://pytest-cov.readthedocs.io/en/latest/ or https://coverage.readthedocs.io/en/latest/ before opening the issue.

Hello, I'm trying to run a test suite with pytest and pytest-cov with the command line:

pytest -c pytest_unit.ini -n 4 --cov modified_module1 --cov modified_module2 --cov modified_module3and so on

I use the --cov option because the project has thousands of files but i'm interested in a clean coverage report with only the modules that were modified.

What happens is that when at least one of the --cov modules is in a specific folder I get this warning:

rootdir: C:\Users\d608771\source\repos\enmain
configfile: pytest_unit.ini
testpaths: web/tests, integration/tests/query_integration, tests/test_unit, tests/test_dev_tools
plugins: cov-4.1.0, html-4.0.2, metadata-3.0.0, xdist-3.3.1
ready: 4/4 workers
C:\Users\d608771\source\repos\enmain\src\dc_tools\file_system\excel_operations.py:6: UserWarning: The NumPy module was reloaded (imported a second time). This can in some cases result in small but subtle issues and is discouraged.
  from numpy import dtype
C:\Users\d608771\source\repos\enmain\src\dc_tools\file_system\excel_operations.py:6: UserWarning: The NumPy module was reloaded (imported a second time). This can in some cases result in small but subtle issues and is discouraged.
  from numpy import dtype                                                                                                                                                                                                                          
C:\Users\d608771\source\repos\enmain\src\dc_tools\file_system\excel_operations.py:6: UserWarning: The NumPy module was reloaded (imported a second time). This can in some cases result in small but subtle issues and is discouraged.
  from numpy import dtype                                                                                                                                                                                                                          
C:\Users\d608771\source\repos\enmain\src\dc_tools\file_system\excel_operations.py:6: UserWarning: The NumPy module was reloaded (imported a second time). This can in some cases result in small but subtle issues and is discouraged.
  from numpy import dtype

Numpy gets reloaded once for every subprocess. Just to be clear the file mentioned (excel_operations.py) is not one of the -- cov modules. However if I run the suite without the parallelization: pytest -c pytest_unit.ini --cov modified_module1 --cov modified_module2 --cov modified_module3 the suite runs fine. The same happens if I run the suite without the coverage: pytest -c pytest_unit.ini -n 4 or with full coverage: pytest -c pytest_unit.ini -n 4 --cov

I don't know why the modules in this specific folder trigger this behaviour, unfortunately I have no idea where to start debugging this.

Many thanks

Versions

Python 3.11.6

Package Version


astroid 2.15.8
attrs 23.1.0
bcrypt 4.0.1
beautifulsoup4 4.12.2
black 23.9.1
blinker 1.6.3
build 1.0.3
cattrs 23.1.2
certifi 2023.7.22
cffi 1.16.0 charset-normalizer 3.3.0 click 8.1.7 colorama 0.4.6 contourpy 1.1.1 coverage 7.3.1 cryptography 41.0.4 cycler 0.12.1 dataclasses-json 0.6.0 deepdiff 6.5.0 dill 0.3.7 elasticsearch 7.17.9 elasticsearch-dsl 7.4.1 et-xmlfile 1.1.0 execnet 2.0.2 flasgger 0.9.7.1 Flask 2.3.3 Flask-Cors 4.0.0 Flask-HTTPAuth 4.8.0 Flask-Login 0.6.2 fonttools 4.43.1 freezegun 1.2.2 furl 2.1.3 genson 1.2.2 gitdb 4.0.10 GitPython 3.1.40 idna 3.4 iniconfig 2.0.0 isort 5.12.0 itsdangerous 2.1.2 Jinja2 3.1.2 joblib 1.3.2 jsonschema 4.19.0 jsonschema-specifications 2023.7.1 kiwisolver 1.4.5 lazy-object-proxy 1.9.0 markdown2 2.4.10 MarkupSafe 2.1.3 marshmallow 3.20.1 matplotlib 3.7.3 mccabe 0.7.0 mistune 3.0.2 mypy-extensions 1.0.0 numpy 1.25.2 openpyxl 3.1.2 oracledb 1.4.2 ordered-set 4.1.0 orderedmultidict 1.0.1 packaging 23.2 pandas 1.5.3 paramiko 3.3.1 pathspec 0.11.2 Pillow 10.0.1 pip 23.2.1 pip-tools 7.3.0 platformdirs 3.11.0 pluggy 1.3.0 psutil 5.9.5 pwlf 2.2.1 pycparser 2.21 pyDOE 0.3.8 pylint 2.17.5 PyMySQL 1.1.0 PyNaCl 1.5.0 pyparsing 3.1.1 pyproject_hooks 1.0.0 pysftp 0.2.9 pytest 7.4.2 pytest-cov 4.1.0 pytest-html 4.0.2 pytest-metadata 3.0.0 pytest-xdist 3.3.1 python-dateutil 2.8.2 python-slugify 8.0.1 pytz 2023.3.post1 PyYAML 6.0.1 referencing 0.30.2 requests 2.31.0 rpds-py 0.10.6 scikit-learn 1.3.0 scipy 1.11.2 seaborn 0.12.2 setuptools 68.2.2 six 1.16.0 smmap 5.0.1 soupsieve 2.5 termcolor 2.3.0 text-unidecode 1.3 threadpoolctl 3.2.0 tomlkit 0.12.1 typing_extensions 4.8.0 typing-inspect 0.9.0 urllib3 1.26.16 Werkzeug 2.3.7 wheel 0.41.2 wrapt 1.15.0 XlsxWriter 3.1.3

Config

pytest_unit.ini:

[pytest]
testpaths =
    web/tests
    integration/tests/query_integration
    tests/test_unit
    tests/test_dev_tools
junit_duration_report = total
ionelmc commented 5 months ago

The pytest-cov plugin doesn't mess with with sys.modules. Coverage and Pytest might look that data structure but I am pretty sure those don't perform any reload on anything.