I am very new in python programming and trying to learn unittest currently. I had wrote a program called Current_age.py and wanting to write a unittest for this, in which i don't know how. So please help me. Though i have given it a try but it does not work as i expected.
Current_age.py
import datetime
def age(birthday):
todays = datetime.date(2001, 5, 12)
yyyy = int(birthday[0:4])
mm = int(birthday[4:6])
dd = int(birthday[6:8])
date_of_birth = datetime.date(yyyy,mm,dd)
age_in_day = (todays - date_of_birth).days
age_in_years = round(age_in_day/365)
return age_in_years
And then i wrote a unittest file which called test_age
import unittest
import Current_age
class test_age(unittest.TestCase):
def Test_Learning_classes_age(self):
self.assertEqual(Current_age.age("20000315"), 1
if __name__ == "__main__":
unittest.main()
I use VS Code and had created a Virtual Environment and run the code straight on Vs Code because it is easier. So when i ran the code, it said
0 test ran in 0.00s
Ok
Please help me on this.
Hello there everyone, thank for commencing on the program.
I have try to rename some of the methods and classes with the files. And it work. But i really don't know where is the difference so if you can please help me spot it.
The first file is called Age.py which store the function.
import datetime
def age(birthday):
todays = datetime.date(2001, 5, 12)
yyyy = int(birthday[0:4])
mm = int(birthday[4:6])
dd = int(birthday[4:6]
date_of_birth = datetime.date(yyyy, mm, dd)
age_in_day = (todays - date_of_birth).days
age_in_years = round(age_in_day / 365)
return age_in_years
And then i created another file to test the function which called Testage.py
import Age
import unittest
class Testage(unittest.Testcase):
def test_age(self):
self.assertEqual(Age.age("19710113"), 30)
self.assertEqual(Age.age("20000712"), 1)
if __name__ == "__main__":
unittest.main()
And another question which is also related to unittest. Can i create a unittest that of a function which have user input()? And if it is possible how shall I do it? Thanks.