pytest-dev / pytest-qt

pytest plugin for Qt (PyQt5/PyQt6 and PySide2/PySide6) application testing
https://pytest-qt.readthedocs.io
MIT License
409 stars 70 forks source link

simple example does not seem to work #405

Closed mkinney closed 2 years ago

mkinney commented 2 years ago

I cannot get the simplest example to work. It is probably me not understanding what to import.

I get E ModuleNotFoundError: No module named 'pytestqt'.

Here's the steps I did:

  1. created a python virtual environment: python3 -m venv venv
  2. activated the environment: source venv/bin/activate
  3. pip install PySide6 pytest pytest-qt
  4. created a test_hello.py (copied from doc)
from pytestqt import qtbot

def test_hello(qtbot):
    widget = HelloWidget()
    qtbot.addWidget(widget)

    # click in the Greet button and make sure it updates the appropriate label
    qtbot.mouseClick(widget.button_greet, qt_api.QtCore.Qt.MouseButton.LeftButton)

    assert widget.greet_label.text() == "Hello!"
  1. Run pytest: pytest

I expect a green pytest 1/1 success.

Instead I get this error:

% pytest
================================================================ test session starts =================================================================
platform darwin -- Python 3.9.9, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /nix/code/pytest-qt-example
plugins: cov-3.0.0
collected 0 items / 1 error

======================================================================= ERRORS =======================================================================
___________________________________________________________ ERROR collecting test_hello.py ___________________________________________________________
ImportError while importing test module '/nix/code/pytest-qt-example/test_hello.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_hello.py:1: in <module>
    from pytestqt import qtbot
E   ModuleNotFoundError: No module named 'pytestqt'
============================================================== short test summary info ===============================================================
ERROR test_hello.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================== 1 error in 0.08s ==================================================================

I created a simple repo, if you just want to clone it and validate: https://github.com/mkinney/pytest-qt-example

nicoddemus commented 2 years ago

Hi,

You don't need to import qtbot, it is used as a fixture. Just remove the from pytestqt import qtbot line from your example.

(copied from doc)

Where did you copy that from? If that import is in the official docs, we need to fix it.

mkinney commented 2 years ago

Removing that line I get:

% pytest
================================================================ test session starts =================================================================
platform darwin -- Python 3.9.9, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /nix/code/pytest-qt-example
plugins: cov-3.0.0
collected 1 item

test_hello.py E                                                                                                                                [100%]

======================================================================= ERRORS =======================================================================
____________________________________________________________ ERROR at setup of test_hello ____________________________________________________________
file /nix/code/pytest-qt-example/test_hello.py, line 1
  def test_hello(qtbot):
E       fixture 'qtbot' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, monkeypatch, no_cover, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

/nix/code/pytest-qt-example/test_hello.py:1
============================================================== short test summary info ===============================================================
ERROR test_hello.py::test_hello
================================================================== 1 error in 0.02s =================================================================
mkinney commented 2 years ago

I added the import in the initial issue above. That line is not in the doc. Sorry for that mis-information. I assumed the fixture would come from that import.

Do I need a minimal conftest.py file or something?

nicoddemus commented 2 years ago

Do I need a minimal conftest.py file or something?

No, that's enough.

Seems like your environment is missing pytest-qt, see:

plugins: cov-3.0.0

You should be seeing qt-4.0.2 in there as well.

Execute this:

$ which python
$ pip list

This should show which Python you are executing, and also which packages are installed in that environment.

mkinney commented 2 years ago
(venv) sweet pytest-qt-example % which python
/nix/code/pytest-qt-example/venv/bin/python
(venv) sweet pytest-qt-example % pip list
Package    Version
---------- -------
attrs      21.4.0
iniconfig  1.1.1
packaging  21.3
pip        22.0.2
pluggy     1.0.0
py         1.11.0
pyparsing  3.0.7
PySide6    6.2.3
pytest     6.2.5
pytest-qt  4.0.2
setuptools 59.0.1
shiboken6  6.2.3
toml       0.10.2
mkinney commented 2 years ago
Python 3.9.9
(venv) sweet pytest-qt-example % uname -a
Darwin sweet 21.1.0 Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:23 PDT 2021; root:xnu-8019.41.5~1/RELEASE_X86_64 x86_64
mkinney commented 2 years ago

oh hang on..

% which pytest
/usr/local/bin/pytest

brew may have helped me...

mkinney commented 2 years ago
sweet pytest-qt-example % sudo chmod -x /usr/local/bin/pytest
Password:

sweet pytest-qt-example % act
(venv) sweet pytest-qt-example % pytest
================================================================ test session starts =================================================================
platform darwin -- Python 3.9.9, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
PySide6 6.2.3 -- Qt runtime 6.2.3 -- Qt compiled 6.2.3
rootdir: /nix/code/pytest-qt-example
plugins: qt-4.0.2
collected 1 item

test_hello.py F                                                                                                                                [100%]

====================================================================== FAILURES ======================================================================
_____________________________________________________________________ test_hello _____________________________________________________________________

qtbot = <pytestqt.qtbot.QtBot object at 0x10f984250>

    def test_hello(qtbot):
>       widget = HelloWidget()
E       NameError: name 'HelloWidget' is not defined

test_hello.py:3: NameError
============================================================== short test summary info ===============================================================
FAILED test_hello.py::test_hello - NameError: name 'HelloWidget' is not defined
================================================================= 1 failed in 0.29s ========
nicoddemus commented 2 years ago

Ahh OK so the problem was environmental as said.

The test now fails because there's no "HelloWidget" widget, that was just an example. If you want something that runs without any failures, you can implement a HelloWidget yourself, or just run a really minimal example:

from PySide6.QtWidgets import QWidget

def test_hello(qtbot):
    widget = QWidget()
    qtbot.addWidget(widget)
nicoddemus commented 2 years ago

Closing for now, feel free to follow up with further questions.

mkinney commented 2 years ago

Got it working.

https://github.com/mkinney/pytest-qt-example