Python Forum
Test a class function via "unittest " - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Test a class function via "unittest " (/thread-28227.html)



Test a class function via "unittest " - Penguin827 - Jul-10-2020

import unittest
#from employee113 import Employee

class Employee():
	"""a class for show employee"""
	def __init__(self,first_name,last_name,salary):
		self.first = first_name
		self.last = last_name
		self.salary = salary 
		
	def give_rise(self,add = 5000):
		self.salary +=  add	
		return(self.salary)
		
		
class TestEmployeeSalary(unittest.TestCase):
	"""test employee salary increment"""
	def setUp(self):
	    """create a survey and a set of prarmemeters for use in all test methods"""
	    self.test_s1 = Employee("CK","Chan",40000)
	    
	def test_salary_fix_increment(self): 
		"""test fix salary default 5000 increment"""
		self.test_s1.give_rise()
		self.assertEqual(self.test_s1.give_rise(),45000 )
		
	def test_salary_floating_increment(self):
	    """test floating salary icnrement"""
	    self.test_s1.give_rise(500)
	    self.assertEqual(self.test_s1.give_rise(), 40500)	    
	

unittest.main()
Output:
FF ====================================================================== FAIL: test_salary_fix_increment (__main__.TestEmployeeSalary) test fix salary default 5000 increment ---------------------------------------------------------------------- Traceback (most recent call last): File "main.py", line 25, in test_salary_fix_increment self.assertEqual(self.test_s1.give_rise(),45000 ) AssertionError: 50000 != 45000 ====================================================================== FAIL: test_salary_floating_increment (__main__.TestEmployeeSalary) test floating salary icnrement ---------------------------------------------------------------------- Traceback (most recent call last): File "main.py", line 30, in test_salary_floating_increment self.assertEqual(self.test_s1.give_rise(), 40500) AssertionError: 45500 != 40500 ---------------------------------------------------------------------- Ran 2 tests in 0.001s FAILED (failures=2)
i dont know why the amounts are not correct on the above test my class via import unittest
however,if i directly input the parameters in the class as below :

class Employee():
	"""a class for show employee"""
	def __init__(self,first_name,last_name,salary):
		self.first = first_name
		self.last = last_name
		self.salary = salary 
		
	def give_rise(self,add = 5000):
		self.salary +=  add	
		return(self.salary)


test_s1 = Employee("CK","Chan",40000)
print(test_s1.give_rise())  # a default value 5000 increment in salary
print(test_s1.give_rise(500)) # a floating value 500 increment in salary
Output:
45000 45500
the first question is : as i expect the result should be 45000, 40500
i cant figure it out what it is my problem in the class TestEmployeeSalary(unittest.TestCase).

the second question is : the outcome in the class TestEmployeeSalary(unittest.TestCase) are 50000,45500 (which is different from what i directly input the parameters in the class 45000 ,45500) even with the same function.

I need some help.


RE: Test a class function via "unittest " - Gribouillis - Jul-10-2020

The problem is that you are calling twice self.test_s1.give_rise() in the test functions. Hence you are raising the employee's salary twice.