unittest 框架可以发现嵌套测试吗?
Can the unittest framework discover nested tests?
unittest
是否可以发现以下嵌套结构?
class HerclTests(unittest.TestCase):
def testJobs(self):
def testJobSubmit():
jid = "foobar"
assert jid,'hercl job submit failed no job_id'
return jid
def testJobShow(jid):
jid = "foobar"
out,errout=bash(f"hercl job show --jid {jid} --form json")
assert 'Job run has been accepted by airflow successfully' in out,'hercl job show failed'
这是尝试 运行 unittest
时的错误:
============================= test session starts ==============================
platform darwin -- Python 3.6.7, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 -- /Users/steve/git/hercl/.venv/bin/python
cachedir: .pytest_cache
rootdir: /Users/steve/git/hercl/tests
collecting ... collected 0 items
ERROR: not found: /Users/steve/git/hercl/tests/hercl_flow_test.py::HerclTests::testJobs::testJobSubmit
(no name '/Users/steve/git/hercl/tests/hercl_flow_test.py::HerclTests::testJobs::testJobSubmit' in any of [<TestCaseFunction testJobs>])
============================ no tests ran in 0.01s =============================
是否可以调整此结构以与 unittest
一起使用,或者是否必须将每个 test
方法提升到 HerclTests
class 的级别?
这行不通 - 在另一个函数(“内部函数”)内定义的函数仅作为外部函数局部范围内的变量“存在”。任何其他代码都无法访问它们。 unittest 发现不会找到它们,即使它知道它们也无法调用它们。
unittest
是否可以发现以下嵌套结构?
class HerclTests(unittest.TestCase):
def testJobs(self):
def testJobSubmit():
jid = "foobar"
assert jid,'hercl job submit failed no job_id'
return jid
def testJobShow(jid):
jid = "foobar"
out,errout=bash(f"hercl job show --jid {jid} --form json")
assert 'Job run has been accepted by airflow successfully' in out,'hercl job show failed'
这是尝试 运行 unittest
时的错误:
============================= test session starts ==============================
platform darwin -- Python 3.6.7, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 -- /Users/steve/git/hercl/.venv/bin/python
cachedir: .pytest_cache
rootdir: /Users/steve/git/hercl/tests
collecting ... collected 0 items
ERROR: not found: /Users/steve/git/hercl/tests/hercl_flow_test.py::HerclTests::testJobs::testJobSubmit
(no name '/Users/steve/git/hercl/tests/hercl_flow_test.py::HerclTests::testJobs::testJobSubmit' in any of [<TestCaseFunction testJobs>])
============================ no tests ran in 0.01s =============================
是否可以调整此结构以与 unittest
一起使用,或者是否必须将每个 test
方法提升到 HerclTests
class 的级别?
这行不通 - 在另一个函数(“内部函数”)内定义的函数仅作为外部函数局部范围内的变量“存在”。任何其他代码都无法访问它们。 unittest 发现不会找到它们,即使它知道它们也无法调用它们。