Py.Test aborts when (py)ROOT is imported - python

I've a problem unit testing Python code, which uses the (py)ROOT package. It simply aborts collecting the tests when there is a line import ROOT in one of the scripts.
Does anybody have an idea what's going on?
foo#bar ~/project/dir [19:21:17]
(project)> $ py.test -v [±master ●]
============================= test session starts ==============================
platform darwin -- Python 2.7.6 -- py-1.4.25 -- pytest-2.6.3 -- /path/to/.virtualenvs/project/bin/python
collecting 0 items[1] 16520 abort py.test -v

I had a similar issue by adding the PyROOT bindings to export $PYTHONPATH in my .bashrc. The locally set PYTHONPATH was ignored. A solution is copying or linking the ROOT.py and libPyROOT.so files to the appropriate Python directory in /usr/lib. More details in this answer: https://stackoverflow.com/a/33130000/4753851

Related

Pandas development environment: pytest does not see changes after building edited .pyx file

Q: Why is pytest not seeing changes when I edit a .pyx file and build? What step am I missing?
I'm using Visuals Studio Code with remote containers as described at the end of this page.
If I add changes to pandas/_libs/tslibs/offsets.pyx, and then run
(pandas-dev) root#60017c489843:/workspaces/pandas# python setup.py build_ext -j 4
Compiling pandas/_libs/tslibs/offsets.pyx because it changed.
[1/1] Cythonizing pandas/_libs/tslibs/offsets.pyx
/opt/conda/envs/pandas-dev/lib/python3.8/site-packages/setuptools/config/pyprojecttoml.py:108: _BetaConfiguration: Support for `[tool.setuptools]` in `pyproject.toml` is still *beta*. warnings.warn(msg, _BetaConfiguration)
# ... more output here without errors ...
my unit-test fails because it does not test against my updated version of offsets.pxy. He points to a line (see below) where the error exists only in the old version of the file.
pandas/tests/tseries/offsets/test_offsets.py ........x......................................................F
.... more output here ...
E TypeError: __init__() got an unexpected keyword argument 'milliseconds'
pandas/_libs/tslibs/offsets.pyx:325: TypeError
Whatever change I add to cdef _determine_offset and build, pytest does not see the edits, therefore I assume I'm missing a compilation step somewhere.
Reproducible example
clone my pandas fork: git clone git#github.com:markopacak/pandas.git
git checkout bug-dateoffset-milliseconds
In your dev-environment (docker container or VS Code remote container) run:
conda activate pandas-dev
python setup.py build_ext -j 4
pytest pandas/tests/tseries/offsets/test_offsets.py::TestDateOffset
Assumes you have set-up a dev environment for pandas, ideally using remote-containers on VS Code like I did.
(pandas-dev) root#60017c489843:/workspaces/pandas# python --version
Python 3.8.15
I'm pretty sure you need to install once the extensions are built (otherwise where are the built extension and how python/pytest should know where to look?). This is how my workflow looked some time ago (not sure it still applies but should be close enough):
python setup.py build_ext --inplace -j 4
python -m pip install -e . --no-build-isolation --no-use-pep517
...
pytest pandas/tests/xxxx/yyyy.py
Installing in development mode (-e) is the most convenient option in my opinion for development.

pytest-cov hangs only in Windows

I've been working on a PR for rpy2 package to improve its support for Windows, and I cannot figure out how to debug the pytest-cov/coverage related issue on a test.
Here is the link to my folk for the test, which only hangs in Windows (passes GitHub Action with Ubuntu & MacOS). Eliminating irrelevant options, the minimal pytest command is:
#!/bin/bash
pytest \
--cov=rpy2.robjects.lib \
rpy2/tests/robjects/lib
and it hangs after printing
============================================= test session starts ==============================================
platform win32 -- Python 3.9.7, pytest-7.1.2, pluggy-1.0.0
rootdir: C:\Users\tikum\Documents\Python\rpy2
plugins: cov-3.0.0
collecting ...
Debugging the pytest execution, I can follow up until it reaches coverage.py's Collector._start_tracer() (collector.py Line 278):
fn = tracer.start()
I basically run out of ideas at this point on what to do. Can anyone point me in the right direction on what to look for to resolve this issue?
Versions:
coverage 6.4.1
pytest 7.1.2
pytest-cov 3.0.0

How to get rid of pytest warnings

