0

I am new to testing in python and I was trying to work on this code in Jupyter notebook, and it keeps on showing failed, ever if the cases are correct. But I tried to run the same code in Spyder and it worked alright and gave me the correct output.

tried running this code in jupyter notebook:

def add(n,m):
    return n+m
def mul(a,b):
    return a*b

import unittest
class DemoTest(unittest.TestCase): #child of TestCase
    
    def testadd(self):
        self.assertEqual(add(5,5),10) #when we pass, 5 n 5 it should return 10
    def testmul(self):
        self.assertEqual(mul(5,7),35)
if __name__=='__main__':
    unittest.main()

It gives me the error:

E
======================================================================
ERROR: C:\Users\ank (unittest.loader._FailedTest.C:\Users\ank)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'C:\Users\ank'

----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (errors=1)
An exception has occurred, use %tb to see the full traceback.

SystemExit: True

The same code when I ran in Spyder it works just fine and giving me the correct output:

..
----------------------------------------------------------------------
Ran 2 tests in 0.003s

OK

Is there something I need to fix in my Jupyter notebook?

Anki
  • 1
  • 2

2 Answers2

0

Anyway, it's no need to fix your jupyter notebook,
as the combination of jupyter and unittest's implmentation leads to unittest incorrect working in jupyter.


The following is added on the next day:
If you want one that can work in both Jupyter and Spyter (or others),
see answers of Unable to run unittest's main function in ipython/jupyter notebook,
which, despite its label python-2.7, is fully compatible with python3

lit
  • 11
  • 3
  • So is there no way that this can work in jupyter? – Anki Jul 12 '23 at 13:56
  • I'll give lit the benefit of the doubt that maybe they ran out of time to give a thorough answer. The problem you have having is that your implementation of `unittest.main()` is flawed. Unittest works in Jupyter and even JupyterLab. I need to put code, and so I'll add an answer expanding on this. – Wayne Jul 12 '23 at 15:03
  • Let me know if that same code I posted works in Spyder. I don't have it easily accessible to see if what works for the `unittest.main(argv=[''], verbosity=2, exit=False);` line works in Spyder or if you indeed need it different for each. – Wayne Jul 12 '23 at 15:10
  • @Wayne yeah surely it works!(in Spyder or somewhere else) – lit Jul 14 '23 at 08:32
  • Also, just now I've found there was already Q&A years ago https://stackoverflow.com/questions/37895781/unable-to-run-unittests-main-function-in-ipython-jupyter-notebook, (though with label `python-2.7`, it can fully apply to python-3) – lit Jul 14 '23 at 08:46
0

Unittest works in a Jupyter .ipynb and even run inside JupyterLab. I'm testing and seeing my variation on your code working there fine. I'm basing my variation on this answer here for the post 'Unit tests for functions in a Jupyter notebook?'. It just seemed your implementation of the unittest.main() was flawed.

This is a single cell in a notebook works well:

def add(n,m):
    return n+m
def mul(a,b):
    return a*b

import unittest
class DemoTest(unittest.TestCase): #child of TestCase
    
    def testadd(self):
        self.assertEqual(add(5,5),10) #when we pass, 5 n 5 it should return 10
    def testmul(self):
        self.assertEqual(mul(5,7),35)
if __name__=='__main__':
    unittest.main(argv=[''], verbosity=2, exit=False);

The only thing I changed was the last line, where OP had unittest.main().

Wayne
  • 6,607
  • 8
  • 36
  • 93