Creating a Unittest
<p>Hey I'm pretty new to python and I'm trying to create a Unit test for some code and I'm running into a lot of trouble, to be honest I'm wondering if the code is even testable. </p>

<pre><code>""" 
Core employee class
"""


class Employee(object):
empCount = 0

def __init__(self, name, salary, debt, takehome):
    self.name = name
    self.salary = salary
    self.debt = debt
    self.takehome = takehome

def display_employee(self):
    print "Name : ", self.name, ",Salary: ", self.salary, "Debt: ", \
        self.debt, "Take Home: ", self.takehome


emp1 = Employee("Scott", 2000, 200, 2000-200)

emp2 = Employee("Mary", 5000, 300, 5000-300)

emp3 = Employee("Sam", 4000, 700, 4000-700)

emp4 = Employee("Sarah", 7000, 2000, 7000-200)

emp5 = Employee("Charlie", 10000, 5000, 10000-5000)

emp6 = Employee("Tony", 16000, 20000, 16000-20000)


employees = [emp1, emp2, emp3, emp4, emp5, emp6]


for employee in employees:
    employee.display_employee()


Employee.empCount = len(employees)

print "Total Employees is %d" % Employee.empCount}
</code></pre>

<p>If anyone would give me a hand that would be much appreciated, also any tips if I need to change to the code so it could run unittests on it more easily  </p>

<p>I think your class is testable. But the tests are trivial since you only have an Ë__init__` method and a display method.</p>

<p>The starting for unit testing is using <strong><a href="https://docs.python.org/2/library/unittest.html" rel="nofollow">unittest</a></strong> library.</p>

<p>Write a single test function for each "public" method:</p>

<pre><code>class TestEmployee(unittest.TestCase):
    def test_init(self):
        ...

    def test_display_employee(self):
        ...
</code></pre>

<p>There are lots of tutorials on Internet, look at this one: <a href="http://docs.python-guide.org/en/latest/writing/tests/" rel="nofollow">Testing Your Code</a>. </p>

<p>See also: <a href="http://pythontesting.net/framework/unittest/unittest-introduction/" rel="nofollow">Python testing</a></p>

python python-2.7