Skip to content

Commit cdbbbd2

Browse files
committed
add python testing_cli_argparse example
1 parent 7282ab5 commit cdbbbd2

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

python/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python Snippets
2+
3+
- [Testing with argparse](./testing_cli_argparse)
Binary file not shown.

python/testing_cli_argparse/script.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import argparse
2+
from typing import Optional
3+
from typing import Sequence
4+
5+
6+
def main(argv: Optional[Sequence[str]] = None) -> int:
7+
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument("first")
10+
11+
args = parser.parse_args(argv)
12+
13+
return 0 if args.first == "foo" else 1
14+
15+
16+
if __name__ == "__main__":
17+
exit(main())

python/testing_cli_argparse/test.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from script import main
2+
3+
4+
def test_main() -> None:
5+
assert main(["foo"]) == 0, "Expected foo to return 0"
6+
assert main(["bar"]) == 1, "Expected bar to return 1"
7+
8+
9+
test_main()

0 commit comments

Comments
 (0)