[gdal-dev] Intermittent failure with Python mocks

Sean Gillies sean.gillies at gmail.com
Thu Apr 13 07:13:33 PDT 2023


Hi Jon,

The number one gotcha with patching is documented here
https://docs.python.org/3/library/unittest.mock.html#where-to-patch. In a
nutshell, you have to patch your own module. I see you're patching gdal in
your tests. Patching gdal from your test doesn't do anything, it's too
late.

Hope this helps!

On Thu, Apr 13, 2023, 7:53 AM Jon Morris <Jon.Morris at jbarisk.com> wrote:

> I’m getting intermittent fails with our Python unit tests and I wondered
> if anyone has seen the same issue. I think it’s a race condition where a
> mock patch is either not applied at all or maybe applied too slowly so the
> patched function still has its original return value. It might be a pytest
> issue, but I’ve now seen it with two different GDAL methods so I’m just
> curious if anyone has any idea what may be going on.
>
>
>
> The first example I reported on Stack Overflow (
> https://stackoverflow.com/q/75185263/3182496) and looks something like
> this:
>
>
>
>         mock_feat_defn = mocker.patch.object(ogr.FeatureDefn,
> 'GetFieldCount')
>
>         mock_feat_defn.side_effect = RuntimeError("Some other error
> message")
>
>
>
>         with pytest.raises(RuntimeError, match="Some other error message"):
>
>             get_from_layer_schema(test_lyr, get_names=True,
> get_types=False)
>
>
>
> The second example is this:
>
>
>
>         with mock.patch('osgeo.gdal.Driver.Delete', side_effect=None):
>
>             with self.assertRaisesRegex(DatasetError, "Could not delete"):
>
>                 delete_path(shp_path)
>
>
>
> In both cases we’re mocking a GDAL method call to check one of our
> functions correctly raises an error, but sometimes it works and sometimes
> it doesn’t. If it’s not a GDAL issue, no problem, I will keep looking
> elsewhere for a solution.
>
>
>
> Jon
> e: ​ *Jon.Morris at jbarisk.com* <Jon.Morris at jbarisk.com>
> d: +44 (0)1756 587229
> t:  *+44 (0)1756 799919* <+44%20(0)1756%20799919>
> *www.jbarisk.com* <http://www.jbarisk.com/>
> [image: Facebook] <https://www.facebook.com/TheFloodPeople>
> [image: LinkedIn] <https://www.linkedin.com/company/jba-risk-management/>
> [image: Twitter] <https://twitter.com/JBARisk>
> [image: YouTube]
> <https://www.youtube.com/channel/UC0iatom2jYbW96voW0rlpCw>
>
>
> All JBA Risk Management's email messages contain confidential information
> and are intended only for the individual(s) named. If you are not the named
> addressee you should not disseminate, distribute or copy this e-mail.
> Please notify the sender immediately by email if you have received this
> email by mistake and delete this email from your system.
> JBA Risk Management Limited is registered in England, company number
> 07732946, 1 Broughton Park, Old Lane North, Broughton, Skipton, North
> Yorkshire, BD23 3FD, England.
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/5ee7c98c/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image978682.png
Type: image/png
Size: 9321 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/5ee7c98c/attachment-0006.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image945321.png
Type: image/png
Size: 651 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/5ee7c98c/attachment-0007.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image904217.png
Type: image/png
Size: 715 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/5ee7c98c/attachment-0008.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image622653.png
Type: image/png
Size: 966 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/5ee7c98c/attachment-0009.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image221773.png
Type: image/png
Size: 784 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/5ee7c98c/attachment-0010.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image978682.png
Type: image/png
Size: 9321 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20230413/5ee7c98c/attachment-0011.png>


More information about the gdal-dev mailing list