Jul-10-2020, 07:49 AM
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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 unittesthowever,if i directly input the parameters in the class as below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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, 40500i 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.