The name "I" is not determined when performing unittest?

Edit

So, I tried again with a new file called test2.py, and it works. I packed it repoman, but it test.py’s in the folder src. I changed test.pyafter I created and installed mine repoman egg. I think the problem. But thanks for the help. Do you guys think 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.pyI 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 rewrite the function __init__, right? What causes this? Why selfnot defined? I already announced my superclassunittest.TestCase

Thank.

I basically learned this from an official example: Unittest - Basic example

+5
source share
5 answers

, , , , .

, :

  • self.assertTrue(r.ok) , . self.assertTrue(r.ok) . ( )
  • .

, , , :

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

, , .

:

# test.py
class MyClass:

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


obj = MyClass()
obj.func()

:

$ 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

, .

: , , self.assertTrue(r.ok) 18 19 ( , ).

+6

, : , test.py [: , , , ] - .

,

NameError: name 'self' is not defined

NameError: global name 'self' is not defined

@ . , , self.assertTrue /:

~/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

~/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
+7

.

, , .

:

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

(NameError: name 'self' is not defined)   (self.assertTrue(r.ok)). , self. self , .

, , , .

+2

, , , . , - . :

import unittest
import requests

class TestWriteFile(unittest.TestCase):
    def setup(self):
        self.username = 'dummy'

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

( ); setup setUp ( U). , self.username test_scm_permission, python . - , , , .

0

I had the same problem, but not with myself. It was an ordinary variable that defined the string before the error occurred.

Apparently this was due to ... mixin tabs and spaces.

I replaced all the tabs with 4 spaces, and the problem disappeared.

For some vague reason, instead of the traditional indented error, I had this one.

0
source

All Articles