|
|
逼格高的饭盒 · 使用Django向手机发送短信_向手机发送短 ...· 1 年前 · |
|
|
曾深爱过的咖啡 · C语言学习笔记——结构体和共用体-阿里云开发者社区· 2 年前 · |
|
|
想出国的勺子 · javascript - ...· 2 年前 · |
|
|
会搭讪的熊猫 · unique_ptr reset ...· 2 年前 · |
编辑
所以我又试了一次,使用了一个名为
test2.py
的新文件,它可以工作。我打包了
repoman
,
test.py
在
src
文件夹中。在创建和安装了我的
test.py
之后,我修改了
repoman egg
。我想这就是问题所在。但是谢谢你的帮助。你们认为这就是真正的原因吗?
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()
运行
python test.py
会得到以下错误:
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
我不认为我需要覆盖
__init__
函数,对吗?是什么引起的?为什么没有定义
self
?我已经声明了我的超类
unittest.TestCase
谢谢。
我基本上是从官方示例: 单元测试-基本示例 中学到的。
我不认为你展示的是被执行的实际代码。
我和其他人认为,有几个原因:
self.assertTrue(r.ok)
失败,那么前面的行也会失败。因此,
self.assertTrue(r.ok)
不会执行。(正如
https://stackoverflow.com/questions/9541397/name-self-is-not-defined-when-doing-an-unittest#comment12090465_9541397
所说)
我要说的是,你可能犯了这样的错误:
def test_scm_permission(self):
and wrote something here that's 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
有一个类似于你的追踪器。
注意:如果我没有计算错的话, 也会出现在第18行,而不是第19行(在跟踪中显示的数字)。
这是大卫·赫弗南的评论的重新措辞。
您发布的代码不可能是该跟踪的原因。
考虑代码中的这两行:
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
,我们就无法通过第一行。
因此,您发布的代码不是您运行的代码。
我不知道问题是从哪里来的--是复制错误,还是执行了错误的test.py更新:还是一些混合的制表符和空格问题,我永远找不出它们何时被标记,什么时候没有--但根本原因几乎肯定是缩进错误。
请注意,错误消息是
NameError: name 'self' is not defined
而不是
NameError: global name 'self' is not defined
@Rik Poggi得到的。如果您在/向上移动
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
|
|
曾深爱过的咖啡 · C语言学习笔记——结构体和共用体-阿里云开发者社区 2 年前 |