Issue with test environment while testing my program?

My function mean_var_std_calculate accepts a 9 number digits list and calculates mean,var,std values and return them in a dictionary format. it should raise ValueError when I pass a list less than 9 digits. it returns correct result when I run it independently in replit editor, and raises ValueError message when I pass a list having less than 9 digits as expected. But when I test it using test_module.py (by default provided by Replit system ) my program passes first two tests (with 9 digits list) but fails in passing third test with less than 9 digits list with an error self.assertRaisesRegex(ValueError, "List must contain nine numbers.", mean_var_std.calculate, [2,6,2,8,4,0,1,]) , AssertionError : ValueError not raised by calculate. By the way I have already added code in my program with try/except block to raise ValueError and it works perfectly. Can anybody help? (Sorry for long question)

I receive following error when I run my program to test it with test_module.py

FAIL: test_calculate_with_few_digits (test_module.UnitTests)
      self.assertRaisesRegex(ValueError, "List must contain nine numbers.",
      AssertionError: ValueError not raised by calculate
Ran 3 tests in 0.003s
FAILED (failures=1)

Below is rough template of my function

def calculate(mylist):
  try:
    if len(mylist) < 9:
      raise ValueError("List must contains nine numbers")
  ## rest of the code below
#  return value
except ValueError as e:
    print(e)
1 Like

6 days have passed and no body answered my question. If you need any doc link please feel free to ask. I am desperately waiting any answer to resolve this issue.

If you catch the error, then as far as the tester is concerned, no error was raised. So the solution here would be to just remove the try-except block.

1 Like

If I remove the try/except block then my project will not fulfill the requirement. Raising error with less than nine digits is the requirement of the project. When I run the program with out test_module.py it works perfectly and raise error when I pass list less than nine digits. But it fails in third test when I run it along with test_module.py.

Here is link of my code
https://replit.com/@SuhailAsghar/boilerplate-mean-variance-standard-deviation-calculator#mean_var_std.py

Link of test_module.py
https://replit.com/@SuhailAsghar/boilerplate-mean-variance-standard-deviation-calculator#test_module.py

Which, by the way python works, you are not doing currently. While yes, the error is thrown, it is then ignored. Which is why the tester counts it as a fail, because the error was handled, not raised and left unchecked.

You can raise errors outside of a try-except block, it’ll just full-stop the program if you do.

1 Like

I raised the error outside the try/except block with print method. it works fine with out test_module.py as it worked with try/except block, but fails in third test along without try/except block. gives same error message.

I re-raise the error with try-except block. it is testing all three examples as expected. Thanks for your help.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.