Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python unittest - assertNotEqual() function
Next article icon

Python unittest – assertEqual() function

Last Updated : 28 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

assertEqual() is a function from Python’s unittest library used for unit testing. It checks whether two values are equal and returns a boolean result based on the condition. If both values are equal, the test passes otherwise it fails and raises an AssertionError with an optional error message. For Example:

Python
import unittest  class TestNegative(unittest.TestCase):          def test_fun(self):         # Failing test: comparing different strings         self.assertEqual("geeks", "gfg", "'geeks' is not equal to 'gfg'.")  if __name__ == '__main__':     unittest.main() 

Output:

Hangup (SIGHUP)
F
======================================================================
FAIL: test_strings_not_equal (__main__.TestNegative)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 7, in test_strings_not_equal
self.assertEqual("geeks", "gfg", "'geeks' is not equal to 'gfg'.")
AssertionError: 'geeks' != 'gfg' : 'geeks' is not equal to 'gfg'.

Explanation: In this negative test case, assertEqual() compares two different strings, “geeks” and “gfg”, resulting in a test failure with an AssertionError.

Syntax of assertEqual()

assertEqual(firstValue, secondValue, message)

Parameter:

  • firstValue: The first value to be compared.
  • secondValue: The second value to be compared.
  • message (optional): A custom message displayed when the test case fails.

Return Value:

  • If firstValue == secondValue : The test passes and nothing is returned.
  • If firstValue != secondValue : An AssertionError is raised with the provided message (if given).

Note: In the unittest module, test method names should begin with the prefix test_. This is necessary for the unittest framework to automatically discover and run the test methods.

Examples of assertEqual()

Example 1: This checks if assertEqual() works correctly by comparing two equal strings. If the strings match, the test passes, showing that the function is working as expected.

Python
import unittest  class TestPositive(unittest.TestCase):          def test_fun(self):         # Passing test: comparing identical strings         self.assertEqual("hello", "hello", "Both values are equal.")  if __name__ == '__main__':     unittest.main() 

Output

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Explanation: “hello” is compared with “hello” and since both values match, the test passes without errors, verifying that assertEqual() correctly identifies equal values.

Example 2: This test verifies that assertEqual() correctly compares identical integers, ensuring accurate numerical equality evaluation.

Python
import unittest  class TestIntegers(unittest.TestCase):          def test_fun(self):         # Passing test: comparing identical integers         self.assertEqual(10, 10, "This test passes since 10 == 10.")  if __name__ == '__main__':     unittest.main() 
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Explanation: assertEqual() checks 10 against 10, confirming that it works consistently with numerical values as the test passes.

Example 3: This shows a failed test where assertEqual() compares two different integers, highlighting how the function detects mismatched values.

Python
import unittest  class TestFailure(unittest.TestCase):          def test_fun(self):         # Failing test: 5 is not equal to 10         self.assertEqual(5, 10, "This test fails since 5 != 10.")  if __name__ == '__main__':     unittest.main() 
Hangup (SIGHUP)
F
======================================================================
FAIL: test_fun (__main__.TestFailure)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test.py", line 6, in test_fun
self.assertEqual(5, 10, "This test fails since 5 != 10.")
AssertionError: 5 != 10 : This test fails since 5 != 10.

Explanation: assertEqual() compares 5 and 10, leading to an AssertionError. This highlights how the function detects incorrect numerical comparisons, ensuring expected and actual outputs align.



Next Article
Python unittest - assertNotEqual() function

S

Shivam.Pradhan
Improve
Article Tags :
  • Python
  • Python Testing
  • Python unittest-library
Practice Tags :
  • python

