[gdal-dev] Motion: adopt RFC 72: Run tests with pytest

Jonathan Moules jonathan-lists at lightpear.com
Mon Dec 10 15:30:18 PST 2018


Hi Craig,

For my suite I've used pytest_make_parameterize_id to auto-create id's 
using the first parameterised value as the id name if that parameter is 
called "_" (just underscore - the Python convention for a throwaway 
variable). It works really well, although would need some work for 
backwards compatibility.

So for example your code of:

@pytest.mark.parametrize('epsg_code,epsg_broken', [
     [2758, False],  # tmerc
     [2036, True],  # sterea   # failure caused by revert done in r22803
     [2046, False],  # tmerc

Which has the test "purpose" as a comment (how I used to do it), becomes:

@pytest.mark.parametrize('_, epsg_code,epsg_broken', [
     ["tmerc", 2758, False],
     ["sterea", 2036, True],   # failure caused by revert done in r22803
     ["tmerc - 2nd," 2046, False],

And so on. Then if the test fails you see the (hopefully meaningful) 
"sterea----". The advantage is that the id is there with the test rather 
than trailing it in a separate list that's just asking to suffer from 
bitrot.

----

If you're interested, the code is simple and basically this:

def pytest_make_parametrize_id(argname, val):
     """ Create a nice readable test id based on "_".

     Doesn't quote as expected and stop at the id alone. Instead it 
keeps going through the rest of the args.

     Also keeps track of test numbers so PyCharm/PyTest doesn't change 
their order to semi-alphabetical

     :param argname: parameterised arg name
     :param val: parameterised value name
     :return: string suitable for a test name
     """
     # We keep a test_num globally so it works across tests; not yet 
investigated a better way of know when we're on the next test
     global test_num

     if argname == '_':
         test_num += 1

         # Replace spaces with underscores so that we can run the given 
test individually.
         # Can't do that with spaces in test names.
         name = "{id}={name}".format(id=test_num, name=val.replace(' ', 
'_'))

         return name
     else:
         # We don't want any value for any other field.
         # Can't leave it as an empty string because then PyTest ignores it.
         return "-"

Just a thought.

Jonathan



On 2018-12-10 03:04, Craig de Stigter wrote:
> Jonathan
>
> > it's worth spending a little thought on coming up with a scheme for 
> test-ids.
>
> I've been through the list of parametrized tests and tweaked the `ids` 
> kwargs to make them a little more helpful at first glance: 
> https://github.com/OSGeo/gdal/pull/963/commits/8db599e7bc08b7dc73d81591898ed0f5f4243a58
>
> I didn't see any way to use `pytest_make_parametrize_id` really; IDs 
> rightly vary enough between tests that I can't see that hook being 
> very useful here.
>
> Cheers
> Craig de Stigter
>
> On Mon, 10 Dec 2018 at 09:59 jratike80 
> <jukka.rahkonen at maanmittauslaitos.fi 
> <mailto:jukka.rahkonen at maanmittauslaitos.fi>> wrote:
>
>     +0
>
>     -Jukka Rahkonen-
>
>
>     Even Rouault-2 wrote
>     > PSC members,
>     >
>     > gentle reminder to cast your vote on this.
>     >
>     > Thanks,
>     >
>     > Even
>
>
>
>
>
>     --
>     Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
>     _______________________________________________
>     gdal-dev mailing list
>     gdal-dev at lists.osgeo.org <mailto:gdal-dev at lists.osgeo.org>
>     https://lists.osgeo.org/mailman/listinfo/gdal-dev
>
>
>
> _______________________________________________
> 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/20181210/e47db284/attachment.html>


More information about the gdal-dev mailing list