pytest: everything you need to know about fixtures (intermediate) anthony explains

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ต.ค. 2022
  • today I go over all the options and use cases for fixtures in pytest!
    - testing a commandline with pytest: • python cli tested with...
    - pytest parametrize: • pytest's parametrize (...
    - pytest usefixtures: • why pytest.mark.usefix...
    playlist: • anthony explains
    ==========
    twitch: / anthonywritescode
    dicsord: / discord
    twitter: / codewithanthony
    github: github.com/asottile
    stream github: github.com/anthonywritescode
    I won't ask for subscriptions / likes / comments in videos but it really helps the channel. If you have any suggestions or things you'd like to see please comment below!
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 46

  • @shekelboi
    @shekelboi 2 หลายเดือนก่อน +4

    The way fixtures work is just so alien to me.

    • @TheMathues123
      @TheMathues123 17 วันที่ผ่านมา

      learn decorators

  • @mfeickert07
    @mfeickert07 ปีที่แล้ว +19

    This is a great introduction which I'll be sending to my students now. :) Thanks!

  • @empty_pit7920
    @empty_pit7920 ปีที่แล้ว +2

    Great video, thanks!
    I would gladly watch a video about pytest hooks and plugins

  • @essamgouda1609
    @essamgouda1609 ปีที่แล้ว +1

    Your videos are extremely useful !!

  • @GOZES
    @GOZES ปีที่แล้ว +2

    Something like this for the mock module from the standard library will be super useful :)

    • @anthonywritescode
      @anthonywritescode  ปีที่แล้ว +5

      yep it's on the list -- hinted at in this video in fact

    • @iDontFinishAnyt
      @iDontFinishAnyt ปีที่แล้ว

      @@anthonywritescode Hopefully its coming soon! Would come super in handy.

  • @malakarakesh3139
    @malakarakesh3139 5 หลายเดือนก่อน

    great videos.
    could you explain a lit bit more on the "state" with the temp dir as an example?

  • @Quarky_
    @Quarky_ ปีที่แล้ว

    Fixtures within a scope is pretty cool, didn't know about that :)

  • @sudharsan4040
    @sudharsan4040 8 หลายเดือนก่อน +1

    One of the best pytest tutorial. Can you please put a tutorial on mocking nested pytest classes and patching them. An excellent use case like using Azure or AWS python sdk

  • @abhinchhabra250
    @abhinchhabra250 ปีที่แล้ว

    Going over indirect fixtures would be cool 😊

  • @ChimaChindaDev
    @ChimaChindaDev 2 หลายเดือนก่อน

    How do you populate default db during a pytest run.

  • @alice-smith
    @alice-smith ปีที่แล้ว

    Love the new font-size :^)

  • @thejtoken
    @thejtoken ปีที่แล้ว

    Great video, thanks!

  • @petertiggerdine2631
    @petertiggerdine2631 6 หลายเดือนก่อน

    Does the conftest inheritance work in the lasted verisons for pytest?

  • @natiachikovani7660
    @natiachikovani7660 ปีที่แล้ว +1

    Thanks a lot for the video.
    If I am good with Pytest, should I know unittest too? Is it a must or I can be OK with only Pytest?

    • @anthonywritescode
      @anthonywritescode  ปีที่แล้ว +4

      I wouldn't bother with unittest personally

    • @natiachikovani7660
      @natiachikovani7660 ปีที่แล้ว

      @@anthonywritescode Neither me! I wanted to hear exactly this ❤️

  • @aneeszuberi1690
    @aneeszuberi1690 7 หลายเดือนก่อน +1

    Hi, while testing class TestMyThing; you only pasing fix fixture to test method, but in the result it is alos printing 'setup and tear down'. How is that happening?

    • @anthonywritescode
      @anthonywritescode  7 หลายเดือนก่อน +2

      pytest hooks up fixtures automatically and it passes them as arguments

  • @malakarakesh3139
    @malakarakesh3139 5 หลายเดือนก่อน +1

    at 14:33 what's the difference between `return request.param` vs `yield request.param`?

    • @anthonywritescode
      @anthonywritescode  4 หลายเดือนก่อน +2

      in this case nothing. I tend to use yield in case I want to convert it to a context fixture later on

  • @dankelman9562
    @dankelman9562 ปีที่แล้ว

    thank you so much!

  • @moiattube
    @moiattube 7 หลายเดือนก่อน

    Nice! Thanks a lot
    For a moment I thought you had autocompletion on -k option! Wouldn't it be great to have?

    • @anthonywritescode
      @anthonywritescode  7 หลายเดือนก่อน

      it's a substring match so idk how it would work

  • @nofutureanymore
    @nofutureanymore ปีที่แล้ว

    Great video ! thx

  • @user-ph7je6hv6n
    @user-ph7je6hv6n 11 หลายเดือนก่อน

    Hello Anthony! Could you tell me how to organize work with fixtures that return entities from the database (I load them at each start of the pytest)? The whole difficulty is that these entities depend on each other and are nested in each other. In an advanced project, keeping the database up to date in tests becomes hell. Mocks also don't save much as they also need to be nested inside each other

    • @cosmicallyderived
      @cosmicallyderived 9 หลายเดือนก่อน

      It sounds like you would create composite fixtures that depend on other fixtures in a representative hierarchy that reflects the domain you’re in.

    • @user-ph7je6hv6n
      @user-ph7je6hv6n 9 หลายเดือนก่อน

      @@cosmicallyderived Please advise a tutorial on how to do it right

  • @cosmicallyderived
    @cosmicallyderived 9 หลายเดือนก่อน +1

    Didn’t know about the name kwarg for fixtures.

  • @JohnZakaria
    @JohnZakaria ปีที่แล้ว +1

    Test classes vs bare functions, when should I use classes

    • @IManu96I
      @IManu96I ปีที่แล้ว +2

      I use them when a set of different test share a common set up, variables, fixtures or topic. So that the developer can find them better while providing unique fixtures that only applied to these.
      This reduces the scope of some fixtures. If some of them are autoused, you can have better control on it for example

    • @anthonywritescode
      @anthonywritescode  ปีที่แล้ว +9

      (for me): functions: always, classes: never
      there's just too many ways classes can become unmaintainable (people trying to get too clever with test inheritance, accidentally misnaming a test class is way easier, weird accidentally stateful problems)

  • @matthewritter1117
    @matthewritter1117 8 หลายเดือนก่อน

    Thanks!

  • @Keshas1
    @Keshas1 ปีที่แล้ว

    This is good can write a fixture with a class scope

  • @duke007x3
    @duke007x3 3 หลายเดือนก่อน

    Please explain pytest hooks and plugins)

  • @hiyadmn9damy
    @hiyadmn9damy ปีที่แล้ว

    Thanks

  • @mahanirvaantantra
    @mahanirvaantantra 2 หลายเดือนก่อน

    pytest has a lot of abstractions. That's one thing I hate about pytest !

  • @yprohoda
    @yprohoda ปีที่แล้ว

    dff

  • @amir.hessam
    @amir.hessam ปีที่แล้ว

    first comment

  • @swehba
    @swehba ปีที่แล้ว

    Anthony, love your videos. I always learn something. I've been using pytest for a while, and one thing that has always confounded me is how to add type annotation to a fixture factory function that an IDE like Pycharm can pick up. For example, let's say I have the following (admittedly simplistic fixture) that uses the `faker` package to generate person names with prefixes:
    @pytest.fixture
    def create_person_name_with_prefix(faker):
    def inner(prefix: str = None, last_name: str = None) -> str:
    prefix = prefix or faker.prefix_nonbinary()
    last_name = last_name or faker.last_name()
    return f'{prefix} {last_name}'
    return inner
    When I use the fixture factory, I'd like Pycharm to be able to call up the documentation (i.e., signature with types) of the inner function. If this were a standard decorator instead of `@pytest.fixture`, I could use `wraps` from the `functools` module, but that doesn't seem to work here. Is there another way to do something similar to `wraps` with pytest fixture factories?

    • @anthonywritescode
      @anthonywritescode  ปีที่แล้ว +1

      pycharm I think added special code for pytest fixtures -- so you should be able to use standard type annotations here
      I think you need a `Protocol` (th-cam.com/video/QjFChmQHJxk/w-d-xo.html) to represent this fixture return value due to the optional args:
      ```python
      class InnerCallable(Protocol):
      def __call__(self, prefix: str | None = None, last_name: str | None = None) -> str: ...
      @pytest.fixture
      def create_person_name_with_faker(faker) -> InnerCallable:
      # actual implementation here
      ...
      ```

    • @swehba
      @swehba ปีที่แล้ว

      @@anthonywritescode Thanks for the lightning fast reply! I did as you suggested, and added type information to the outer/decorator function, and, sure enough, Pycharm sees that and displays it as documentation. However, as expected, it only displays the types, not the formal parameter names as it would with a non-decorated function. Any ideas about how to get the complete documentation (i.e. parameter names and types) to show?