pkw,
@pkw@mastodon.sdf.org avatar

#python fixture config is magic and I don't like it.

def test_something(fixture):
...

So in pytest. What this does is get the name of the param fixture to see if it matches the name of a previously defined fixture function. If you don't know that it looks bizarre. That IS NOT a parameter passed into a function but a sentinel that is used to look up a fixture by it's parameter name.

WHY not just pass in the ACTUAL FIXTURE ?!?!

def test_something(fixtures=[fixture1, fixture2]):
...

SnoopJ,
@SnoopJ@hachyderm.io avatar

@pkw if you would prefer to write tests in the more explicit unittest style where you do all the setup yourself, pytest supports running tests so-written. This may be more acceptable if you find the pytest style offensive.

Other replies have already covered why pytest chose this style.

pkw,
@pkw@mastodon.sdf.org avatar

@SnoopJ

I will reply with what i already said ?:

"Also it works, I underastand the mechanics of it, and I'm making peace with it. I have to use pytest."

Also I will take the python tag off the post. Maybe that is making people think I am looking to debate instead of having an opinion and ruminate on language design.

SnoopJ,
@SnoopJ@hachyderm.io avatar

@pkw it does read as very critical of a piece of software that is new to you, but I am personally not interested in arguing about it. Cannot speak for others.

pkw,
@pkw@mastodon.sdf.org avatar

@SnoopJ I would delete the post, but i appreciate @meejah perspective and thought it would be rude to delete it considering that.

meejah,
@meejah@mastodon.social avatar

@pkw in the end it does pass in a value (whatever the fixture function returns).

There is definitely some magic involved, though -- at least it gives an error if you misname one. (On balance, I prefer the less typing -- you can try "testtools" for one that has more-explicit fixtures)

pkw,
@pkw@mastodon.sdf.org avatar

@meejah It even says that in the docs that it uses the name to lookup the fixture.

https://docs.pytest.org/en/7.1.x/how-to/fixtures.html#requesting-fixtures

Pytest probably inspects that test function (before calling it) and does the lookup for fixtures, AND THEN calls that function passing in the right fixture. But still when you write the test function that parameter is all about the name which is not something you do in regular python programming.

I'm just venting because the NAME OF A PARAMETER SHOULD NOT HAVE SIDE EFFECTS.

pkw,
@pkw@mastodon.sdf.org avatar

@meejah Also it works, I underastand the mechanics of it, and I'm making peace with it. I have to use pytest.

meejah,
@meejah@mastodon.social avatar

@pkw It can definitely feel a little weird ... but it's hard to get some of the other very-useful fixture features (module/session scoping, parametrization, ..) with other mechanisms. It's also nice and short (although of course you need to "know" pytest to read the code)

The command-line library Click also does something a little similar with the option decorators (i.e. "magically" maps names)

pkw,
@pkw@mastodon.sdf.org avatar

@meejah Thanks!, I am seeing that it is adds convenience for pytest, I still don''t like it.

Sorry to be pedantic, but I use click, and this isn't like click. Click doesn't use parameters solely for the side effect of their name. All of the parameters in click have actual values.

This is different but more in spirit similar to Flask and how it sets the request variable automatically inside functions. But even that is more palatable because it's doesn't bleed out of the function definition.

meejah,
@meejah@mastodon.social avatar

@pkw The similarity I see is that Click "magically" uses the option-name to pass the value to the correct arg; pytest "magically" uses the arg-name to call the correct fixture (it's not just for the side-effect: it passes the value returned into your function). Click also depends on "side effects" of the decorators...

Certainly I still agree that pytest's is "more magic", but they're both about convenience (e.g. you could implement Click's to be strictly explicit passing too)

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • magazineikmin
  • rosin
  • Youngstown
  • ngwrru68w68
  • khanakhh
  • slotface
  • InstantRegret
  • mdbf
  • GTA5RPClips
  • kavyap
  • thenastyranch
  • DreamBathrooms
  • everett
  • tacticalgear
  • JUstTest
  • tester
  • Durango
  • cubers
  • ethstaker
  • cisconetworking
  • modclub
  • osvaldo12
  • Leos
  • normalnudes
  • megavids
  • provamag3
  • anitta
  • lostlight
  • All magazines