python test - ghdrako/doc_snipets GitHub Wiki

class Task:
  def __init__(self, title, urgency):
    self.title = title
    self.urgency = urgency
  @classmethod
  def task_from_text(cls, text_data):
    title, urgency_text = text_data.split(",")
    urgency = int(urgency_text)
    task = cls(title, urgency)
    return task
  @classmethod
  def task_from_dict(cls, task_data):
    title = task_data["title"]
    urgency = task_data["urgency"]
    task = cls(title, urgency)
    return task
from task_class import Task
import unittest
class TestTask(unittest.TestCase):
  def setUp(self):
    task_to_compare = Task("Laundry", 3)
    self.task_dict = task_to_compare.__dict__
  def test_create_task_from_text(self):
    task_text = "Laundry,3"
    created_task = Task.task_from_text(task_text)
    self.assertEqual(created_task.__dict__, self.task_dict)
  def test_create_task_from_dict(self):
    task_data = {"title": "Laundry", "urgency": 3}
    created_task = Task.task_from_dict(task_data)
    self.assertEqual(created_task.__dict__, self.task_dict)
if __name__ == "__main__":
  unittest.main()