February 2022
Intermediate to advanced
274 pages
6h 28m
English
A lot of lovely single-file Python applications could use a little test coverage. Single-file applications, sometimes called scripts, are often not packaged or deployed, but just shared as single files. In those cases, it’s handy to put the test code right into the script.
Here’s a small example:
| | def foo(): |
| | return "foo" |
| | |
| | |
| | def bar(): |
| | return "bar" |
| | |
| | |
| | def baz(): |
| | return "baz" |
| | |
| | |
| | def main(): |
| | print(foo(), baz()) |
| | |
| | |
| | if __name__ == "__main__": # pragma: no cover |
| | main() |
| | |
| | # test code, requires pytest |
| | |
| | |
| | def test_foo(): |
| | assert foo() == "foo" |
| | |
| | |
| | def test_baz(): |
| | assert baz() == "baz" |
| | |
| | |
| | def test_main(capsys): |
| | main() ... |
Read now
Unlock full access