1

This is the code:

# mean_var-_std.py
def calculate(list):
    if len(list) < 9:
        print("List must contain nine numbers.")
        raise ValueError
    else:
        return 0

This is the unit-test:

import unittest
import mean_var_std


# the test case
class UnitTests(unittest.TestCase):
    def test_calculate_with_few_digits(self):
        self.assertRaisesRegex(ValueError, "List must contain nine numbers.", mean_var_std.calculate, [2,6,2,8,4,0,1,])

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

This is the error that I get:

F
======================================================================
FAIL: test_calculate_with_few_digits (test_module.UnitTests)
----------------------------------------------------------------------
ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/fcc-mean-var-std-2/test_module.py", line 8, in test_calculate_with_few_digits
    self.assertRaisesRegex(ValueError, "List must contain nine numbers.", mean_var_std.calculate, [2,6,2,8,4,0,1,])
AssertionError: "List must contain nine numbers." does not match ""

----------------------------------------------------------------------
Ran 1 test in 0.004s

FAILED (failures=1)

I don't understand what AssertionError: "List must contain nine numbers." does not match "" means. How can I solve this problem? Thanks in advance.

2 Answers 2

2

Your function raises an error without error message. It also prints a message but that’s unrelated (and generally seen as bad practice).

Remove the print statement and raise an error with an error message to fix the unit test failure:

def calculate(list):
    if len(list) < 9:
        raise ValueError("List must contain nine numbers.")
    else:
        return 0
Sign up to request clarification or add additional context in comments.

Comments

0

Here's a small modification in the code.

def calculate(list):
    if len(list) < 9:
        print("List must contain nine numbers.")
        raise ValueError("List must contain nine numbers.")
    else:
        return 0

Since you're looking for ValueError, the unittest will not look at your print statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.