【发布时间】:2018-11-05 09:36:55
【问题描述】:
我读过几篇文章说,如果你用 unittest.main() 调用你的单元测试,如果它们失败,它们应该以失败代码退出。我用命令调用我的单元测试:python -m unittest discover -v。我正在使用 Python 3.6.6。一个示例单元测试可能如下所示:
from server import app
import unittest
class ServerTestCase(unittest.TestCase):
"""
Unittesting for the server application.
"""
def setUp(self):
"""
Create a test client
"""
self.app = app.test_client()
self.app.testing = True
def tearDown(self):
pass
def test_root_endpoint(self):
"""
Testing the root endpoint
"""
result = self.app.get('/')
self.assertEqual(result.status_code, 200)
def test_health_endpoint(self):
"""
Testing the health endpoint
"""
result = self.app.get('/health')
assert b'UP' in result.data
if __name__ == '__main__':
unittest.main()
即使我的一项测试失败,我在检查退出代码时也会得到这个:
$ echo $?
0
我做错了什么?
【问题讨论】:
-
@bjmc 该帖子的最佳答案是说他没有使用
unittest.main,因此他需要使用解决方法。我正在使用unittest.main并遇到同样的问题,所以不同的情况
标签: python python-3.x unit-testing python-unittest