getting started with pytest (beginner - intermediate) anthony explains

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ม.ค. 2023
  • I realize I've talked a lot about pytest on the channel, but never given it a proper introduction!
    - testing a cli with pytest: • python cli tested with...
    - parametrize: • pytest's parametrize (...
    - testing lru cache: • testing functools.lru_...
    - why pytest.mark.usefixtures? • why pytest.mark.usefix...
    - parametrize permutation: • pytest: parametrize pe...
    - testing exceptions: • pytest: testing except...
    - testing output: • testing output with py...
    - all pytest statuses: • pytest: xfail vs xpass...
    - testing environment variables: • pytest: testing env va...
    - conditional contexts and pytest: • what is nullcontext? (...
    - pytest: all about fixtures: • pytest: everything you...
    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!
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @tobb10001
    @tobb10001 ปีที่แล้ว +22

    8:20 pytest also prints out whatever was written to stdout whenever a test fails. So if you're debugging a failing test you can read the output even without using -s.

  • @theendlessriver13
    @theendlessriver13 ปีที่แล้ว +3

    -v{1,3} is also very helpful when tests with a more complex comparison fail, e.g. long strings, lists/tuples/dicts with many items. It shows you exactly where the difference occurred, what item is missing etc.
    It helps me a lot breaking this down

  • @Jakub1989YTb
    @Jakub1989YTb ปีที่แล้ว +13

    One of the top python packages! 🙂

    • @jacobsan
      @jacobsan 6 หลายเดือนก่อน +1

      Truly one of the python packages of all time

  • @araujoao
    @araujoao 4 หลายเดือนก่อน

    Thanks a lot for the video. Coming from other language backgrounds, it helped a lot to clarify some info. Plus, it was short and enlightening

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

    Didn't know that including __init__.py would resolve name collisions - thanks!

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

    Awesome video! can we also have a video on Mock, patching, monkey patch..etc please!

  • @varioustoxins
    @varioustoxins ปีที่แล้ว +3

    Nice video, just checked I was sure unit test wasn’t in Python 1. Python predates junit by about 7 years and unit test was inspired by junit. python
    20 February 1991. JUnit was born on a flight from Zurich to the 1997 OOPSLA in Atlanta.

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

    Thanks!

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

    Thanks for the video! Many of your repos have testing directory, what do you store there? A code that is related for tests but you keep it separate because tests directory should contain only tests? Or my guess is wrong

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

      if you search my channel I did a video on my project structure

  • @DavidDellsperger
    @DavidDellsperger ปีที่แล้ว +3

    I assume the `-s` and suppression of I/O extends to the code not only in the test file (t_test.py) but also the original file (t.py)?

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

    great video

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

    please explain the plugins for pytest!! great video!! thumbsed upppp!!!

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

    Thanks, Anthony. Can you make a video about 'indirect' in parametrize?
    And also request param in fixtures?

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

      personally I don't think anyone should use indirect

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

    flask app is dockerized. Uses rq worker, redis, postgres as compose services. How should one thing about testing such application?

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

      you mock the output from those services to test your logic

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

    TIL about -s for print debugging an incorrectly passing test. I've normally just gone with the tried and true 1 / 0 in the test if it comes to that. haha
    The plugins certainly make the tool shine brighter than it already does. Off the top of my head, pytest-randomly and pytest-repeat are two I use regularly to shake out and expose tricky pollution.

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

      And I have been using the infinitely more verbose --capture=tee-sys :-!

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

    Thanks for the video!
    It would be helpful to get a more advanced follow up video about fixtures, patches and so on :)

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

    great video, do u have any idea on best way to configure test data for different environments (dev, staging etc) in pytest?

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

      I would hope you're not running pytest in staging?

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

      @@anthonywritescode It is running in staging but its blackbox level test script, but i was just curious how would one manage test data in this situation.

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

    Great video!
    I've quickly tested some time ago unittest from python and I found out that writing assertion keyword is not strictly prohibited, maaaaybeee they changed something? I don't know. But pytest is by far most useful 3rd party tool downloadable from PIP! Thanks for sharing this tutorial!

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

      there's nothing stopping you from using plain assert, but your error messages will be useless

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

      @@anthonywritescode Oh, okay then. thanks! :)

  • @diegobonaventura8501
    @diegobonaventura8501 11 หลายเดือนก่อน

    whats with the keyboard view?

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

    Hey Anthony! Why do you not like oop? 😊 its a battle am fighting to learn or not but i checked many of your repos and didnt see you using classes, so was hoping to hear your thoughts

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

      it's terrible for large code cases and tends to encourage poor patterns. when a lot of people say "I'm doing oop" I find most of them are instead doing "I'm too lazy to import what I need so I'm using inheritance for convenience"

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

      @@anthonywritescode thank you for responding and everything you share through this channel.

  • @johnwt7333
    @johnwt7333 ปีที่แล้ว +3

    I never knew there was a name to call how you name your variables:
    camelCase
    snake_case
    PascalCase
    kebab-case

    • @yan-amar
      @yan-amar 29 วันที่ผ่านมา

      # just in case

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

    i did learn OOP way in Python just to understood pretty well unittest code examples.
    Now i just accidentally here Pytest and saw your video, while i did research on youtube how it looks like.
    And i like that pytest , it does not strictly used Class Based written test.
    I also enjoyed my learning on unittest framwork using Class Base.
    But i do believe in unittest you can do it in by writing functions instead like the Pytest. But not really good idea since the documentation does not mention something like that, i could also be wrong since i am still fairly new to writing unit test code, when i did my first python project, all i did was like intuition. No written test and it was horrible experience because it was hard to identify the bugs on the project.
    But yeah lesson learned and i better write a test whenever i write a functionality of the program.
    Your video showing pytest was pretty great, i have no idea how great is pytest in general. looks more simpler in terms of usage compare to unittest in my opinion.
    I have still to open my OOP book in Python, just to make myself not getting lost in the middle.
    Thanks for creating the video of demonstrating pytest.
    Just notice you type in terminal: " babi .py "
    what is that "babi"?
    It looks like nano text editor, i guess it is? you just make some alias for nano text editor?

  • @wwijsman
    @wwijsman ปีที่แล้ว +8

    I honestly prefer unittest. It is just nice to have it integrated in the language. Other than that I don't use it very differently from how you use pytest in this video.

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

    why do you put an underscore infront of main like _main.py, and the other files

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

      it is an attempt to tell people to not muck with the internals

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

    Why all files of pyupgrade starts with _?

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

      it's a commandline tool -- there is no intended python api -- but people still reach into the internals and muck with them nonetheless

  • @daniel-mizsak
    @daniel-mizsak ปีที่แล้ว +2

    Great video. Thank you for it!
    One thing that I was wondering about is that, as you mentioned in the video, for basically every python file, you create a corresponding test file. Personally, this makes a lot of sense to me and keeps everything in order, but previously in a clean code video, I thought they stated that this could result in overcoupling.
    Part of the video: th-cam.com/video/58jGpV2Cg50/w-d-xo.html
    Are they talking about something fundamentally different, or how would you explain this?

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

      "clean code" is a lie -- and you should be immediately skeptical of anyone trying to sell you that lie

    • @daniel-mizsak
      @daniel-mizsak ปีที่แล้ว +2

      @@anthonywritescode Could you maybe detail this a little more?

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

      @@anthonywritescode dirty code is more fun anyway

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

    😁

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

    I've heard about this great tool, pre-commit. It's from this really cool guy, you should definitely give it a try *wink*.
    It would be nice to have a "getting started with pre-commit".

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

      yeah I've recorded this and thrown it away few times -- maybe the next recording I'll be happy with