Closed kloczek closed 1 year ago
It does appear as if two of the tests are dependent on access to the network to succeed. I'll need to work out a way to exercise that behavior in an isolated environment or skip those tests.
Just tested 10.0.1 ..
+ /usr/bin/pytest -ra -m 'not network'
==================================================================================== test session starts ====================================================================================
platform linux -- Python 3.8.16, pytest-7.2.0, pluggy-1.0.0
rootdir: /home/tkloczko/rpmbuild/BUILD/pip-run-10.0.1, configfile: pytest.ini
collected 34 items
pip_run/commands.py ... [ 8%]
pip_run/deps.py . [ 11%]
pip_run/read-deps.py . [ 14%]
pip_run/scripts.py .. [ 20%]
pip_run/mode/persist.py . [ 23%]
tests/test_deps.py .......... [ 52%]
tests/test_launch.py ... [ 61%]
tests/test_scripts.py E..........E. [100%]
========================================================================================== ERRORS ===========================================================================================
____________________________________________________________________________ ERROR at setup of test_pkg_imported ____________________________________________________________________________
file /home/tkloczko/rpmbuild/BUILD/pip-run-10.0.1/tests/test_scripts.py, line 16
@pytest.mark.usefixtures('needs_internet')
def test_pkg_imported(tmp_path):
E fixture 'needs_internet' not found
> available fixtures: alt_cache_dir, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, monkeypatch_session, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, reqs_files, run_mode, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
/home/tkloczko/rpmbuild/BUILD/pip-run-10.0.1/tests/test_scripts.py:16
__________________________________________________________________ ERROR at setup of test_pkg_loaded_from_alternate_index ___________________________________________________________________
file /home/tkloczko/rpmbuild/BUILD/pip-run-10.0.1/tests/test_scripts.py, line 157
@pytest.mark.usefixtures('needs_internet')
def test_pkg_loaded_from_alternate_index(tmp_path):
E fixture 'needs_internet' not found
> available fixtures: alt_cache_dir, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, monkeypatch_session, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, reqs_files, run_mode, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
/home/tkloczko/rpmbuild/BUILD/pip-run-10.0.1/tests/test_scripts.py:157
================================================================================== short test summary info ==================================================================================
ERROR tests/test_scripts.py::test_pkg_imported
ERROR tests/test_scripts.py::test_pkg_loaded_from_alternate_index
=============================================================================== 32 passed, 2 errors in 2.85s ================================================================================
Why not use standard pytest mark settings passed in -m "not network"
by add @pytest.mark.network
? 🤔
This would not require additioanl module with pytest fixture ..
s/standard pytest mark/network
mark used in many other modules test suites/
PS. I have difficulty with finding which one module provides needs_internet
fixture 🤔
Why not use the
network
mark used in many other modules test suites?
I guess because I was unaware of such a convention. Is it documented anywhere? I found one such place where the marker was used: https://github.com/jazzband/pip-tools/pull/917.
The needs_internet
fixture works automatically. Instead of merely marking the tests and leaving it to the user to filter on it, it performs a test automatically skips tests that rely on the network. I extracted it from a technique I employed 11 years ago for the pmxbot project.
PS. I have difficulty with finding which one module provides
needs_internet
fixture 🤔
In order to have the fixture, you need the dependency installed. To find all of the fixtures available to the pip-run
package, first install the dependencies for pip-run[testing]
, then run pytest --fixtures
, which will emit:
------------------------------------------------------- fixtures defined from jaraco.test.http -------------------------------------------------------
needs_internet -- .tox/python/lib/python3.11/site-packages/jaraco/test/http.py:20
no docstring available
There's also a --fixtures-per-test
option:
pip-run main $ .tox/python/bin/pytest --fixtures-per-test -k test_pkg_imported
================================================================ test session starts =================================================================
platform darwin -- Python 3.11.1, pytest-7.2.0, pluggy-1.0.0
rootdir: /Users/jaraco/code/jaraco/pip-run, configfile: pytest.ini
plugins: jaraco.test-5.2.0, black-0.3.12, mypy-0.10.3, checkdocs-2.9.0, flake8-1.1.1, enabler-2.0.0, cov-4.0.0
/Users/jaraco/code/jaraco/pip-run/.tox/python/lib/python3.11/site-packages/_pytest/nodes.py:717: PytestWarning: BlackItem is an Item subclass and should not be a collector, however its bases File are collectors.
Please split the Collectors and the Item into separate node types.
Pytest Doc example: https://docs.pytest.org/en/latest/example/nonpython.html
example pull request on a plugin: https://github.com/asmeurer/pytest-flakes/pull/40/
warnings.warn(
collected 91 items / 89 deselected / 2 selected
--------------------------------------------------------- fixtures used by test_pkg_imported ---------------------------------------------------------
------------------------------------------------------------- (tests/test_scripts.py:17) -------------------------------------------------------------
alt_cache_dir -- conftest.py:42
no docstring available
monkeypatch_session -- conftest.py:36
no docstring available
needs_internet -- .tox/python/lib/python3.11/site-packages/jaraco/test/http.py:20
no docstring available
tmp_path -- .tox/python/lib/python3.11/site-packages/_pytest/tmpdir.py:203
Return a temporary directory path object which is unique to each test
function invocation, created as a sub directory of the base temporary
directory.
tmp_path_factory -- .tox/python/lib/python3.11/site-packages/_pytest/tmpdir.py:188
Return a :class:`pytest.TempPathFactory` instance for the test session.
=============================================================== 89 deselected in 0.41s ===============================================================
Of course, because fixtures use dependency injection (entry points), there's no direct path to determine which dependency is needed to satisfy usage of a fixture. Fortunately, you have pretty good signal from the commit that added the dependency at the same time as it added the usage of the fixture.
Issue wth any public network detections is that it takes time to test that and in some cases in isolated envs it may produce mileading results.
Few weeks ago when I've started building all my from packages in build env which is cut off from public network I found that many modules suppoerts alredy -m "not nework"
pytest switch which allows on demand specify to skip test whihi needs anyting else than access to localhost.
I have alredy padckaged +1060 pytjom modules and less than 200 are affected by building in isolated env.
Amongs those ~200 about half already supports network
mark so I've even modyfied my own %pytest macro to:
# Additional command over which pytest and tox could be executed.
# Exemple: to run putest/tox over "xvfb-run -a" redefin %%__py_wrapper to that value
%__py_wrapper %{nil}
# By tefault do not run pytest units which are using network (more than localhost access)
%with_pytest_not_network 1
%pytest %{expand:\\\
%{set_build_flags} \\\
PATH=%{buildroot}%{_bindir}:$PATH \\\
LD_LIBRARY_PATH=%{buildroot}%{_libdir} \\\
PYTHONDONTWRITEBYTECODE=1 \\\
PBR_VERSION=%(v=%{version}; echo ${v%~*}) \\\
PDM_PEP517_SCM_VERSION=%(v=%{version}; echo ${v%~*}) \\\
SETUPTOOLS_SCM_PRETEND_VERSION=%(v=%{version}; echo ${v%~*}) \\\
PYTHONPATH=${PYTHONPATH:-%{buildroot}%{python3_sitearch}:%{buildroot}%{python3_sitelib}} \\\
%__py_wrapper \\\
/usr/bin/pytest -ra}%{?with_pytest_not_network: -m "not network"}
Because jaraco-test
5.2.0 hs been released only few hours ago none of the other modules would be using that.
Just tested new jaraco-test
5.2.0 and it takes 2 min until test in the fixture
https://github.com/jaraco/jaraco.test/blob/8e47a5fede7785fa7652cd330ac37680ab8b36c0/jaraco/test/http.py#L8-L11
fails to skip all network dependent units 🤔
IMO a bit better would be to follow that convention to use @pytest.mark.network
like in many other modules.
I've already removed in my spec files use explicit -m "not network"
so here is grep otput from Fedora spec files
[tkloczko@pers-jacek SPECS.fedora]$ grep 'not network' python*
python-anyio.spec:%pytest -m "not network"
python-cartopy.spec: -m "not network"
python-contextily.spec:%{pytest} -m 'not network'
python-dask.spec: -m 'not network'
python-fiona.spec:%{pytest} -m "not network and not wheel" -k "not debian" -ra || :
python-matplotlib.spec: -m 'not network' \
python-mplcairo.spec:%{python3} run-mpl-test-suite.py --tolerance=50 -m 'not network' -v -n auto \
python-pip.spec:%pytest -m 'not network' -k "$(echo $pytest_k)" \
python-rasterio.spec:%{pytest} -ra -m 'not network and not wheel' \
python-xarray.spec: -m "not network"
However in last few weeks I found that there are many more modules which are alredy supporting that pytest mark.
it takes 2 min until test in the fixture
That's interesting. I don't have the same experience. I disabled wifi on my mac, then ran tox -- -rs
and it reported the two tests were skipped. It also failed in pytest-checkdocs
, so I had to run with tox -- -p no:checkdocs -rs
and the tests all passed with all internet connectivity removed (in about 3 sec).
The fact that it takes 2 minutes makes me think the network isn't actually disabled, but that packets are being dropped, so the http handler has to time out to fail. I'd welcome some improvements to the detection algorithm (currently just urllib.request.urlopen
) to fail faster under a partially-degraded environment.
I've opened https://github.com/jaraco/jaraco.test/issues/5 to track using network
as the indicator for tests reliant on the network. My idea is that the tests should be skipped automatically, but could also be deselected with -m not network
.
I've opened jaraco/jaraco.test#5 to track using
network
as the indicator for tests reliant on the network. My idea is that the tests should be skipped automatically, but could also be deselected with-m not network
.
If it still would be possible tu use that convention used in other modules that would be great. 👍
I'm packaging your module as an rpm package so I'm using the typical PEP517 based build, install and test cycle used on building packages from non-root account.
python3 -sBm build -w --no-isolation
build
with--no-isolation
I'm using during all processes only locally installed modulesHere is pytest output:
Here is list of installed modules in build env