getting started with pytest (beginner - intermediate) anthony explains

Поділитися
Вставка
  • Опубліковано 30 вер 2024
  • 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/aso...
    stream github: github.com/ant...
    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!

КОМЕНТАРІ • 57

  • @tobb10001
    @tobb10001 Рік тому +26

    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.

  • @Jakub1989YTb
    @Jakub1989YTb Рік тому +15

    One of the top python packages! 🙂

    • @jacobsan
      @jacobsan 10 місяців тому +1

      Truly one of the python packages of all time

  • @reyjanson1788
    @reyjanson1788 Рік тому +5

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

  • @StanislavSchmidt1
    @StanislavSchmidt1 Рік тому +4

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

  • @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 4 місяці тому

      # just in case

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

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

  • @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 8 місяців тому +1

    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

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

  • @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: ua-cam.com/video/58jGpV2Cg50/v-deo.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

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

  • @Lines115
    @Lines115 9 днів тому

    11:20 pyupgrade uses another popular lib for testing - unittest, not pytest ._.

  • @NarutoMinecraft007
    @NarutoMinecraft007 6 днів тому

    I try to imagine that the hands are from someone else and trying to move according to what you say hahaha

  • @private9062
    @private9062 13 днів тому

    I want that keyboard!

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

  • @Jakefromstatefarm56503
    @Jakefromstatefarm56503 8 місяців тому

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

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

  • @Karlkkaaarrll
    @Karlkkaaarrll 5 місяців тому

    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?

  • @elenaromanova2841
    @elenaromanova2841 Рік тому +1

    Thanks!

  • @duke007x3
    @duke007x3 8 місяців тому

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

    • @anthonywritescode
      @anthonywritescode  8 місяців тому

      personally I don't think anyone should use indirect

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

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

  • @diegobonaventura8501
    @diegobonaventura8501 Рік тому

    whats with the keyboard view?

  • @jeffreyalidochair
    @jeffreyalidochair 11 місяців тому

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

    • @anthonywritescode
      @anthonywritescode  11 місяців тому

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

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

  • @muddasirkhan805
    @muddasirkhan805 8 місяців тому

    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  8 місяців тому +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 8 місяців тому

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

  • @niconeumann2963
    @niconeumann2963 Рік тому

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

  • @physbuzz
    @physbuzz Рік тому

    😁

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

  • @pawejerzyna5674
    @pawejerzyna5674 9 місяців тому

    great video