python - 单元测试后的命令不执行

标签 python unit-testing execution

我在 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

第二次调用 printprint("printing after calling unittest"),不执行。 为什么单元测试后我什么都做不了?测试后如何继续使用代码?

最佳答案

unittest docs解释一下:

By default main calls sys.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/

相关文章:

python - 读取文本文件中的行并将其转换为字符串列表

javascript - ( react 原生): Execution failed for task ':app:generatePackageList'

c - 程序运行时的近似公式

linux - 在 Linux 中执行文件的顺序以及如何更改它

python - Odoo 以编程方式在 python 中为采购订单添加税费

python - 查找 n 个元素集合的 k 不同元素子集的唯一组合

python - 如何以json格式返回数据?

php - 如何使用 PHPunit 测试抽象类?

javascript - Jasmine 测试失败 EmberJS 需要 API

unit-testing - TDD 如何处理异常和参数验证?