0

I'm trying to use the "unittest" module in Python version 3.

I tried to verify if it's correctly installed with:

try:
   import unittest
   print("imported")
except ImportError as e:
   print("no")

The module will be imported correctly, but, if I try to access to its own methods including "main()" and "TestCase" will be raised the exception:

Command run: python3 -m testunit.py

testunit.py:

    import unittest
import calculation



class TestCalc(unittest.TestCase):
    def test_add(self):
        result = calculation.add(10,50)
        self.assertEquals(result,60)

if __name__ == '__main__':
    unittest.main(defaultTest='suite')

Error given:

AttributeError: module 'unittest' has no attribute 'main'

Others results:

If I try to run the command python3 -m unittest it should give as output "RAN 0 tests in 0s", but no.

No output will be given.

In a nutshell the module is correctly installed but seems to not working at all.

Stacktrace given from the error:

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 183, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py", line 109, in _get_module_details
    __import__(pkg_name)
  File "/Users/istorn/testunit.py", line 3, in <module>
Istorn
  • 485
  • 1
  • 5
  • 23
  • 3
    It sounds like you named a file `unittest.py`, or named a folder `unittest`. Don't do that. – user2357112 Apr 25 '20 at 10:50
  • @user2357112supportsMonica thanks, you saved me. If you make it as answer I believe that it could be helpful for more people. – Istorn Apr 25 '20 at 10:52
  • 1
    Also, unittest comes with Python, so **don't try to install `unittest`**. You'll get some other weird thing if you try. – user2357112 Apr 25 '20 at 10:52

0 Answers0