Skip to main content

Featured

Introduction to Finance, Accounting, Modeling and Valuation

  What you'll learn #1 Best Selling Accounting Course on Udemy (Learn Finance and Accounting the Easy Way)!​ Analyze and understand an income statement (even if you have no experience with income statements). Analyze and understand a balance sheet (even if you have no experience with balance sheets). The Foundation: An Introduction to Accounting and Business Every business transaction—from buying inventory to selling a product—must be recorded. This is the realm of accounting . Often referred to as the "language of business ," accounting is a systematic process that involves identifying, measuring, and communicating financial information to users, enabling them to make informed judgments and decisions. What is Accounting? A Deeper Dive At its core, accounting is about providing a clear, accurate, and standardized picture of a company’s financial activity. The ultimate goal is to generate financial statements that are used by various stakeholders, including owners, mana...

Exclusive DEALS

Unit Testing A Practical Guide

 Unit Testing: A Practical Guide

Unit Testing A Practical Guide


Here's a Python function that checks whether a word is a palindrome.
Add 10 different unit tests for this snippet with comments. Follow up with a short explanation of what was done.

      def is_palindrome(word):
  """
  Checks whether a word is a palindrome.

  Args:
    word: The word to check.

  Returns:
    True if the word is a palindrome, False otherwise.
  """

  # Convert the word to lowercase and remove non-alphanumeric characters.
  word = ''.join(char.lower() for char in word if char.isalnum())

  # Check if the word is the same forwards and backwards.
  return word == word[::-1]
 _________________________________________________ 
      def is_palindrome(word):
  """
  Checks whether a word is a palindrome.

  Args:
    word: The word to check.

  Returns:
    True if the word is a palindrome, False otherwise.
  """

  # Convert the word to lowercase and remove non-alphanumeric characters.
  word = ''.join(char.lower() for char in word if char.isalnum())

  # Check if the word is the same forwards and backwards.
  return word == word[::-1]

# Unit Tests
import unittest

class TestIsPalindrome(unittest.TestCase):

  def test_empty_string(self):
    """Test with an empty string."""
    self.assertTrue(is_palindrome(""))

  def test_single_character(self):
    """Test with a single character."""
    self.assertTrue(is_palindrome("a"))

  def test_palindrome(self):
    """Test with a palindrome word."""
    self.assertTrue(is_palindrome("racecar"))

  def test_not_palindrome(self):
    """Test with a non-palindrome word."""
    self.assertFalse(is_palindrome("hello"))

  def test_case_insensitive(self):
    """Test case-insensitive palindrome."""
    self.assertTrue(is_palindrome("Racecar"))

  def test_with_spaces(self):
    """Test with spaces."""
    self.assertTrue(is_palindrome("A man, a plan, a canal: Panama"))

  def test_with_punctuation(self):
    """Test with punctuation."""
    self.assertTrue(is_palindrome("madam, I'm Adam!"))

  def test_with_numbers(self):
    """Test with numbers."""
    self.assertTrue(is_palindrome("12321"))

  def test_with_special_characters(self):
    """Test with special characters."""
    self.assertFalse(is_palindrome("!@#$%^"))

  def test_with_mixed_characters(self):
    """Test with mixed characters."""
    self.assertTrue(is_palindrome("A Toyota's a Toyota"))

if __name__ == '__main__':
  unittest.main()
    

Explanation:

1-Import unittest:

We start by importing the unittest module,

which provides tools for writing and running unit tests in Python.

2-Create a Test Class:

We define a class TestIsPalindrome that
inherits from unittest.TestCase. This class will contain our test methods.

3-Test Methods:

We create several methods, each representing a
different test case. Each method name starts with "test_" to indicate
that it's a test method.

4-Assertions:

Inside each test method, we use assertions from
the unittest.TestCase class to check if the expected outcome is achieved.

  • self.assertTrue(is_palindrome(word)):

This asserts that the is_palindrome function returns True for the given
input word.

  • self.assertFalse(is_palindrome(word)):

This asserts that theis_palindrome function returns False for the given
input word.

5-Run Tests:

The if __name__ == '__main__': block ensures that
the tests are run only when the script is executed directly. unittest.main()
runs all the test methods defined in the TestIsPalindrome class.

The tests cover various scenarios, including:

  • Empty string
  • Single character
  • Palindrome word
  • Non-palindrome word
  • Case-insensitive input
  • Input with spaces
  • Input with punctuation
  • Input with numbers
  • Input with special characters
  • Input with mixed characters

This comprehensive set of tests ensures that the
is_palindrome function behaves correctly in various situations, promoting code
reliability.

Comments