Skip to content

Commit

Permalink
Update test_dock_widget.py
Browse files Browse the repository at this point in the history
  • Loading branch information
RoccoDAnt committed Jan 25, 2022
1 parent ecddf85 commit 44d93fb
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions napari_zelda/_tests/test_dock_widget.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
from napari_zelda import ExampleQWidget, example_magic_widget
import numpy as np
import napari_zelda
import pytest

# make_napari_viewer is a pytest fixture that returns a napari viewer object
# capsys is a pytest fixture that captures stdout and stderr output streams
def test_example_q_widget(make_napari_viewer, capsys):
# make viewer and add an image layer using our fixture
viewer = make_napari_viewer()
viewer.add_image(np.random.random((100, 100)))

# create our widget, passing in the viewer
my_widget = ExampleQWidget(viewer)

# call our widget method
my_widget._on_click()
# this is your plugin name declared in your napari.plugins entry point
MY_PLUGIN_NAME = "napari-zelda"
# the name of your widget(s)
MY_WIDGET_NAMES = ["Example Q Widget", "example_magic_widget"]

# read captured output and check that it's as we expected
captured = capsys.readouterr()
assert captured.out == "napari has 1 layers\n"

def test_example_magic_widget(make_napari_viewer, capsys):
@pytest.mark.parametrize("widget_name", MY_WIDGET_NAMES)
def test_something_with_viewer(widget_name, make_napari_viewer, napari_plugin_manager):
napari_plugin_manager.register(napari_zelda, name=MY_PLUGIN_NAME)
viewer = make_napari_viewer()
layer = viewer.add_image(np.random.random((100, 100)))

# this time, our widget will be a MagicFactory or FunctionGui instance
my_widget = example_magic_widget()

# if we "call" this object, it'll execute our function
my_widget(viewer.layers[0])

# read captured output and check that it's as we expected
captured = capsys.readouterr()
assert captured.out == f"you have selected {layer}\n"
num_dw = len(viewer.window._dock_widgets)
viewer.window.add_plugin_dock_widget(
plugin_name=MY_PLUGIN_NAME, widget_name=widget_name
)
assert len(viewer.window._dock_widgets) == num_dw + 1

0 comments on commit 44d93fb

Please sign in to comment.