Similar Reads

  • Python Unittest Tutorial | Unit Testing in Python using unittest Framework
    Unit Testing is the first level of software testing where the smallest testable parts of software are tested. This is used to validate that each software unit performs as designed. The unittest test framework is Python xUnit style framework. In this article, we will learn about unittest framework wi
    7 min read
  • Python Unittest Comparison Assertions

    • Python - assertLess() function in unittest
      assertLess() in Python is an unittest library function that is used in unit testing to check whether the first given value is less than the second value or not. This function will take three parameters as input and return a boolean value depending upon the assert condition. This function check that
      2 min read

    • Python - assertGreater() function in unittest
      assertGreater() in Python is an unittest library function that is used in unit testing to check whether the first given value is greater than the second value or not. This function will take three parameters as input and return a boolean value depending upon the assert condition. This function check
      2 min read

    • Python - assertGreaterEqual() function in unittest
      assertGreaterEqual() in Python is an unittest library function that is used in unit testing to check whether the first given value is greater than or equal to the second value or not. This function will take three parameters as input and return a boolean value depending upon the assert condition. Th
      2 min read

    • Python - assertLessEqual() function in unittest
      assertLessEqual() in Python is an unittest library function that is used in unit testing to check whether the first given value is less than or equal to the second value or not. This function will take three parameters as input and return a boolean value depending upon the assert condition. This fun
      2 min read

    Python Unittest Approximate Value Assertions

    • Python unittest - assertNotAlmostEqual() function
      assertNotAlmostEqual() in Python is a unittest library function that is used in unit testing to check whether two given values are not approximately. This function will take five parameters as input and return a boolean value depending upon the assert condition. This function check that first and se
      3 min read

    • Python unittest - assertAlmostEqual() function
      assertAlmostEqual() in Python is a unittest library function that is used in unit testing to check whether two given values are almost equal or not. This function will take five parameters as input and return a boolean value depending upon the assert condition. This function check that first and sec
      3 min read

    Python Unittest Type Assertions

    • Python unittest - assertIsInstance() function
      assertIsInstance() in Python is a unittest library function that is used in unit testing to check whether an object is an instance of a given class or not. This function will take three parameters as input and return a boolean value depending upon the assert condition. If the object is an instance o
      2 min read

    • Python unittest - assertNotIsInstance() function
      assertNotIsInstance() in Python is a unittest library function that is used in unit testing to check whether an object is not an instance of a given class or not. This function will take three parameters as input and return a boolean value depending upon the assert condition. If the object is not an
      2 min read

    Python Unittest Membership Assertions

    • Python unittest - assertIn() function
      assertIn() in Python is a unittest library function that is used in unit testing to check whether a string is contained in other or not. This function will take three string parameters as input and return a boolean value depending upon the assert condition. If the key is contained in container strin
      2 min read

    • Python unittest - assertNotIn() function
      assertNotIn() in Python is a unittest library function that is used in unit testing to check whether a string is not contained in other. This function will take three string parameters as input and return a boolean value depending upon the assert condition. If the key is not contained in container s
      2 min read

    Python Unittest Equality Assertions

    • Python unittest - assertEqual() function
      assertEqual() is a function from Python's unittest library used for unit testing. It checks whether two values are equal and returns a boolean result based on the condition. If both values are equal, the test passes otherwise it fails and raises an AssertionError with an optional error message. For
      3 min read

    • Python unittest - assertNotEqual() function
      assertNotEqual() in Python is a unittest library function that is used in unit testing to check the inequality of two values. This function will take three parameters as input and return a boolean value depending upon the assert condition. If both input values are unequal assertNotEqual() will retur
      2 min read

    Python Unittest None Assertions

    • Python unittest - assertIsNotNone() function
      assertIsNotNone() in Python is a unittest library function that is used in unit testing to check that input value is not None. This function will take two parameters as input and return a boolean value depending upon assert condition. If input value is not equal to None assertIsNotNone() will return
      2 min read

    • Python unittest - assertIsNone() function
      assertIsNone() in Python is a unittest library function that is used in unit testing to check that input value is None or not. This function will take two parameters as input and return a boolean value depending upon assert condition. If input value is equal to None assertIsNone() will return true e
      2 min read

    Python Unittest Identity Assertions

    • Python unittest - assertIn() function
      assertIn() in Python is a unittest library function that is used in unit testing to check whether a string is contained in other or not. This function will take three string parameters as input and return a boolean value depending upon the assert condition. If the key is contained in container strin
      2 min read

    • Python unittest - assertIsNot() function
      assertIsNot() in Python is a unittest library function that is used in unit testing to test whether first and second input value don't evaluate to the same object or not. This function will take three parameters as input and return a boolean value depending upon the assert condition. If both inputs
      2 min read

    Python Unittest Boolean Assertions

    • Python unittest - assertFalse() function
      assertFalse() in Python is a unittest library function that is used in unit testing to compare test value with false. This function will take two parameters as input and return a boolean value depending upon the assert condition. If test value is false then assertFalse() will return true else return
      2 min read

    • Python unittest - assertTrue() function
      assertTrue() in Python is a unittest library function that is used in unit testing to compare test value with true. This function will take two parameters as input and return a boolean value depending upon the assert condition. If test value is true then assertTrue() will return true else return fal
      2 min read

geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences