即使测试失败,单元测试脚本也会返回退出代码 = 0

编程入门 行业动态 更新时间:2024-10-11 11:19:41
本文介绍了即使测试失败,单元测试脚本也会返回退出代码 = 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的测试脚本如下所示:

My testing script looks as follows:

import os import sys from unittest import defaultTestLoader as loader, TextTestRunner path_to_my_project = os.path.dirname(os.path.abspath(__file__)) + '/../' sys.path.insert(0, path_to_my_project) suite = loader.discover('my_project') runner = TextTestRunner() runner.run(suite)

如果我运行这个脚本,输出是:

If I run this script, the output is:

$ python3 runtest.py .....F..... ====================================================================== FAIL: test_insert (fate.test.test_operators.OperatorTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/chiel/Projects/tfate/libs/fate/../fate/test/test_operators.py", line 16, in test_insert self.assertEqual(expected, self.session.text[:14]) AssertionError: 'Foo import sys$' != 'Foo import sys' - Foo import sys$ ? - + Foo import sys ---------------------------------------------------------------------- Ran 12 tests in 0.030s FAILED (failures=1)

退出代码为零:

$ echo $? 0

但是,Python 文档指出默认情况下,main 调用 sys.exit() 并带有一个退出代码,指示测试运行是成功还是失败."

However, the Python documentation states that "By default main calls sys.exit() with an exit code indicating success or failure of the tests run."

我的脚本有什么问题?

推荐答案

代码未使用 unittest.main.您需要使用 TestResult.wasSuccessful 并手动调用 sys.exit.

The code is not using unittest.main. You need to check the result using TestResult.wasSuccessful and call sys.exit manually.

import sys .... ret = not runner.run(suite).wasSuccessful() sys.exit(ret)

更多推荐

即使测试失败,单元测试脚本也会返回退出代码 = 0

本文发布于:2023-11-23 13:27:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621610.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也会   脚本   单元测试   代码   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!