When I run my pytest in pipenv shell I get this:
pipenv shell
Loading .env environment variables…
Launching subshell in virtual environment…
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$ . /Users/.../.local/share/virtualenvs/kittycapital-UOiJxZhy/bin/activate
(kittycapital) bash-3.2$ python -m pytest
============================================================================================================================= test session starts ==============================================================================================================================
platform darwin -- Python 3.7.7, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: /Users/.../kittycapital
plugins: mock-3.1.1
collected 4 items
tests/test_account.py . [ 25%]
tests/test_pair.py .. [ 75%]
tests/helpers/test_number_helpers.py . [100%]
=============================================================================================================================== warnings summary ===============================================================================================================================
/Users/.../.local/share/virtualenvs/kittycapital-UOiJxZhy/lib/python3.7/site-packages/urllib3/util/selectors.py:14
/Users/.../.local/share/virtualenvs/kittycapital-UOiJxZhy/lib/python3.7/site-packages/urllib3/util/selectors.py:14: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
from collections import namedtuple, Mapping
/Users/.../.local/share/virtualenvs/kittycapital-UOiJxZhy/lib/python3.7/site-packages/urllib3/_collections.py:2
/Users/.../.local/share/virtualenvs/kittycapital-UOiJxZhy/lib/python3.7/site-packages/urllib3/_collections.py:2: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
from collections import Mapping, MutableMapping
-- Docs: https://docs.pytest.org/en/latest/warnings.html
How do I get rid of these warnings?
Either upgrade your dependencies to versions that do not trigger warnings, or put this in pytest.ini to hide that warning:
[pytest]
filterwarnings = ignore:.*Using or importing the ABCs.*is deprecated:DeprecationWarning
See https://docs.pytest.org/en/stable/warnings.html#deprecationwarning-and-pendingdeprecationwarning
Or use the --disable-warnings flag to hide all warnings:
python -m pytest --disable-warnings
See https://docs.pytest.org/en/stable/warnings.html#disabling-warnings-summary

No tests collected by pytest

I have problem in importing pytest while writing a python code. "import pytest is grayed out.
Python is 3.8.3, Pycharm community edition.
pytest version 5.4.2, is successfully installed and can be seen in the project interpreter in pycharm. As well as I can see the installed path of pytest in python directory.
When running py.test command from console. It starts the test run shows "collected 0 items" and lastly ends with "NO TESTS RAN IN 0.05s"
If anyone running similar problems with some other packages kindly let me know.
TIA...
You simply run pytest from the commandline. There is no need to import pytest into a script. Take this Python script as an example:
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 4
To run pytest on it, from the terminal (after changing to the right directory):
$ pytest
And you will then see the test outcome in the commandline as pytest automatically picks up the python scripts names test_*.py, where * is any name, e.g. test_increment.py. To have a test from your Python script run, name it with test_ as well to begin with.
Running pytest in the terminal is an option. In addition, Pycharm has integrated test suite for automatic discovery and collection of test tasks. You can use hotkey ctrl+shift+10 to run the test tasks directly in current file .

Why does nosetests say --with-coverage is not an option?

I am using nosetests to automatically discover and run my unittests. I would also like to have it generate coverage reports.
When I run nosetests with the following command everything works just fine
nosetests .
I looked up online that to generate the coverage, nosetests has a command line argument --with-coverage. I also double checked that this command exists using nosetests --help. However, whenever I run the following command I get the following output
nosetests --with-coverage .
Usage: nosetests [options]
nosetests: error: no such option: --with-coverage
I double checked that the coverage plugin is installed by running
nosetests --plugins
coverage shows up in the list along with a bunch of other plugins.
I also know I have coverage installed because I can manually run the coverage data collection using something along the lines of:
coverage run test.py
Am I misusing the --with-coverage option? Or is there something else I am missing?
Thanks in advance.
I never got the command line options working. I did what Janne Karila suggested and created a setup.cfg file in my projects main directory. Once I had that file I could just run nosetests with no arguments and everything would run.
One problem I had when trying to create my document was that I couldn't figure out what parameters were allowed in the config. But it turns out that any of the commands listed here https://nose.readthedocs.org/en/latest/usage.html#options can be used. Just leave off the double dashes before the command.
For reference my current config file is
[nosetests]
verbosity=1
detailed-errors=1
with-coverage=1
cover-erase=1
cover-package=application
cover-html=1
cover-html-dir=htmlcov
where=tests
This config file says to use coverage, to erase the previous run's coverage, to only report on the files in the application package, and to output an html report to the htmlcov directory.
Hopefully this will help someone else in the future.
Your syntax is correct. It maybe an issue with your environment, double check your python environment and where your have nose and coverage installed. As a sanity check, you can quickly setup a new virtualenv, install nose, and run the command with the coverage option.
As of nose 1.3.7 - the most recent version available on Pypy - that command doesn't exist:
https://github.com/nose-devs/nose/blob/release_1.3.7/nose/plugins/cover.py
It looks like the documentation is being generated from the master branch of the project that does have those options available:
https://github.com/nose-devs/nose/blob/master/nose/plugins/cover.py
What you can do is install nose from the master branch like this:
pip install git+https://github.com/nose-devs/nose#master --upgrade
It will say it just installed 1.3.7 but that's only because the version hasn't been bumped in the project setup.py file yet: https://github.com/nose-devs/nose/blob/master/setup.py#L4
Remember you have just installed an unreleased version of nose, there may be other bugs.

Categories

Resources