Python Forum
Test a class function via "unittest "
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Test a class function via "unittest "
#1
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.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in using unittest akbarza 2 258 Feb-25-2024, 12:51 PM
Last Post: deanhystad
  The function of double underscore back and front in a class function name? Pedroski55 9 562 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  Class test : good way to split methods into several files paul18fr 4 403 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 918 Feb-15-2023, 05:34 PM
Last Post: zsousa
Question Unwanted execution of unittest ThomasFab 9 1,948 Nov-15-2022, 05:33 PM
Last Post: snippsat
  unittest.mock for an api key silver 3 1,336 Aug-29-2022, 03:52 PM
Last Post: ndc85430
  TimeOut a function in a class ? Armandito 1 1,586 Apr-25-2022, 04:51 PM
Last Post: Gribouillis
  Ran 0 tests in 0.000s - unittest Peaches 8 4,928 Dec-31-2021, 08:58 AM
Last Post: Peaches
  Calling a class from a function jc4d 5 1,755 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  a function common to methods of a class Skaperen 7 2,499 Oct-04-2021, 07:07 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020