我使用命令#34执行以下代码:nosetests --with-html test_rest_api.py"
class Test_rest_api(unittest.TestCase):
def test_create_task_group(self):
data = {"name":"group2"}
response = ib_api.rest_request('POST', object_type="create_Test")
msg = json.loads(response.read())
self.assertTrue(response.status >= 200 and response.status < 300,msg)
if __name__ == '__main__':
unittest.main(verbosity=2)
如果案例失败,我会得到变量&#34; msg&#34;中的字符串,但如果案件通过,我就不会收到消息
尝试如下解决方案,
self.assertTrue(response.status == 200 , msg)
print msg
这样可行,但问题是如果案例失败,则在html报告中出现2次消息
请建议处理上述案件的任何好方案
答案 0 :(得分:0)
我最终定制了unittest的断言函数,以便在我的日志记录中获取PASS消息。
像这样:
class TestBase(unittest.TestCase):
def _assert_logs(self, expr, msg=None):
"""
Helper function: Auto-logs PASS/FAIL for all overriden asserts
Keyword arguments:
expr -- Conditional expression appearing in an assert, ex. "x is None"
msg -- General message to log (default = None)
"""
if expr:
self.log.debug("PASS {0}".format(msg))
else:
self.log.error("FAIL {0}".format(msg))
def assertIn(self, member, container, msg=None, expected=None,
actual=None):
"""
Prepend unittest.TestCase's assertIn function for logging
Arguments:
first -- first variable of conditional being evaluated
second -- second variable of conditional being evaluated
Keyword arguments:
msg -- msg that identifies the test assertion
expected -- msg indicating expected result
actual -- msg indicating actual result
"""
msg = self._format_message(msg, expected, actual)
self._assert_logs(member in container, msg)
super(TestBase, self).assertIn(member, container, msg)
然后让您的测试用例继承自TestBase
而不是unittest.TestCase
。希望它有所帮助!