python cli tested with pytest - (beginner to intermediate) anthony explains

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ต.ค. 2024
  • I build out a simple skeleton for a command line interface in python and show how to test it with pytest!
    thanks to PanosTrak for the question!
    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!
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    Such a simple idea, but really enjoyed how you built it into a full script. Filled in blanks for me of how to test and use cases for raising exceptions. Great video. Thank you.

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

    Saying that your videos are amazing would be an understatement :) thanks for the explanation!

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

    ALL python coders should watch your videos

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

    thank you Anthony! You definitely have a talent for explaining complex things in simple language

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

    I found ypur channel on the recommendation of a colleague and I am very glad, cause I have learned a lot of new things from these video! Thanks :)

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

    Continuing on the pytest path, an interesting advanced one would be fixtures and dependency injection
    By the way, this is great content. Cheers

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

    I would love to see a video on the best/most common use cases for fixtures in pytest!

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

      I have plans to do a thing on fixtures at some point!

    • @jrib
      @jrib 3 ปีที่แล้ว

      @@anthonywritescode Nice! Looking forward to it.

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

    I use python for five years , your videos are t h e best

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

    Awesome work, Thanks Anthony

  • @starshipx1282
    @starshipx1282 2 ปีที่แล้ว

    a lot of very useful info presented very nicely. Thank you anthony.

  • @nomik
    @nomik 27 วันที่ผ่านมา

    Awesome tutorial!

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

    nice explanation !

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

    any reason why you didn't import Optional and Sequence in a single line import?

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

      yep! that's covered in th-cam.com/video/Sjor8PZXnaw/w-d-xo.html

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

    awesome quick intro to cli, learned some new stuff. How about talking about asyncio next? ;D

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

      I don't know that much about asyncio, but maybe I could do like a hello world program or something -- stay tuned!

    • @vinzent1992
      @vinzent1992 4 ปีที่แล้ว

      @@anthonywritescode will definitely stay tuned 👍.. Hello worlds are plentiful though, am having a harder time finding useful real-world examples 😉..

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

    I found a very good example in this video: "testing output with pytest (beginner - intermediate) anthony explains #246"
    Thanks again for pytest examples that is not (for me) overly advanced.
    Wonderful explanation of argparse which I am trying to learn at the moment. And a suprisingly simple way of testing it.
    At least in the beginning. But now I can start from there and learn the rest.
    Do you have any examples how to test sys?
    import sys
    def names():
    filename, first, last = sys.argv
    print(f"Welcome {first} {last}.")
    How do I test this?
    I have been searching solutions for days on the internet. They all are too advanced for me. And not really what I am looking for.
    Your solution seems to be close. For the argparse it was perfect.

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

      you can monkeypatch using the `unittest.mock` module:
      ```python
      from unittest import mock
      with mock.patch.object(sys, 'argv', ['prog', 'first', 'last']):
      ...
      ```

    • @ulfsjodin7872
      @ulfsjodin7872 3 ปีที่แล้ว

      @@anthonywritescode
      Hello Anthony. I have tried for a week now without any success. Not all the time of course. I am just a hobby pythonista. Doing a little bit now and then
      If you can give me a little bit more hints I would be glad. Challenges are great for developement. So dont give all of the answer.

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

      what are you stuck on?

    • @ulfen69
      @ulfen69 3 ปีที่แล้ว

      @@anthonywritescode
      Do I need a @mock.patch…?
      Or capsys in the def test_sys():?
      And is there a readouterr() I assert something to?

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

      if you want to reliably change `sys.argv` without polluting other tests then yes use `mock.patch.object` -- I prefer the context manager over the decorator (less magical, more tightly scoped). capsys is needed if you want to test output

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

    Looks great!
    Will you plan on some tutorials how to do TDD (Test Driven Development) in practice?

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

      I did already! th-cam.com/video/JmMxU8ljiOg/w-d-xo.html

  • @SC-ee5zc
    @SC-ee5zc 2 ปีที่แล้ว +1

    damn u r good , thanks

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

    argparse throws an error if pass in an empty string
    python hello.py ""
    hello.py: error: the following arguments are required: person

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

      are you on windows? that should work fine I think (an empty string is a valid command line parameter)

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

      @anthonywritescode Yes, I'm on windows. Ended up just using " ", so on to the next video.

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

    I find the background music a bit distracting,
    Maybe I am not used to it in your videos..

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

      good news, it's gone after the first couple videos

  • @OldOneX
    @OldOneX 3 ปีที่แล้ว

    انته ❣️

  • @tkm3ss10
    @tkm3ss10 3 ปีที่แล้ว

    At 03:55, you can change the index to 0 to remove the index error and to 2 to get 2nd name, Jeff.

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

      that doesn't fix anything, it breaks the point of the program -- `argv[0]` is the name of the program itself

  • @Walruz1000
    @Walruz1000 4 ปีที่แล้ว

    I noticed that in python multiple return statements seem to be accepted, whereas in all the java projects I worked on in the past the sonar builds would fail for this. What is your opinion on that?

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

      I think they're mostly fine -- there are places where multiple return statements can be confusing (and easy to miss one case or another) but as long as a type checker (such as mypy) is set up you're ~mostly guarded against being a problem. imo it's really only an issue when functions get too long and one return statement might be lost further up