deathbeds / importnb

notebook files as source
https://importnb.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
67 stars 5 forks source link

importnb causes pytest to run tests twice #160

Open ddouglas87 opened 5 days ago

ddouglas87 commented 5 days ago

When I pip install importnb individual pytests run twice.

This is quite weird behavior, because importing importnb isn't required. All you have to do is pip install it and tests start running twice. When uninstalling importnb the issue goes away.

To reproduce:

cd /tmp/
mkdir project
cd project/
python3 -m venv .venv
source .venv/bin/activate
pip install pytest
mkdir tests
cd tests/
vim test.py

def test_empty():
    ...

pytest test.py -k test_empty
================================================ test session starts =================================================
platform linux -- Python 3.11.10, pytest-8.3.3, pluggy-1.5.0
rootdir: /tmp/project/tests
collected 1 item                                                                                                     

test.py .                                                                                                      [100%]

================================================= 1 passed in 0.00s ==================================================

pip install importnb

pytest test.py -k test_empty
================================================ test session starts =================================================
platform linux -- Python 3.11.10, pytest-8.3.3, pluggy-1.5.0
rootdir: /tmp/project/tests
plugins: importnb-2023.11.1
collected 2 items                                                                                                    

test.py ..                                                                                                     [ 50%]

================================================= 2 passed in 0.01s ==================================================

Note: I've tested this with Python 3.11, 3.12, and 3.13. I've also tested it with pytest 8.3 and 8.2. This is reproducible both in PyCharm and in the command line.

Also, it doesn't work with the current version from github:

pytest test.py -k test_empty
================================================ test session starts =================================================
platform linux -- Python 3.11.10, pytest-8.3.3, pluggy-1.5.0
rootdir: /tmp/project/tests
plugins: importnb-2023.11.2.dev11+ga633de3
collected 2 items                                                                                                    

test.py ..                                                                                                     [ 50%]

================================================= 2 passed in 0.01s ==================================================
ddouglas87 commented 4 days ago

A temporary fix for this problem is to disable the importnb pytest plugin until it is fixed. This comment explains how. I chose to add the setting into my pyproject.toml file at the root of my project directory. Here's what my pyproject.toml file looks like:

[tool.pytest.ini_options]
addopts = [
    "--capture=no",
    "--log-cli-level=10",
    "-p no:importnb",
]

The capture=no and log level are so print statements can be seen while running a test. The "-p no:importnb", line fixes the issue.

tonyfast commented 1 day ago

thanks for trying importnb and raising this issue. i think a solution is make testing notebook with importnb not the default and then dealing with the duplicate tests.

as a user, i feel like you would want to opt into the testing. is this what you'd expect?

Also, it doesn't work with the current version from github:

are you saying the current code isn't discovering the tests.