Yes it does help. I’m still getting to grips with patching and see it as a last 
resort if you can’t set the test up a better way. I’ll skip the failing tests 
for now and see if I can rewrite them.

Though I guess the question now is why does it actually work most of the time? 😊

Thanks


e: [email protected]
d:+44 (0)1756 587229
t: +44 (0)1756 799919
www.jbarisk.com
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.
From: Sean Gillies <[email protected]>
Sent: Thursday, April 13, 2023 3:14 PM
To: Jon Morris <[email protected]>
Cc: gdal dev <[email protected]>
Subject: Re: [gdal-dev] Intermittent failure with Python mocks

CAUTION: This email originated from outside of JBA and contains one or more 
links and one or more attachments. DO NOT click links or open attachments 
unless you recognise the sender's email address and are absolutely certain that 
the content is safe.
See the Phishing page on IMS on SP for more information about how to spot and 
report suspicious messages.
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 
<[email protected]<mailto:[email protected]>> 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: ​
[email protected]<mailto:[email protected]>
d:
+44 (0)1756 587229
t:
+44 (0)1756 799919<tel:+44%20(0)1756%20799919>
www.jbarisk.com<http://www.jbarisk.com/>
[cid:[email protected]]
[Facebook]<https://www.facebook.com/TheFloodPeople>
[LinkedIn]<https://www.linkedin.com/company/jba-risk-management/>
[Twitter]<https://twitter.com/JBARisk>
[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
[email protected]<mailto:[email protected]>
https://lists.osgeo.org/mailman/listinfo/gdal-dev
_______________________________________________
gdal-dev mailing list
[email protected]
https://lists.osgeo.org/mailman/listinfo/gdal-dev

Reply via email to