Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem in using unittest
#1
hi
I have the two below codes:
#one.py
def add(x,y):
    return x+y
    
def subtract(x,y) :
    return x-y
    
def   multiply(x,y):
    return x*y
    
def division(x,y) :
    if y==0 :
        raise ZeroDIvisionErorr ("can't division by zero...")   
    return x/y
    
and the below test_one.py:
#test_one.py
'''
for test one.py with using unittest.
'''

import unittest
import one

class oneTest(unittest.TestCase):
    def test_add(self):
        self.assertEqual(one.add(4,5),9)
        self.assertEqual(one.add(-1,4),3)
        
    def test_subtract(self):
        self.assertEqual(one.subtract(5,0),5)
        
    def test_multiply(self):
        self.assertEqual(one.multiply(4,5),20)   
        self.assertEqual(one.multiply(7,0),0)
        
    def   test_division(self):
        self.assertEqual(one.division(30,5),6)
        self.assertRaises(ZeroDivisionError,one.division,4,0)
        
        
if __name__=='__main__'  :
     unittest.main
after that, when I wrote in cmd: D:\Desktop\pypypy>python -m unittest test_one.py
the below error was appeared:
Error:
.E.. ====================================================================== ERROR: test_division (test_one.oneTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "D:\Desktop\pypypy\test_one.py", line 23, in test_division self.assertRaises(ZeroDivisionError,one.division,4,0) File "C:\Program Files (x86)\Thonny\lib\unittest\case.py", line 738, in assertRaises return context.handle('assertRaises', args, kwargs) File "C:\Program Files (x86)\Thonny\lib\unittest\case.py", line 201, in handle callable_obj(*args, **kwargs) File "D:\Desktop\pypypy\one.py", line 12, in division raise ZeroDIvisionErorr ("can't division by zero...") NameError: name 'ZeroDIvisionErorr' is not defined ---------------------------------------------------------------------- Ran 4 tests in 0.004s FAILED (errors=1) D:\Desktop\pypypy>'''
what is the problem?
thanks
Reply
#2
(Feb-25-2024, 11:54 AM)akbarza Wrote: what is the problem?
ZeroDIvisionErorr is different from ZeroDivisionError Exclamation
akbarza likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
ZeroDIvisionError is not defined. Carefully check the case.
akbarza likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Unwanted execution of unittest ThomasFab 9 2,073 Nov-15-2022, 05:33 PM
Last Post: snippsat
  unittest.mock for an api key silver 3 1,397 Aug-29-2022, 03:52 PM
Last Post: ndc85430
  Ran 0 tests in 0.000s - unittest Peaches 8 5,124 Dec-31-2021, 08:58 AM
Last Post: Peaches
Sad Problem with Unittest mhanusek 1 3,769 Nov-12-2020, 04:58 PM
Last Post: Gribouillis
  Unittest et patch mad31 2 2,139 Aug-09-2020, 06:16 AM
Last Post: mad31
  Unusual things to do with unittest/HTMLTestRunner AndyHolyer 0 2,148 Jul-29-2020, 02:43 PM
Last Post: AndyHolyer
  Test a class function via "unittest " Penguin827 1 1,625 Jul-10-2020, 08:31 AM
Last Post: Gribouillis
  How to use unittest module ? binhduonggttn 1 2,053 Feb-17-2020, 03:28 AM
Last Post: Larz60+
  unittest mock and patch piscvau 1 2,116 Nov-08-2019, 03:23 PM
Last Post: ichabod801
  Optimize unittest loading Nazz 3 2,553 Mar-05-2019, 11:59 AM
Last Post: Nazz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020