Skip to content

Commit

Permalink
Simple GUI test for CI (#223)
Browse files Browse the repository at this point in the history
Enable GUI tests in CLI and add a simple GUI test.
  • Loading branch information
GenevieveBuckley authored and anwai98 committed Oct 25, 2023
1 parent 6ad6c2b commit 925b1fa
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
16 changes: 13 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
test:
name: ${{ matrix.os }} ${{ matrix.python-version }}
runs-on: ${{ matrix.os }}

timeout-minutes: 60
strategy:
fail-fast: false
matrix:
Expand All @@ -31,19 +31,29 @@ jobs:
with:
environment-file: environment_cpu.yaml

# Setup Qt libraries for GUI testing
- uses: tlambert03/setup-qt-libs@v1

- name: Install napari and pyqt for GUI tests
shell: bash -l {0}
run: pip install "napari[all]"

- name: Install pytest
shell: bash -l {0}
run: |
python -m pip install pytest
python -m pip install pytest-cov
python -m pip install pytest-qt
- name: Install package
shell: bash -l {0}
run: pip install --no-deps -e .

- name: Run tests
shell: bash -l {0}
run: pytest
uses: aganders3/headless-gui@v1
with:
shell: bash -l {0}
run: pytest

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
Expand Down
1 change: 1 addition & 0 deletions micro_sam/visualization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Functionality for visualizing image embeddings.
"""
from typing import Tuple

from typing import Tuple

Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ requires = ["setuptools>=42.0.0", "wheel"]
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
addopts = "-v --cov=micro_sam --cov-report xml:coverage.xml --cov-report term"
addopts = "-v --durations=10 --cov=micro_sam --cov-report xml:coverage.xml"
markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
"gui: marks GUI tests (deselect with '-m \"not gui\"')",
]

[tool.black]
line-length = 79
Expand Down
48 changes: 48 additions & 0 deletions test/test_gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import numpy as np
import pytest

from micro_sam.sam_annotator import annotator_2d
from micro_sam.sam_annotator.annotator_2d import _initialize_viewer


def _check_layer_initialization(viewer):
"""Utility function to check the initial layer setup is correct."""
assert len(viewer.layers) == 6
expected_layer_names = ['raw', 'auto_segmentation', 'committed_objects', 'current_object', 'point_prompts', 'prompts']
for layername in expected_layer_names:
assert layername in viewer.layers
# Check layers are empty before beginning tests
np.testing.assert_equal(viewer.layers["auto_segmentation"].data, 0)
np.testing.assert_equal(viewer.layers["current_object"].data, 0)
np.testing.assert_equal(viewer.layers["committed_objects"].data, 0)
np.testing.assert_equal(viewer.layers["point_prompts"].data, 0)
assert viewer.layers["prompts"].data == [] # shape data is list, not numpy array


@pytest.mark.gui
def test_annotator_2d(make_napari_viewer_proxy, tmp_path):
"""Integration test for annotator_2d widget with automatic mask generation.
* Creates 2D image embedding
* Opens annotator_2d widget in napari
"""
model_type = "vit_b"
embedding_path = tmp_path / "test-embedding.zarr"
# example data - a basic checkerboard pattern
image = np.zeros((16,16))
image[:8,:8] = 1
image[8:,8:] = 1

viewer = make_napari_viewer_proxy()
viewer = _initialize_viewer(image, None, None, None) # TODO: fix hacky workaround
# test generating image embedding, then adding micro-sam dock widgets to the GUI
viewer = annotator_2d(
image,
embedding_path,
show_embeddings=False,
model_type=model_type,
v=viewer,
return_viewer=True
)
_check_layer_initialization(viewer)
viewer.close() # must close the viewer at the end of tests

0 comments on commit 925b1fa

Please sign in to comment.