napari / docs

Documentation for napari (other than API docs)
BSD 3-Clause "New" or "Revised" License
10 stars 36 forks source link

How to test functions involving thread workers #250

Open GenevieveBuckley opened 11 months ago

GenevieveBuckley commented 11 months ago

📚 New content request

Suggest adding a small section to the guide on threading in napari to include a small demo on how to test functions involving threading.

Outline

Testing functions involving thread workers

When writing tests for your code, sometimes you want the test to wait for the thread worker to finish and return the data, so that you can make assertions about the result. For this, use theawait_workers() method.

Here is an example:

def test_average_large_image(make_napari_viewer):
    viewer = make_napari_viewer()
    worker = average_large_image()
    worker.await_workers()  # blocks until thread worker is finished computation
    result_layer = viewer.layers[0]
    assert result_layer.data.shape == (512, 512)
Czaki commented 11 months ago

I do not like this. In the past, the make_napari_vewer fixture create problems (even now it crashes https://github.com/napari/napari/pull/6340). We should propagate to write unit tests, not integration tests.

So, I prefer to directly connect signals and not use a viewer if unnecessary.