GispoCoding / pytest-qgis

A pytest plugin for testing QGIS python plugins
GNU General Public License v2.0
29 stars 8 forks source link

QgsProcessingAlgorithm #27

Closed cefect closed 1 year ago

cefect commented 1 year ago

Sorry if this is off-base, but I'm wondering if there is any support for testing custom processing algorithms, like those described here. Some of the projects I've seen are using some very cumbersome nose tests.

Thanks for the awesome tool,

Joonalai commented 1 year ago

Thank you for the issue and great that you find the tool useful :)

You can actually test the custom processing algorithms with pytest-qgis. Here is a simple example of how that could be done:

from qgis import processing

from your.custom.algorithm import YourCustomAlgorithm
from your.custom.provider import YourCustomProcessingProvider

@pytest.fixture()
def algorithm_provider(qgis_processing):
    provider = YourCustomProcessingProvider()
    QgsApplication.processingRegistry().addProvider(provider)

    yield provider
    QgsApplication.processingRegistry().removeProvider(provider)

def test_custom_processing_algorithm(algorithm_provider, some_layer):
    result = processing.run(
        algorithm_provider.algorithm(YourCustomAlgorithm().name()),
        {
            "INPUT": some_layer,
            "OUTPUT": QgsProcessing.TEMPORARY_OUTPUT,
        },
    )
    # test the result 
    assert isinstance(result["OUTPUT"], QgsVectorLayer)
cefect commented 1 year ago

Thanks @Joonalai, this is much more elegant than what I was doing.