test_io_utils.py
python_old/pytest-workshop/pytest-workshop-starter/tests/test_io_utils.py
import json
from src.io_utils import write_lines, read_lines, get_api_key, greet, fetch_json
def test_write_and_read(tmp_path):
p = tmp_path / "data.txt"
write_lines(p, ["a", "b", "c"])
assert read_lines(p) == ["a", "b", "c"]
def test_get_api_key(monkeypatch):
monkeypatch.setenv("API_KEY", "secret123")
assert get_api_key() == "secret123"
def test_greet_output_and_log(capsys, caplog):
caplog.set_level("INFO")
greet("Nelson")
out = capsys.readouterr().out
assert "Hello, Nelson!" in out
assert any("Greeted Nelson" in r.message for r in caplog.records)
def test_fetch_json_mocker(mocker):
fake = {"ok": True}
get = mocker.patch("src.io_utils.requests.get")
get.return_value.json.return_value = fake
assert fetch_json("http://example.com") == fake
get.assert_called_once_with("http://example.com", timeout=5)
관련 글
01_classical_caesar_cipher.py
01_classical_caesar_cipher.py — python source code from the python old learning materials (python_old/Cryptography/01_classical_caesar_cipher.py).
글 읽기 →02_classical_monoalphabetic.py
02_classical_monoalphabetic.py — python source code from the python old learning materials (python_old/Cryptography/02_classical_monoalphabetic.py).
글 읽기 →03_classical_rail_fence.py
03_classical_rail_fence.py — python source code from the python old learning materials (python_old/Cryptography/03_classical_rail_fence.py).
글 읽기 →04_frequency_analysis.py
04_frequency_analysis.py — python source code from the python old learning materials (python_old/Cryptography/04_frequency_analysis.py).
글 읽기 →05_cryptographic_hashing.py
05_cryptographic_hashing.py — python source code from the python old learning materials (python_old/Cryptography/05_cryptographic_hashing.py).
글 읽기 →06_password_hashing.py
06_password_hashing.py — python source code from the python old learning materials (python_old/Cryptography/06_password_hashing.py).
글 읽기 →