【发布时间】:2015-03-12 18:54:47
【问题描述】:
我需要为凭证检查模块编写一个单元测试,如下所示。我很抱歉我无法复制确切的代码..但我尽力简化作为一个例子。 我想修补 methodA 使其返回 False 作为返回值并测试 MyClass 以查看它是否抛出错误。 cred_check 是文件名,MyClass 是类名。 methodA 在 MyClass 之外,返回值 checkedcredential 为 True 或 False。
def methodA(username, password):
#credential check logic here...
#checkedcredential = True/False depending on the username+password combination
return checkedcredential
class MyClass(wsgi.Middleware):
def methodB(self, req):
username = req.retrieve[constants.USER]
password = req.retrieve[constants.PW]
if methodA(username,password):
print(“passed”)
else:
print(“Not passed”)
return http_exception...
我目前的单元测试看起来像......
import unittest
import mock
import cred_check import MyClass
class TestMyClass(unittest.Testcase):
@mock.patch('cred_check')
def test_negative_cred(self, mock_A):
mock_A.return_value = False
#not sure what to do from this point....
我想在我的单元测试中写的部分是 return http_exception 部分。我正在考虑通过修补方法A来返回False。设置返回值后,编写单元测试以使其按预期工作的正确方法是什么?
【问题讨论】:
-
为什么要修补
methodB????????? -
哎呀.. 对。谢谢,我摆脱了methodB补丁。仍然不确定我应该怎么做..
-
@killahjc 你还对这个问题感兴趣吗?
-
@Micheled'Amico 抱歉,我接受了你的回答。非常感谢
标签: python unit-testing mocking wsgi assert