title: Why does assertRaises produce an ERROR in one case and not in the other?
body: <p>I've searched through StackOverflow Q&amp;As on assertRaises to identify what I'm doing wrong. As I still don't understand what the problem is, I wanted to pose my own question to get a specific answer.</p>
  <p>I have two different calculator programmes for which I have created unit tests with the unittest module. The unit test for programme A works with assertRaises whereas using assertRaises in B produces an ERROR.</p>
  <p>What is the difference between A and B with reference to assertRaises?</p>
  <p><strong>A code:</strong></p>
  <pre><code>import unittest
  
  class Calculator:       
      def __init__(self):
          pass
  
      def add(self, a, b):
          return a + b
      def sub(self, a, b):
          return a - b
      def mul(self, a, b):
          return a * b
      def div(self, a, b):
          if b == 0:
              raise ZeroDivisionError(&quot;The divisor must not be zero.&quot;)
          else:
              return a / b
  
  class TestCalculator(unittest.TestCase):  
      def test_add(self):
             #Test case function for addition
          self.calc = Calculator()
          result = self.calc.add(4, 7)
          expected = 11
          self.assertEqual(result, expected)
  
      def test_sub(self):
             #Test case function for subtraction
          self.calc = Calculator()
          result = self.calc.sub(10, 5)
          expected = 5
          self.assertEqual(result, expected)
  
      @unittest.skip('Some reason')
      def test_mul(self):
             #Test case function for multiplication
          self.calc = Calculator()
          result = self.calc.mul(3, 7)
          expected = 21
          self.assertEqual(result, expected)
  
      def test_div(self):
             #Test case function for division
          self.calc = Calculator()
          result = self.calc.div(10, 2)
          expected = 5
          self.assertEqual(result, expected)
          self.assertRaises(ZeroDivisionError, self.calc.div, 10, 0)   # ??
      
  
  unittest.main(argv=[''], verbosity=2, exit=False)   
  </code></pre>
  <p><strong>B code:</strong></p>
  <pre><code>import unittest 
  
  # Creates unit tests for a calculator that only summarises numbers.                       
  class CalculatorTestCase(unittest.TestCase):  
     
      def test_calculator_returns_correct_sum(self): # Checks that the calculator works properly.   
          self.assertEqual(calculator(1, 3), 4, msg = &quot;Inaccurate Result&quot;)
          self.assertEqual(calculator(1, -3), -2, msg= &quot;Inaccurate Result&quot;)
  
   
      def test_calculator_rejects_invalid_arguments(self):   # Wants the calculator to raise TypeError when the input values for num1 and num2 are anything but two integers.
          self.assertRaises(TypeError, calculator(&quot;one&quot;, &quot;two&quot;))
          self.assertRaises(TypeError, calculator(1, &quot;two&quot;))
          self.assertRaises(TypeError, calculator(&quot;one&quot;, 2))
          self.assertRaises(TypeError, calculator(1.0, &quot;two&quot;))
          self.assertRaises(TypeError, calculator(&quot;one&quot;, 2.0))
          self.assertRaises(TypeError, calculator(1.0, 2.0))
          self.assertRaises(TypeError, calculator(1, 2.0))
          self.assertRaises(TypeError, calculator(1.0, 2))
  
                            # The calculator #
  def calculator(num1, num2): 
      if isinstance(num1, int) and isinstance(num2, int): # summarise num1 and num2 only if both arguments are integers...
          return num1+num2
      else:
          raise TypeError(&quot;Arguments num1 and num2 must be integers.&quot;)
  # ... otherwise &quot;TypeError&quot; is raised and this will be in line with the test_calculator_rejects_invalid_argument test.
  
  if __name__ == &quot;__main__&quot;:
      num1 = 1
      num2 = 3
      print(calculator(num1, num2))
      #unittest.main()
      unittest.main(argv=[''], verbosity=2, exit=False) 
  </code></pre>
  <p>Why does A work without ERRORS whereas B produces the following output:</p>
  <pre><code>test_calculator_rejects_invalid_arguments (__main__.CalculatorTestCase) ... ERROR
  test_calculator_returns_correct_sum (__main__.CalculatorTestCase) ... ok
  
  ======================================================================
  ERROR: test_calculator_rejects_invalid_arguments (__main__.CalculatorTestCase)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File &quot;c:\Users\XXXXX\.vscode\YY_ZZ\Calculator_project\calculator.py&quot;, line 12, in test_calculator_rejects_invalid_arguments
      self.assertRaises(TypeError, calculator(&quot;one&quot;, &quot;two&quot;))
    File &quot;c:\Users\XXXXX\.vscode\YY_ZZ\Calculator_project\calculator.py&quot;, line 26, in calculator
      raise TypeError(&quot;Arguments num1 and num2 must be integers.&quot;)
  TypeError: Arguments num1 and num2 must be integers.
  
  ----------------------------------------------------------------------
  Ran 2 tests in 0.001s
  
  FAILED (errors=1)
  </code></pre>