w3resource

Python unit test: Accurate floating-point calculation handling

Python Unit test: Exercise-6 with Solution

Write a Python unit test that checks if a function handles floating-point calculations accurately.

Sample Solution:

Python Code:

# Import the 'unittest' module for writing unit tests.
import unittest

# Define a test case class 'TestFloatingPointCalculations' that inherits from 'unittest.TestCase'.
class TestFloatingPointCalculations(unittest.TestCase):
    # Define a test method 'test_addition' to test floating-point addition.
    def test_addition(self):
        # Perform the addition operation.
        result = 0.3 + 0.5
        # Assert that the result is approximately equal to 0.8 within 6 decimal places.
        self.assertAlmostEqual(result, 0.8, places=6)

    # Define a test method 'test_multiplication' to test floating-point multiplication.
    def test_multiplication(self):
        # Perform the multiplication operation.
        result = 0.3 * 0.5
        # Assert that the result is approximately equal to 0.15 within 6 decimal places.
        self.assertAlmostEqual(result, 0.15, places=6)

    # Define a test method 'test_division' to test floating-point division.
    def test_division(self):
        # Perform the division operation.
        result = 0.7 / 0.3
        # Assert that the result is approximately equal to 2.333333 within 6 decimal places.
        self.assertAlmostEqual(result, 2.333333, places=6)

# Check if the script is run as the main program.
if __name__ == '__main__':
    # Run the test cases using 'unittest.main()'.
    unittest.main()

Sample Output:

Ran 3 tests in 0.001s
OK

Explanation:

In the above exercise,

  • TestFloatingPointCalculations class that inherits from unittest.TestCase. Inside the class, there are three test methods: test_addition(), test_multiplication(), and test_division().
  • In each test method, we perform a specific floating-point calculation and store the result in the result variable. We then use the self.assertAlmostEqual assertion to check if the result is approximately equal to the expected value with a given number of decimal places.

For example, in the test_addition method, we calculate the sum of 0.3 and 0.5 and compare it to the expected value of 0.8. We use self.assertAlmostEqual(result, 0.8, places=6) to assert that the result is approximately equal to 0.8 with a precision of up to 6 decimal places.

Flowchart:

Flowchart: Python - Accurate floating-point calculation handling.
Flowchart: Python - Accurate floating-point calculation handling.

Previous: Check file existence in a directory.
Next: Multi-threading handling verification.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/unittest/python-unittest-exercise-6.php