Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

name 'self' is not defined when doing an unittest?

Edit

So I did try again, with a new file called test2.py and it works. I packaged repoman , and test.py is in the src folder. I modified test.py after I created and installed my repoman egg. I think that's the problem. But thanks for the help. Do you guys think that's the exact reason?


import unittest
import requests
from repoman.core import ultraman, supported
from repoman.ext import writefile,locate_repo

class TestWriteFile(unittest.TestCase):

    def setUp(self):
        self.username = 'dummy'
        self.password = 'dummy'
        self.remote   = 'http://192.168.1.138:6666/scm/hg/NCL'

    def test_scm_permission(self):
        """
        Test SCM login.
        """
        r = requests.get("http://192.168.1.138:6666/scm/", auth=(self.username, self.password))
        self.assertTrue(r.ok)

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

Running python test.py I get this error:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    class TestWriteFile(unittest.TestCase):
  File "test.py", line 19, in TestWriteFile
    self.assertTrue(r.ok)
NameError: name 'self' is not defined

I don't think I need to overwrite __init__ function, do I? What's causing this? Why is self not defined? I already declared my superclass unittest.TestCase

Thanks.

I basically learned it from the official sample: Unittest - Basic Example

like image 880
CppLearner Avatar asked Mar 02 '12 22:03

CppLearner


2 Answers

I'm not sure where the problem is coming from -- whether it's a copying error or the wrong test.py is being executed [update: or some mixed tabs-and-spaces issue, I can never figure out when those get flagged and when they don't] -- but the root cause is almost certainly an indentation error.

Note that the error message is

NameError: name 'self' is not defined

and not

NameError: global name 'self' is not defined

which @Rik Poggi got. This is exactly what happens if you move the self.assertTrue one level in/up:

~/coding$ cat test_good_indentation.py
import unittest

class TestWriteFile(unittest.TestCase):
    def test(self):
        """
        Doc goes here.
        """
        self.assertTrue(1)

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

~/coding$ python test_good_indentation.py 
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

versus

~/coding$ cat test_bad_indentation.py
import unittest

class TestWriteFile(unittest.TestCase):
    def test(self):
        """
        Doc goes here.
        """
    self.assertTrue(1)

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

~/coding$ python test_bad_indentation.py 
Traceback (most recent call last):
  File "test_bad_indentation.py", line 3, in <module>
    class TestWriteFile(unittest.TestCase):
  File "test_bad_indentation.py", line 8, in TestWriteFile
    self.assertTrue(1)
NameError: name 'self' is not defined
like image 76
DSM Avatar answered Nov 14 '22 21:11

DSM


I don't think that what you showed is the actual code that get executed.

I and others belive that, for a couple of reasons:

  • If self.assertTrue(r.ok) fails then the line before will too. Therefore self.assertTrue(r.ok) won't execute. (as David Heffernan said)
  • And because your code looks fine.

I'd say that you probably made a typo of this kind:

def test_scm_permission(self):
                         ^
                         |
         and wrote something here that's not self

In some file that get executed instead of the one you're showing.

Take a look at this example:

# test.py
class MyClass:

    def func(sel):    # typo error here
        self.name = 10


obj = MyClass()
obj.func()

And when I tried to run:

$ python3 test.py 
Traceback (most recent call last):
  File "test.py", line 8, in <module>
    obj.func()
  File "test.py", line 4, in func
    self.name = 10
NameError: global name 'self' is not defined

Having a traceback similar to yours.

Note: Also if I'm not counting wrong self.assertTrue(r.ok) is on line 18, instead of line 19 (which is the number showed in your traceback).

like image 27
Rik Poggi Avatar answered Nov 14 '22 21:11

Rik Poggi