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