[TIP] configure a pytest fixture

oliver oliver.schoenborn at gmail.com
Wed Nov 16 04:54:07 PST 2016


On Wed, Nov 16, 2016, 00:23 Florian Bruhin <me at the-compiler.org> wrote:

> Hi,
>
> * oliver <oliver.schoenborn at gmail.com> [2016-11-16 02:32:04 +0000]:
> > Thanks for the reply Bruno. I've used something like that in some
> fixtures
> > (actually returning a closure / lambda which, when called from the test,
> > returns a configured object), but it seems a little counter-intuitive
> (the
> > premise being that fixtures return objects ready to be used). How about
> > overloading the item operator [], so "def test(fixture[a,b])" would be
> > implemented in a similar way to annotations like "Map[str, int]" but
> > fixture[a,b] would allow the fixture function to receive a, b as
> > parameters.
>
> That's invalid Python syntax - fixture is an argument name here, not
> an object.
>

Ah yes, rats! Then how about via a default value, this could easily be
introspected by pytest:

def test(fixture=a): would call fixture(a)
def test(fixture=[a,b]): would call fixture(a, b)



> If your fixture argument is static, another possibility which is used
> sometimes is to use a marker on the test, and then getting that marker
> from the fixture function via the 'request' object:
>
> http://docs.pytest.org/en/latest/fixture.html#fixtures-can-introspect-the-requesting-test-context


Way too much boiler-plate code needed, and the intent is obfuscated by
piggybacking on a feature that has completely different purpose.

Oliver
-- 
Oliver
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20161116/28dfc61c/attachment.html>


More information about the testing-in-python mailing list