hpr_info.py
test_hpr_info.py
test_hpr_info.py
import hpr_info
Run pytest
ModuleNotFoundError: No module named 'hpr_info'
touch hpr_info.py
pytest
============================= test session starts ==============================
platform linux -- Python 3.11.8, pytest-7.4.4, pluggy-1.4.0
rootdir: /tmp/Demo
collected 0 items
test_hpr_info.py
with a single
line to import hpr_infohpr_info.py
and pytest ran without an
error.test_hpr_info.py
and tried
to execute its testshpr_info.py
assert
tests if a statement is trueasert 1==1
In pytest, we can use assert to check a function returns a specific value
assert module.function() == "Desired Output"
Without doing a comparison operator, we can also use assert to check if something exists without specifying a specific value
assert dictionary.key
hpr_info.py
hpr_info.py
by
prepending the name with hpr_info.
for examplehpr_info.HPR_FEED
import hpr_info
def test_hpr_feed_url():
assert hpr_info.HPR_FEED == "https://hackerpublicradio.org/hpr_ogg_rss.php"
AttributeError: module 'hpr_info' has no attribute 'HPR_FEED'
hpr_info.py
to get the
test to passHPR_FEED = "https://hackerpublicradio.org/hpr_ogg_rss.php"
1 passed
indicating the
pytest found 1 test which passeddef test_get_show_data():
show_data = hpr_info.get_show_data()
assert show_data.status == 200
1 failed, 1 passed
and we
can see the error
AttributeError: module 'hpr_info' has no attribute 'get_show_data'
feedparser
python module to make it
easier to parse the rss feed.hpr_info.py
looks like thisimport feedparser
HPR_FEED = "https://hackerpublicradio.org/hpr_ogg_rss.php"
def get_show_data():
showdata = feedparser.parse(HPR_FEED)
return showdata
-v
flag so I can see each test as it runs.test_hpr_info.py::test_hpr_feed_url PASSED [ 50%]
test_hpr_info.py::test_get_show_data PASSED [100%]
def test_get_latest_entry():
latest_entry = hpr_info.get_latest_entry()
assert latest_entry["title"]
assert latest_entry["published"]
hpr_info.py
and pytest -v
will show 3 passing tests.def get_latest_entry():
showdata = get_show_data()
return showdata["entries"][0]
def test_get_entry_data():
entry_data = hpr_info.get_entry_data(hpr_info.get_latest_entry())
assert entry_data["title"]
assert entry_data["host"]
assert entry_data["published"]
assert entry_data["file"]
And then code to get the test to pass
def get_entry_data(entry):
for link in entry["links"]:
if link.get("rel") == "enclosure":
enclosure = link.get("href")
return {
"title": entry["title"],
"host": entry["authors"][0]["name"],
"published": entry["published"],
"file": enclosure,
}
Now that we have tested that we can, get all the info we want from
the most recent episode lets add the last bit of code to
hpr_info.py
to print the episode info
if __name__ == "__main__":
most_recent_show = get_entry_data(get_latest_entry())
print()
print(f"Most Recent HPR Episode")
for x in most_recent_show:
print(f"{x}: {most_recent_show.get(x)}")
if __name__ == "__main__":
ensures code inside this
block will only run when the script is called directly, and not when
imported by test_hpr_info.py
Unless otherwise stated, our shows are released under a Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.
The HPR Website Design is released to the Public Domain.