getting started with pytest (beginner - intermediate) anthony explains

Поделиться
HTML-код
  • Опубликовано: 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

  • @araujoao
    @araujoao 5 месяцев назад

    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

  • @reyjanson1788
    @reyjanson1788 Год назад +5

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

  • @Jakub1989YTb
    @Jakub1989YTb Год назад +13

    One of the top python packages! 🙂

    • @jacobsan
      @jacobsan 7 месяцев назад +1

      Truly one of the python packages of all time

  • @StanislavSchmidt1
    @StanislavSchmidt1 Год назад +2

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

  • @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.

  • @pawejerzyna5674
    @pawejerzyna5674 6 месяцев назад

    great video

  • @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

  • @Jakefromstatefarm56503
    @Jakefromstatefarm56503 5 месяцев назад

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

  • @duke007x3
    @duke007x3 5 месяцев назад

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

    • @anthonywritescode
      @anthonywritescode  5 месяцев назад

      personally I don't think anyone should use indirect

  • @niconeumann2963
    @niconeumann2963 Год назад

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

  • @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)?

  • @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.

  • @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 :-!

  • @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

  • @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! :)

  • @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?

  • @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 Месяц назад

      # just in case

  • @diegobonaventura8501
    @diegobonaventura8501 Год назад

    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.

  • @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: ruclips.net/video/58jGpV2Cg50/видео.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