Skip to content

Commit

Permalink
Fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DragaDoncila committed Oct 4, 2023
1 parent 84714df commit 60a9ef9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
import numpy as np

from {{cookiecutter.module_name}} import ExampleQWidget, example_magic_widget
from {{cookiecutter.module_name}} import example_function_widget, example_magic_widget, ExampleQWidget




def test_example_function_widget():
# because our "widget" is a pure function, we can call it and
# test it independently of napari
im_data = np.random.random((100, 100))
thresholded = example_function_widget(im_data)
assert thresholded.shape == im_data.shape
# etc.

# make_napari_viewer is a pytest fixture that returns a napari viewer object
# you don't need to import it, as long as napari is installed
# in your testing environment
def test_example_magic_widget(make_napari_viewer):
viewer = make_napari_viewer()
layer = viewer.add_image(np.random.random((100, 100)))

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

# if we "call" this object, it'll execute our function
thresholded = my_widget(viewer.layers[0], 0.5)
assert thresholded.shape == layer.data.shape
# etc.

# 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
Expand All @@ -19,18 +43,3 @@ def test_example_q_widget(make_napari_viewer, capsys):
# 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):
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"
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
# to indicate it should be wrapped as a magicgui to autogenerate
# a widget.
def example_function_widget(
img_layer: "napari.layers.Image",
img: "napari.types.ImageData",
) -> "napari.types.LabelsData":
return img_layer.data > 0.5
return img > 0.5


# the magic_factory decorator lets us customize aspects of our widget
Expand All @@ -56,6 +56,8 @@ def example_magic_widget(
return img_layer.data > threshold


# if we want even more control over our widget, we can use
# magicgui `Container`
class ImageThreshold(Container):
def __init__(self, viewer: "napari.viewer.Viewer"):
super().__init__()
Expand Down

0 comments on commit 60a9ef9

Please sign in to comment.