Closed mkinney closed 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.
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 =================================================================
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?
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.
(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
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
oh hang on..
% which pytest
/usr/local/bin/pytest
brew
may have helped me...
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 ========
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)
Closing for now, feel free to follow up with further questions.
Got it working.
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:
I expect a green pytest 1/1 success.
Instead I get this error:
I created a simple repo, if you just want to clone it and validate: https://github.com/mkinney/pytest-qt-example