[SOLVED] How do you write unittest code to test the code like getDateOfYesterday?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How do you write unittest code to test the code like getDateOfYesterday?
Like you write a function to return the date of yesterday,Nomatter what language we use, Let me take python language as the example,
the code as below:
If I want to write code to unittest this function, The way I could figure out is to hard coded the expected return value,
Like:
Code:
class Test_getDateOfYesterday(unittest.TestCase):
def test_1(self):
onedayago = getDateOfYesterday
self.assertEqual("20140924", onedayago, "")
But then everytime I need to run this unittest, I have to modify the expected return date to pass this unittest.
It's tedious.
If I don't hard coded the expected return date, then I need to write another code to verify this function.
Then what if the another code has the same fault as this function has?
Could somebody explain some theory which unittest such situation?
This question has bothered me a long time.
Hope I explain the problem clearly, and Thanks in advance.
The problem is that you've coded with an absolute string and are not considering variability of the input.
A proper unit test first examines all possible input values; including illegal values and all possible output values.
To properly unit test something you should be testing all possible input values which can be sent to that module, or the best representation of values you can conceive of and you should automate the testing better.
Two ways I might approach this, and I would use both ways for a full and complete test:
Alter system date in iteration to cause the output of datetime.datetime.now() to be a broad range of values and test iteratively
Replace the possible values that agodate could be with test table data and test iteratively
In all cases, you should have a table of expected results which you compare against when you write your test function.
I would use both methods, but lean more on method #2 for the following reasons. In method #1, your system is only going to accept legal dates, and providing the library function now() works, which it does, then you're always only ever going to test legal date strings and therefore fail to test illegal date strings. Using method #2 you can cause incorrect input and verify that your module rejects or does no continued harm.
Either case, in writing your test harness, you do not hard code the expected result, you instead contrive a variety of inputs as broadly defined as you can and you also code expected outputs or behaviors from the module under test and then you run the test program and generate a report.
A bit more complicated approach, but one that does not require the module you are testing to be changed, is to mock datetime.datetime.now method. Unfortunately, datetime.datetime is a built-in type so you have to mock the whole thing:
mina86, Thanks for your patient explanation and the example code, That's really helpful, Your view open my mind and expand my view too. I really appreciate your help. Thanks again.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.