napari / cookiecutter-napari-plugin

Cookiecutter for napari plugins
BSD 3-Clause "New" or "Revised" License
67 stars 39 forks source link

Errors when testing widget #105

Closed schmiedc closed 2 years ago

schmiedc commented 2 years ago

I played around with testing and went from what was present in the cookiecutter as an example to test widgets. But noticed that I get errors. When applying pytest on a freshly setup cookiecutter I get the following errors:

`pytest . ==================================== test session starts ===================================== platform linux -- Python 3.9.10, pytest-7.1.0, pluggy-1.0.0 rootdir: /home/christopher.schmied/HT_Docs/Projects/2022-03_Hackaton/cookie_cutter/test_cookie/src/test_cookie/_tests plugins: napari-plugin-engine-0.2.0, napari-0.4.15, cov-3.0.0 collected 8 items

test_dock_widget.py EE [ 25%] test_reader.py .. [ 50%] test_sample_data.py . [ 62%] test_widget.py EE [ 87%] test_writer.py . [100%]

=========================================== ERRORS =========================================== __ ERROR at setup of test_example_q_widget ___ file /home/christopher.schmied/HT_Docs/Projects/2022-03_Hackaton/cookie_cutter/test_cookie/src/test_cookie/_tests/test_dock_widget.py, line 6 def test_example_q_widget(make_napari_viewer, capsys): file /home/christopher.schmied/.conda/envs/napari-env/lib/python3.9/site-packages/napari/utils/_testsupport.py, line 103 @pytest.fixture def make_napari_viewer( E fixture 'qtbot' not found

  available fixtures: MouseEvent, add_implementation, add_specification, cache, caller_from_implementation, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, make_napari_viewer, monkeypatch, napari_plugin_manager, no_cover, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, temporary_hookimpl, test_plugin_manager, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
  use 'pytest --fixtures [testpath]' for help on them.

/home/christopher.schmied/.conda/envs/napari-env/lib/python3.9/site-packages/napari/utils/_testsupport.py:103 ____ ERROR at setup of test_example_magicwidget ____ file /home/christopher.schmied/HT_Docs/Projects/2022-03_Hackaton/cookie_cutter/test_cookie/src/test_cookie/_tests/test_dock_widget.py, line 21 def test_example_magic_widget(make_napari_viewer, capsys): file /home/christopher.schmied/.conda/envs/napari-env/lib/python3.9/site-packages/napari/utils/_testsupport.py, line 103 @pytest.fixture def make_napari_viewer( E fixture 'qtbot' not found

  available fixtures: MouseEvent, add_implementation, add_specification, cache, caller_from_implementation, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, make_napari_viewer, monkeypatch, napari_plugin_manager, no_cover, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, temporary_hookimpl, test_plugin_manager, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
  use 'pytest --fixtures [testpath]' for help on them.

/home/christopher.schmied/.conda/envs/napari-env/lib/python3.9/site-packages/napari/utils/_testsupport.py:103 __ ERROR at setup of test_example_q_widget ___ file /home/christopher.schmied/HT_Docs/Projects/2022-03_Hackaton/cookie_cutter/test_cookie/src/test_cookie/_tests/test_widget.py, line 6 def test_example_q_widget(make_napari_viewer, capsys): file /home/christopher.schmied/.conda/envs/napari-env/lib/python3.9/site-packages/napari/utils/_testsupport.py, line 103 @pytest.fixture def make_napari_viewer( E fixture 'qtbot' not found

  available fixtures: MouseEvent, add_implementation, add_specification, cache, caller_from_implementation, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, make_napari_viewer, monkeypatch, napari_plugin_manager, no_cover, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, temporary_hookimpl, test_plugin_manager, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
  use 'pytest --fixtures [testpath]' for help on them.

/home/christopher.schmied/.conda/envs/napari-env/lib/python3.9/site-packages/napari/utils/_testsupport.py:103 ____ ERROR at setup of test_example_magicwidget ____ file /home/christopher.schmied/HT_Docs/Projects/2022-03_Hackaton/cookie_cutter/test_cookie/src/test_cookie/_tests/test_widget.py, line 21 def test_example_magic_widget(make_napari_viewer, capsys): file /home/christopher.schmied/.conda/envs/napari-env/lib/python3.9/site-packages/napari/utils/_testsupport.py, line 103 @pytest.fixture def make_napari_viewer( E fixture 'qtbot' not found

  available fixtures: MouseEvent, add_implementation, add_specification, cache, caller_from_implementation, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, make_napari_viewer, monkeypatch, napari_plugin_manager, no_cover, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, temporary_hookimpl, test_plugin_manager, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
  use 'pytest --fixtures [testpath]' for help on them.

/home/christopher.schmied/.conda/envs/napari-env/lib/python3.9/site-packages/napari/utils/_testsupport.py:103 ================================== short test summary info =================================== ERROR test_dock_widget.py::test_example_q_widget ERROR test_dock_widget.py::test_example_magic_widget ERROR test_widget.py::test_example_q_widget ERROR test_widget.py::test_example_magic_widget ================================ 4 passed, 4 errors in 0.09s =================================`

tlambert03 commented 2 years ago

Thanks for the issue and sorry about that @schmiedc...

I opened https://github.com/napari/cookiecutter-napari-plugin/pull/106 to fix this for future cookiecutter users.
To run tests with pytest locally you'll need to have a couple more dependencies installed. run pip install pytest-qt then try again. If that doesn't do it, let me know

cnstt commented 2 years ago

I just experienced the same problem, qtbot is missing when running tests! (with the latest version of the template). Why is pytest-qt not in the testing requirements?

tlambert03 commented 2 years ago

I just experienced the same problem, qtbot is missing when running tests! (with the latest version of the template). Why is pytest-qt not in the testing requirements?

It is ... but only if you said "yes" to "include widget plugin": https://github.com/napari/cookiecutter-napari-plugin/blob/96891d3348d2744dbfd38fb0b140914597fec094/%7B%7Bcookiecutter.plugin_name%7D%7D/setup.cfg#L80

otherwise... there's no immediate need for it, and if you later add a widget manually, then you'll have to add it. (not all plugins need qt, and it's a big thing to add if the plugin isn't using it)

cnstt commented 2 years ago

Oh I see, thanks for the explanation!