Reputation: 419
My problem is: In my following test, everything is working today but not will work tomorrow, I'm a beginner and did try a lot of options, but I failed, I,m trying pass "now" as a parameter but with no success until now. I have to stop the "datetime.now()" and put one fixed date to can test all variations. I had god progress until here, but I'm stuck on this Can you help me, please? Thank you. Flavio
import unittest
from datetime import datetime
def get_last_name_and_birthday(name, d):
x = name.split()
dob = d.split("-")
year, month, day = int(dob[2]), int(dob[1]), int(dob[0])
user_birthday = datetime(year, month, day)
return x[-1], user_birthday
def calc_days(user_birthday):
now = datetime.now()
if user_birthday < now:
birthday = datetime(now.year + 1, user_birthday.month, user_birthday.day)
return (birthday - now).days + 1
else:
birthday = datetime(now.year, user_birthday.month, user_birthday.day)
return (birthday - now).days + 1
def generate_output(last_name, cd):
if cd == 365:
return "Hello Mr " + last_name + " Happy Birthday"
elif cd < 365:
return "Hello Mr " + last_name + " your birthday is in " + str(cd) + " days"
else:
return "Hello Mr " + last_name + " your birthday is in " + str(cd - 365) + " days"
def process_name_and_birthday(name, dob):
last_name, user_birthday = get_last_name_and_birthday(name, dob)
cd = calc_days(user_birthday)
return generate_output(last_name, cd)
#name = input("type your full name: ")
#dob = input("type your date of birthday(dd-mm-yy): ")
#print(process_name_and_birthday(name, dob))
class BirthdayTest(unittest.TestCase):
def test_same_day_birthday(self):
self.assertEqual("Hello Mr Oliveira Happy Birthday", process_name_and_birthday("Flavio Oliveira", "11-06-1990"))
class DaysToBirthdayTest(unittest.TestCase):
def test_days_to_birthday(self):
self.assertEqual("Hello Mr Oliveira your birthday is in 9 days", process_name_and_birthday("Flavio Oliveira", "20-06-1978"))
class DaysToPassedBirthdayTest(unittest.TestCase):
def test_how_many_days_passed_birthday(self):
self.assertEqual("Hello Mr Oliveira your birthday is in 364 days", process_name_and_birthday("Flavio Oliveira", "10-06-1978"))
unittest.main()
Upvotes: 1
Views: 648
Reputation: 1914
Add the following three lines under your import of datetime
:
from unittest.mock import Mock
datetime = Mock(wraps=datetime)
datetime.now.return_value = datetime(2020, 6, 11, 20)
There is more information about the mock
module here: https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock
Upvotes: 4