0

I'm trying to set some tests for my server.

According to a friend's recommendation I wanna use unittest and mock. For this purpose I wrote some lines but when I try to execute the script I got an importError.

Traceback (most recent call last):
  File "test_creds.py", line 146, in <module>
    unittest.main()       
AttributeError: 'module' object has no attribute 'main'

Do I have some mistake with the imports?

Thanks!

# coding=utf-8

import sys, os
import unittest, mock, kiss

from twisted.python import log
from twisted.trial import unittest
from twisted.cred import credentials

class CredentialsChecker(unittest.TestCase):

    """
    Testing the server credentials handling
    """

    def _setUp_databases(self):           
        username_1 = 'user_name'
        password_1 = 'user_pass'
        email_1 = 'user@mail.org'

        create_user_profile = mock.Mock()

        self.db = mock.Mock()

        self.db.username = username_1
        self.db.password = password_1
        self.db.email = email_1

    def setUp(self):
        log.startLogging(sys.stdout)

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Flushing database")
        #management.execute_from_command_line(['manage.py', 'flush',\
#'--noinput'])

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Populating database")
        #management.execute_from_command_line(['manage.py', 'createsuperuser',\
#'--username', 'superuser_name', '--email', 'superuser_name@email.org',\
#'--noinput'])
        self._setUp_databases()

        log.msg(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Running tests")
        return

    """
    Log in with valid credentianls. The server should return True
    """
        def test_GoodCredentials(self):

        creds = credentials.UsernamePassword('user_name', 'user_pass')
        checker = DjangoAuthChecker()
        d = checker.requestAvatarId(creds)

if __name__ == '__main__':
    unittest.main()    
theB
  • 6,450
  • 1
  • 28
  • 38
Samuel Góngora
  • 141
  • 1
  • 11
  • 1
    Is your file named `unittest.py`? Do you have such a file lying around somewhere? – Kevin Aug 10 '15 at 17:35
  • on what line do you get the error? – David Mašek Aug 10 '15 at 17:35
  • Open Python interpreter. Type `import unittest;unittest.main`. Does it print ``? I think you have shadowed unittest improperly with your directory structure. – Two-Bit Alchemist Aug 10 '15 at 17:35
  • @DavidMašek The only place main appears in the program is the `unittest.main()` call so it's gotta be a problem with that, unless OP has provided totally the wrong file. – Two-Bit Alchemist Aug 10 '15 at 17:37
  • @Kevin No, the name of the file is "test_creds.py" – Samuel Góngora Aug 10 '15 at 17:39
  • @DavidMašek line 146, I just edit the question. Thanks for the remark. – Samuel Góngora Aug 10 '15 at 17:41
  • @Two-BitAlchemist Yep, I get 'unittest.main.TestProgram'. – Samuel Góngora Aug 10 '15 at 17:42
  • @Two-BitAlchemist Ok, but he really should write where does the error occur unless it's obvious at first look. And it could be called from some function/constructor. I was mainly asking because the last line looks like it should work. – David Mašek Aug 10 '15 at 17:42
  • @DavidMašek Agreed just trying to help you spot it since it wasn't obvious. Samuel, did you run that from the same directory as you're running the script? Using the same Python interpreter? Before the `unittest.main()` line, write `import pdb;pdb.set_trace()`. When it runs the file, and drops you into the debugger, try `print type(unittest.main)` and `print type(unittest)`. – Two-Bit Alchemist Aug 10 '15 at 17:46
  • @Two-BitAlchemist Yes, I'm running the script from the same directory, just typing `python test_creds.py`. The debugger gave me the following output. `(Pdb) print type(unittest.main) *** AttributeError: 'module' object has no attribute 'main' (Pdb) print type(unittest) ` – Samuel Góngora Aug 10 '15 at 17:53
  • @SamuelGóngora Somehow you have put another module in the way of the traditional `unittest` one and you're importing that instead. You have some file or folder in your search path called `unittest` so you have to find it and remove it. – Two-Bit Alchemist Aug 10 '15 at 17:57
  • @Two-BitAlchemist Problem solved! I replaced `from twisted.trial import unittest` with `import unittest` and finally works! Thanks for the help. – Samuel Góngora Aug 10 '15 at 18:11

1 Answers1

0

If you create an object inheriting from the class unittest.TestCase test you have to use the native Python unittest module.

Samuel Góngora
  • 141
  • 1
  • 11