How should I rewrite my code to make it amenable to unittesting?

Posted by justin on Stack Overflow See other posts from Stack Overflow or by justin
Published on 2010-04-20T01:17:15Z Indexed on 2010/04/20 1:23 UTC
Read the original article Hit count: 367

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about python

Related posts about unittest