How should I rewrite my code to make it amenable to unittesting?
- by justin
I've been trying to get started with unit-testing while working on a little cli program.
My program basically parses the command line arguments and options, and decides which function to call. Each of the functions performs some operation on a database.
So, for instance, I might have a create function:
def create(self, opts, args):
#I've left out the error handling.
strtime = datetime.datetime.now().strftime("%D %H:%M")
vals = (strtime, opts.message, opts.keywords, False)
self.execute("insert into mytable values (?, ?, ?, ?)", vals)
self.commit()
Should my test case call this function, then execute the select sql to check that the row was entered? That sounds reasonable, but also makes the tests more difficult to maintain. Would you rewrite the function to return something and check for the return value?
Thanks