Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7913363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:49:32+00:00 2026-06-03T13:49:32+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:49:33+00:00Added an answer on June 3, 2026 at 1:49 pm

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often write codes in MATLAB/Python to test whether my algorithm is feasible (&
I'm just beginning to learn python. I wrote an example script to test OOP
I'm using Selenium RC to write test cases for a website that's at least
I want to write a python decorator to decorate a test function of a
I'm using Python's unittest. I'd like to write a test that ensures a certain
I am using unittest.TestCase to write test cases for my django app (which is
I am doing some R&D on selenium+python. I wrote some test cases in python
I wrote an XML RPC server in python and a simple Test Client for
Is there a way to abort a python write operation in such a way
I write Python script to copy files; unfortunately it keeps failing because filename is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.