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! - วิทยาศาสตร์และเทคโนโลยี
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.
Saying that your videos are amazing would be an understatement :) thanks for the explanation!
glad you're enjoying them!
ALL python coders should watch your videos
thank you Anthony! You definitely have a talent for explaining complex things in simple language
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 :)
glad you're enjoying it!
Continuing on the pytest path, an interesting advanced one would be fixtures and dependency injection
By the way, this is great content. Cheers
I would love to see a video on the best/most common use cases for fixtures in pytest!
I have plans to do a thing on fixtures at some point!
@@anthonywritescode Nice! Looking forward to it.
I use python for five years , your videos are t h e best
Awesome work, Thanks Anthony
a lot of very useful info presented very nicely. Thank you anthony.
Awesome tutorial!
nice explanation !
any reason why you didn't import Optional and Sequence in a single line import?
yep! that's covered in th-cam.com/video/Sjor8PZXnaw/w-d-xo.html
awesome quick intro to cli, learned some new stuff. How about talking about asyncio next? ;D
I don't know that much about asyncio, but maybe I could do like a hello world program or something -- stay tuned!
@@anthonywritescode will definitely stay tuned 👍.. Hello worlds are plentiful though, am having a harder time finding useful real-world examples 😉..
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.
you can monkeypatch using the `unittest.mock` module:
```python
from unittest import mock
with mock.patch.object(sys, 'argv', ['prog', 'first', 'last']):
...
```
@@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.
what are you stuck on?
@@anthonywritescode
Do I need a @mock.patch…?
Or capsys in the def test_sys():?
And is there a readouterr() I assert something to?
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
Looks great!
Will you plan on some tutorials how to do TDD (Test Driven Development) in practice?
I did already! th-cam.com/video/JmMxU8ljiOg/w-d-xo.html
damn u r good , thanks
argparse throws an error if pass in an empty string
python hello.py ""
hello.py: error: the following arguments are required: person
are you on windows? that should work fine I think (an empty string is a valid command line parameter)
@anthonywritescode Yes, I'm on windows. Ended up just using " ", so on to the next video.
I find the background music a bit distracting,
Maybe I am not used to it in your videos..
good news, it's gone after the first couple videos
انته ❣️
At 03:55, you can change the index to 0 to remove the index error and to 2 to get 2nd name, Jeff.
that doesn't fix anything, it breaks the point of the program -- `argv[0]` is the name of the program itself
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?
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