February 2022
Intermediate to advanced
274 pages
6h 28m
English
Reaching for builtin fixtures whenever possible is a great way to simplify your own test code. The exercises below are designed to give you experience using tmp_path and monkeypatch, two super handy and common builtin fixtures.
Take a look at this script that writes to a file:
| | def hello(): |
| | with open("hello.txt", "w") as f: |
| | f.write("Hello World!\n") |
| | |
| | |
| | if __name__ == "__main__": |
| | hello() |
Write a test without fixtures that validates that hello() writes the correct content to hello.txt.
Write a second test using fixtures that utilizes a temporary directory and monkeypatch.chdir().
Add a print statement to see where the temporary directory is located. Manually check the hello.txt file after ...
Read now
Unlock full access