February 2022
Intermediate to advanced
274 pages
6h 28m
English
Let’s start with the canonical coding example, Hello World!:
| | print("Hello, World!") |
The run output shouldn’t be surprising:
| | $ cd /path/to/code/ch12/script |
| | $ python hello.py |
| | Hello, World! |
Like any other bit of software, scripts are tested by running them and checking the output and/or side effects.
For the hello.py script, our challenge is to (1) figure out how to run it from a test, and (2) how to capture the output. The subprocess module, which is part of the Python standard library,[42] has a run() method that will solve both problems just fine:
| | from subprocess import run |
| | |
| | |
| | def test_hello(): |
| | result = run(["python", "hello.py" ... |
Read now
Unlock full access