Write test_basics.py, run pytest, break something on purpose, and meet pytest.raises()
Day 67 of 80
Before writing any code, spend 10 minutes with the official docs: pytest — Getting Started. Skim it for the structure. You don't need to absorb every detail — you'll learn by doing starting in the next section.
The pytest "Getting Started" page is unusually well-written and brief. It shows exactly the pattern you're about to use. Pay attention to the test_sample.py example — it's the same structure you'll use all week.
Create a new file called test_basics.py anywhere in your project. Type (don't paste) these four tests:
def test_addition():
assert 1 + 1 == 2
def test_string_upper():
assert "kling".upper() == "KLING"
def test_list_append():
shots = ["wide", "close"]
shots.append("aerial")
assert len(shots) == 3
assert "aerial" in shots
def test_dictionary_access():
prompt = {"platform": "Kling", "shot": "golf swing"}
assert prompt["platform"] == "Kling"
$ pytest test_basics.py -v
test_basics.py::test_addition PASSED [ 25%]
test_basics.py::test_string_upper PASSED [ 50%]
test_basics.py::test_list_append PASSED [ 75%]
test_basics.py::test_dictionary_access PASSED [100%]
=================== 4 passed in 0.02s ===================
-v flag gives you names instead of just dots — always use it while learning.Edit test_addition to fail intentionally. Change the assertion so it's wrong:
def test_addition():
assert 1 + 1 == 3 # wrong on purpose
FAILED test_basics.py::test_addition - assert 2 == 3
========================= FAILURES =========================
____________________ test_addition ____________________
def test_addition():
> assert 1 + 1 == 3
E assert 2 == 3
E + where 2 = 1 + 1
test_basics.py:2: AssertionError
=================== 1 failed, 3 passed in 0.04s ===================
== 2 before moving on.Your code should raise errors in certain situations. For example, int("not a number") should raise a ValueError. If you want to verify that your validation code raises the right error when given bad input, you need pytest.raises().
It's a context manager. If the expected exception is raised inside the with block, the test passes. If no exception is raised (or the wrong one is), the test fails.
import pytest
def test_invalid_number():
with pytest.raises(ValueError):
int("not a number")
int("not a number") raises a ValueError. If you changed the argument to "42", no exception would be raised and the test would fail — because it expected an error and didn't get one.Add two more error tests to solidify the pattern:
def test_key_error():
d = {"platform": "Kling"}
with pytest.raises(KeyError):
_ = d["missing_key"]
def test_index_error():
shots = ["wide", "close"]
with pytest.raises(IndexError):
_ = shots[99]
Prompt("Pika", ...) raises InvalidPlatformError. The structure is identical — you just swap in your custom exception class.$ pytest test_basics.py -v
test_basics.py::test_addition PASSED [ 14%]
test_basics.py::test_string_upper PASSED [ 28%]
test_basics.py::test_list_append PASSED [ 42%]
test_basics.py::test_dictionary_access PASSED [ 57%]
test_basics.py::test_invalid_number PASSED [ 71%]
test_basics.py::test_key_error PASSED [ 85%]
test_basics.py::test_index_error PASSED [100%]
=================== 7 passed in 0.03s ===================
with pytest.raises() block. This is the foundation you'll build on tomorrow.Before tomorrow, write at least 3 more tests of your own. Pick any Python behavior you've used in this course — string formatting, list operations, dictionary lookups, file paths, whatever. The goal is to make the test_ naming and assert pattern feel automatic.
Some ideas to try:
"Kling".lower() == "kling"Path object's .suffix is ".json"ZeroDivisionError is raised when dividing by zerotest_basics.py with at least 4 test functionspytest test_basics.py -v and saw all tests pass (green)import pytest and used pytest.raises() for at least one error testtest_ prefix is required for pytest discoveryDay 68 is where it gets real. You'll create a tests/ folder inside the DVP Prompt Vault project and write comprehensive tests for the Prompt and PromptLibrary classes — including the tmp_path fixture for safe file operations.