Python Forum
unittest.assertEqual giving failure on result although it's ok.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unittest.assertEqual giving failure on result although it's ok.
#1
Hi, i have been trying the unittest.TestCase class but when I tried it using this python program it somehow says the assertion it's wrong.

employee.py
class Employee():
    """Manages an employee's information."""

    def __init__(self, first_name, last_name, annual_salary):
        """Initializes the Employee class."""
        self.first_name = first_name
        self.last_name = last_name
        self.annual_salary = int(annual_salary)

    def give_raise(self, raise_amount=5000):
        """Adds a number to the employee annual salary."""
        if raise_amount:
            int(raise_amount)
            self.annual_salary += raise_amount
        else:
            self.annual_salary += 5000

            return self.annual_salary
employee_test.py
import unittest
from employee import Employee


class TestEmployee(unittest.TestCase):
    """Testing the Employee class."""

    def setUp(self):
        """Creates an instance which will be used to tst the class"""
        self.employee_one = Employee('Francisco', 'Wendeburg', 500000)
        self.salary_rise = 100000

    def test_employee_default_raise(self):
        """Will the default raise work?"""
        self.employee_one.give_raise()
        self.assertEqual(self.employee_one, 505000)

    def test_employee_given_raise(self):
        """Will test if input raise is working."""
        self.employee_one.give_raise(self.salary_rise)

        self.assertEqual(self.employee_one, 600000)


unittest.main()
The error:
Error:
FF ====================================================================== FAIL: test_employee_default_raise (__main__.TestEmployee) Will the default raise work? ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\Users\Franc\Documents\python_work\chapter_eleven\employee\employee_test.py", line 16, in test_employee_default_raise self.assertEqual(self.employee_one, 505000) AssertionError: <employee.Employee object at 0x02AC5F30> != 505000 ====================================================================== FAIL: test_employee_given_raise (__main__.TestEmployee) Will test if input raise is working. ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\Users\Franc\Documents\python_work\chapter_eleven\employee\employee_test.py", line 22, in test_employee_given_raise self.assertEqual(self.employee_one, 600000) AssertionError: <employee.Employee object at 0x02AC5FF0> != 600000 ---------------------------------------------------------------------- Ran 2 tests in 0.003s FAILED (failures=2) [Finished in 1.6s] Linter Severity Provider Description Line Git GitHub Initialize a new project directory with a Git repository Create repository

Thanks in advance!
Reply
#2
you want to compare annual_salary attribute, not the object itself

self.assertEqual(self.employee_one.annual_salary, 505000)
also in the Employee.give_raise() method there is line 11 int(raise_amount). Effectivelly it will do nothing because you don't assign the converted value
By the way, in your give_raise method you don't need the if else block.
If the user does not supply raise_amount, the default value will be used. i.e. if raise_amount will always be True, unless user does not supply something like None. But you don't try to validate the input so you will just end up with exception. In other words at the moment else part will not be executed even if user does not supply explicitly raise_amount

def give_raise(self, raise_amount=5000):
    """Adds a number to the employee annual salary."""
    self.annual_salary += raise_amount
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Feb-16-2019, 10:17 PM)buran Wrote: you want to compare annual_salary attribute, not the object itself

self.assertEqual(self.employee_one.annual_salary, 505000)
also in the Employee.give_raise() method there is line 11 int(raise_amount). Effectivelly it will do nothing because you don't assign the converted value
By the way, in your give_raise method you don't need the if else block.
If the user does not supply raise_amount, the default value will be used. i.e. if raise_amount will always be True, unless user does not supply something like None. But you don't try to validate the input so you will just end up with exception. In other words at the moment else part will not be executed even if user does not supply explicitly raise_amount

def give_raise(self, raise_amount=5000):
    """Adds a number to the employee annual salary."""
    self.annual_salary += raise_amount

Thanks! I ended up with so much unnecessary things in the program because at first I tried using another solution to the problem but then when the test failed I added the raise_amount parameter and forgot to delete some of the things that were used in the previous version of the method. I ended up with this:
    def give_raise(self, raise_amount=''):
        """Adds a number to the employee annual salary."""
        if raise_amount:
            int(raise_amount)
            self.annual_salary += raise_amount
        else:
            self.annual_salary += 5000

        return self.annual_salary
Although it's more efficient to use the default value 5000 for the raise_amount parameter and then add a little code to add the raise_amount to annual_salary.
Reply
#4
I don't understand why you would have empty string as a default value of numeric attribute, but it's your decision after all.
And again - if the following line
int(raise_amount)
is intended to allow user to pass string, not int/float, then you should know it will not work.
it should be
raise_amount = int(raise_amount)
and there is drawback - if you pass float, it will convert it to int and you will loose the decimal part
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in using unittest akbarza 2 302 Feb-25-2024, 12:51 PM
Last Post: deanhystad
  Failure to run source command middlestudent 2 689 Sep-22-2023, 01:21 PM
Last Post: buran
Question Unwanted execution of unittest ThomasFab 9 2,050 Nov-15-2022, 05:33 PM
Last Post: snippsat
  Dickey Fuller failure Led_Zeppelin 4 2,606 Sep-15-2022, 09:07 PM
Last Post: Led_Zeppelin
  unittest.mock for an api key silver 3 1,380 Aug-29-2022, 03:52 PM
Last Post: ndc85430
  Ran 0 tests in 0.000s - unittest Peaches 8 5,069 Dec-31-2021, 08:58 AM
Last Post: Peaches
  Assert failure jtcostel 1 1,635 Sep-03-2021, 05:28 PM
Last Post: buran
Sad Problem with Unittest mhanusek 1 3,744 Nov-12-2020, 04:58 PM
Last Post: Gribouillis
  Unittest et patch mad31 2 2,121 Aug-09-2020, 06:16 AM
Last Post: mad31
  Unusual things to do with unittest/HTMLTestRunner AndyHolyer 0 2,138 Jul-29-2020, 02:43 PM
Last Post: AndyHolyer

Forum Jump:

User Panel Messages

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