17

In python, how do we write test cases for our classes? For example:

class Employee(object):
  num_employees = 0


# numEmployess is incremented each time an employee is constructed
  def __init__(self, salary=0.0, firstName="", lastName="", ssID="", DOB=datetime.fromordinal(1), startDate=datetime.today()): #Employee attributes
    self.salary=salary
    self.firstName = firstName
    self.lastName = lastName
    self.ssID = ssID
    self.DOB = DOB
    self.startDate = startDate
    Employee.num_employees += 1 #keep this 

  def __str__(self): #returns the attributes of employee for print
    return str(self.salary) + ', ' + self.firstName + ' ' + self.lastName + ', ' + self.ssID + ', ' + str(self.DOB) + ', ' + str(self.startDate)

I know there is something called unit testing. But I'm not sure how it works at all. Could not find a good explanation I understood online.

VPNTIME
  • 721
  • 2
  • 8
  • 13
  • 1
    You mean [like this](http://docs.python.org/library/unittest.html)? –  May 10 '12 at 00:18

2 Answers2

11

doctest is the simplest. Tests are written in the docstring, and look like REPL episodes.

 ...

  def __str__(self):
    """Returns the attributes of the employee for printing

    >>> import datetime
    >>> e = Employee(10, 'Bob', 'Quux', '123', startDate=datetime.datetime(2009, 1, 1))
    >>> print str(e)
    10, Bob Quux, 123, 0001-01-01 00:00:00, 2009-01-01 00:00:00
    """
    return (str(self.salary) + ', ' +
            self.firstName + ' ' + 
            self.lastName + ', ' +
            self.ssID + ', ' + 
            str(self.DOB) + ', ' +
            str(self.startDate)
            )

if __name__ == '__main__':
  import doctest
  doctest.testmod()
Murray Lynch
  • 357
  • 4
  • 5
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
10

The "Testing Your Code" section of the Hitchhiker's Guide to Python discusses general testing practices/methodologies in Python, as well as introducing specific tools in more-or-less increasingly complex order. As mentioned earlier, doctest is a super-simple way to start...and from there, you may wish to move to unittest() and beyond.

My experience has been that doctest can (and should) be used as a quick and dirty test up front, but beware of going overboard - it can lead to long and ugly docstrings that users of your module may not want to look at, especially if you're exhaustive in your tests and are including all sorts of corner cases. Porting those tests into a dedicated testing framework like unittest() is a better practice in the long run. You can leave just the basics in your doctest so that anyone looking at the docstring will get a quick idea of how the module works in practice.

Air
  • 8,274
  • 2
  • 53
  • 88
Jeff Wright
  • 1,014
  • 1
  • 11
  • 17
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Linda Lawton - DaImTo Jan 23 '15 at 14:02
  • 7
    How is my answer any less useful than Mike's above? All he did was provide a link to the docs for the unittest() module. My link provides a gentler and more thorough description of testing Python code, which is what I understood the question-poser asked about. – Jeff Wright Jan 23 '15 at 14:07