我在 python 3.6 中导入了 unittest,并像这样使用它:
class TestFunc(unittest.TestCase):
def test_half(self):
pass
def test_merge(self):
pass
def test_decrypt(self):
pass
def test_rank(self):
pass
if __name__ == "__main__":
print("printing before calling unittest")
unittest.main()
print("printing after calling unittest")
输出看起来像这样:
printing before calling unittest
....
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK
Process finished with exit code 0
第二次调用 print
即 print("printing after calling unittest")
,不执行。
为什么单元测试后我什么都做不了?测试后如何继续使用代码?
最佳答案
unittest docs解释一下:
By default
main
callssys.exit()
with an exit code indicating success or failure of the tests run.
sys.exit()
立即退出脚本,所以您的最后一行永远不会被调用。
您可以通过传递 exit=False
来避免此行为:
unittest.main(exit=False)
关于python - 单元测试后的命令不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50292618/