Created
April 3, 2023 14:02
-
-
Save jslvtr/815035d97f7a02d043a44237999dc27c to your computer and use it in GitHub Desktop.
Introduction to pytest: A Beginner's Guide (https://youtu.be/Kt6QqGoAlvI)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
class Reply: | |
def __init__(self, body: str, user: str, created: datetime.datetime) -> None: | |
self.body = body | |
self.user = user | |
self.created = created | |
def __repr__(self) -> str: | |
return f"<Reply from '{self.user}' on '{self.created.strftime('%Y-%m-%d %H:%M')}'>" | |
def __eq__(self, __value: object) -> bool: | |
if not isinstance(__value, Reply): | |
return False | |
return ( | |
self.body == __value.body and | |
self.user == __value.user and | |
self.created == __value.created | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import pytest | |
from reply import Reply | |
def test_init(): | |
body = "Hello, World!" | |
user = "user1" | |
created = datetime.datetime.now() | |
reply = Reply(body, user, created) | |
assert reply.body == body | |
assert reply.user == user | |
assert reply.created == created | |
def test_repr(): | |
body = "Hello, World!" | |
user = "user1" | |
created = datetime.datetime(2023, 1, 1, 0, 0, 0) | |
reply = Reply(body, user, created) | |
expected_repr = f"<Reply from '{user}' on '2023-01-01 00:00'>" | |
assert repr(reply) == expected_repr | |
def test_repr_without_date(): | |
body = "Hello, World!" | |
user = "user1" | |
created = "2021-02-07 12:00" | |
reply = Reply(body, user, created) | |
with pytest.raises(AttributeError): | |
assert repr(reply) == f"<Reply from '{user}' on '2021-02-07 12:00'>" | |
def test_eq_same_values(): | |
body = "Hello, World!" | |
user = "user1" | |
created = datetime.datetime.now() | |
reply1 = Reply(body, user, created) | |
reply2 = Reply(body, user, created) | |
assert reply1 == reply2 | |
def test_eq_different_values(): | |
body1 = "Hello, World!" | |
user1 = "user1" | |
created1 = datetime.datetime.now() | |
body2 = "Goodbye, World!" | |
user2 = "user2" | |
created2 = created1 + datetime.timedelta(minutes=1) | |
reply1 = Reply(body1, user1, created1) | |
reply2 = Reply(body2, user2, created2) | |
assert reply1 != reply2 | |
def test_eq_different_types(): | |
body = "Hello, World!" | |
user = "user1" | |
created = datetime.datetime.now() | |
reply = Reply(body, user, created) | |
non_reply = "Not a reply object" | |
assert reply != non_reply |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import os | |
import pytest | |
def test_simple_add(): | |
assert 1 + 1 == 2 | |
# @pytest.mark.xfail | |
# @pytest.mark.skip | |
# @pytest.mark.skipif(os.name == "posix", reason="does not work on mac") | |
def test_failing(): | |
assert 'a' == 'b' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import pytest | |
from reply import Reply | |
from thread import Thread | |
@pytest.fixture | |
def thread(): | |
"""This fixture returns a Thread object""" | |
title = "Hello, World!" | |
user = "user1" | |
replies = [ | |
Reply("Hello, World!", "user1", datetime.datetime(2023, 1, 1, 0, 0, 0)), | |
Reply("Goodbye, World!", "user2", datetime.datetime(2023, 1, 2, 0, 0, 0)), | |
] | |
return Thread(title, user, replies) | |
def test_init(): | |
title = "Hello, World!" | |
user = "user1" | |
replies = [ | |
Reply("Hello, World!", "user1", datetime.datetime(2023, 1, 1, 0, 0, 0)), | |
Reply("Goodbye, World!", "user2", datetime.datetime(2023, 1, 2, 0, 0, 0)), | |
] | |
thread = Thread(title, user, replies) | |
assert thread.title == "Hello, World!" | |
assert thread.user == "user1" | |
assert thread.replies == replies | |
def test_repr(thread): | |
assert repr(thread) == "<Thread 'Hello, World!' by 'user1'>" | |
def test_reply(thread): | |
new_reply = Reply("Hello, World!", "user1", datetime.datetime(2023, 1, 3, 0, 0, 0)) | |
thread.reply(new_reply) | |
assert new_reply in thread.replies | |
def test_reply_more_recent(thread): | |
new_reply = Reply("Hello, World!", "user1", datetime.datetime(2023, 1, 1, 0, 0, 0)) | |
with pytest.raises(ValueError): | |
thread.reply(new_reply) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from reply import Reply | |
class Thread: | |
def __init__(self, title: str, user: str, replies: list[Reply] = None) -> None: | |
self.title = title | |
self.user = user | |
self.replies = replies or [] | |
def __repr__(self) -> str: | |
return f"<Thread '{self.title}' by '{self.user}'>" | |
def reply(self, reply: Reply) -> None: | |
if reply.created < self.replies[-1].created: | |
raise ValueError("New reply is older than the last reply") | |
self.replies.append(reply) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment