Skip to content

Commit

Permalink
Docs for 5195 from main repository (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Peter Sobolewski <[email protected]>
Co-authored-by: Melissa Weber Mendonça <[email protected]>
  • Loading branch information
3 people authored Nov 2, 2022
1 parent 48443c7 commit ac24a78
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
23 changes: 22 additions & 1 deletion docs/plugins/debug_plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ When developing plugins in napari, you may encounter mistakes or bugs in your co
4. Reloading code during plugin development.
5. Isolating issues from napari.
6. Logging and debug messages.
7. Debugging segfaults/memory violation errors

## Debugging plugin start-up issues

Expand Down Expand Up @@ -287,4 +288,24 @@ DEBUG: 20/09/2022 05:59:23 PM The input string was (logging): fast
'You entered fast!'
```

The full code changes and new files after applying the changes to the plugin in each step of the examples are [here](https://github.com/seankmartin/napari-plugin-debug/tree/full_code/napari-simple-reload).

The full code changes and new files after applying the changes to the plugin in each step of the examples are [here](https://github.com/seankmartin/napari-plugin-debug/tree/full_code/napari-simple-reload).

## Debugging segfaults/memory violation errors

If napari crashes with a segfault or memory violation error when using your plugin
it may be connected with setting some viewer/layers properties outside main thread.
Because of the limitations of the Qt library, such interactions with napari may lead to a crash.

To test if this is the case, you can use the `NAPARI_ENSURE_PLUGIN_MAIN_THREAD` environment variable to help debug the issue.

Set the environement variable: `NAPARI_ENSURE_PLUGIN_MAIN_THREAD=1`, then start napari and run your plugin.

```bash
NAPARI_ENSURE_PLUGIN_MAIN_THREAD=1 napari
```

Next, start using your plugin and observe if
`RuntimeError("Setting attributes on a napari object is only allowed from the main Qt thread.")`
occurred. If so, then you need to make sure that all of your plugin code that interacts with napari structures is executed
in the main thread. For more details you could read the [multithreading](https://napari.org/stable/guides/threading.html) section of the documentation.
11 changes: 7 additions & 4 deletions docs/plugins/test_deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ We recommend using
[pytest](https://docs.pytest.org/en/6.2.x/getting-started.html) for testing your
plugin. Aim for [100% test coverage](./best_practices.md#how-to-check-test-coverage)!

### The `make_napari_viewer` fixture
### The `make_napari_viewer_proxy` fixture

Testing a napari `Viewer` requires some setup and teardown each time. We have
created a [pytest fixture](https://docs.pytest.org/en/6.2.x/fixture.html) called
`make_napari_viewer` that you can use (this requires that you have napari
`make_napari_viewer_proxy` that you can use (this requires that you have napari
installed in your environment).

To use a fixture in pytest, you simply include the name of the fixture in the
test parameters (oddly enough, you don't need to import it!). For example, to
create a napari viewer for testing:

```
def test_something_with_a_viewer(make_napari_viewer):
viewer = make_napari_viewer()
def test_something_with_a_viewer(make_napari_viewer_proxy):
viewer = make_napari_viewer_proxy()
... # carry on with your test
```

If you embed the viewer in your own application and need to access private attributes,
you can use the `make_napari_viewer` fixture.

### Prefer smaller unit tests when possible

The most common issue people run into when designing tests for napari plugins is
Expand Down

0 comments on commit ac24a78

Please sign in to comment.