作者:七仙女跳皮筋 | 来源:互联网 | 2022-12-24 14:40
我试图理解以下python代码片段,取自SublimeText3插件开发单元测试示例.
def test_delayed_insert(self):
sublime.set_timeout(
lambda: self.view.run_command("delayed_insert_hello_world"),
100)
# `delayed_insert_hello_world` will be execulated after the timeout
# `yield 1000` will yield the runtime to main thread and continue
# the execution 1 second later
yield 1000
row = self.getRow(0)
self.assertEqual(row, "hello world")
这怎么办?如果unittest不支持这个(将测试称为生成器),代码将不会被执行,对吧?我目前的理解是unittest框架采用屈服值'x',并将其自身暂停为'x'ms,在这种情况下为1000ms,类似于c ++线程中yield/sleep的语义.但是,这取决于'unittest'以这种方式真正使用产生的价值.
如果确实如此,为什么这个测试不会失败(在st3 = python 3.3.6和python 3.5.2中尝试过)?
from unittest import TestCase
class TestMe(TestCase):
def test_failtest(self):
self.assertTrue(False)
yield 0
运行它给出:
~$ python3 -m unittest test_me.py --verbose
test_failtest (test_me.TestMe) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
DeepSpace..
5
TL; DR您链接的代码是子类化自定义TestCase
类(DeferrableTestCase
),这可能会改变"正常"行为unittest
.
让我们退后一步,不要使用unittest
.
def foo():
assert True is False
foo()
这提高了AssertionError
预期.然而,
def foo():
assert True is False
yield
foo()
才不是.
原因是yield
关键字转向foo
生成器.为了获得,AssertionError
我们将不得不使用foo
返回的生成器:
def foo():
assert True is False
yield
next(foo())
File "main.py", line 48, in
next(foo())
File "main.py", line 45, in foo
assert True is False
AssertionError
此行为是继承到生成器,与如何unittest
或assert
工作无关:
def foo():
1/0
yield
gen = foo()
print('No exception yet')
next(gen)
输出
'No exception yet'
Traceback (most recent call last):
File "main.py", line 50, in
next(gen)
File "main.py", line 45, in foo
1/0
ZeroDivisionError: division by zero
但是,您链接的代码是子类化自定义TestCase
类(DeferrableTestCase
),这可能会改变行为.
1> DeepSpace..:
TL; DR您链接的代码是子类化自定义TestCase
类(DeferrableTestCase
),这可能会改变"正常"行为unittest
.
让我们退后一步,不要使用unittest
.
def foo():
assert True is False
foo()
这提高了AssertionError
预期.然而,
def foo():
assert True is False
yield
foo()
才不是.
原因是yield
关键字转向foo
生成器.为了获得,AssertionError
我们将不得不使用foo
返回的生成器:
def foo():
assert True is False
yield
next(foo())
File "main.py", line 48, in
next(foo())
File "main.py", line 45, in foo
assert True is False
AssertionError
此行为是继承到生成器,与如何unittest
或assert
工作无关:
def foo():
1/0
yield
gen = foo()
print('No exception yet')
next(gen)
输出
'No exception yet'
Traceback (most recent call last):
File "main.py", line 50, in
next(gen)
File "main.py", line 45, in foo
1/0
ZeroDivisionError: division by zero
但是,您链接的代码是子类化自定义TestCase
类(DeferrableTestCase
),这可能会改变行为.