-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
14 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |