सबसे पहले, आप xmlrpc.client.ServerProxy
पर नकल कर सकते हैं और करना चाहिए; आपकी लाइब्रेरी xmlrpc.client
को नए नाम के रूप में आयात करती है, लेकिन यह अभी भी वही मॉड्यूल ऑब्जेक्ट है, इसलिए आपकी लाइब्रेरी में xmlrpclib.ServerProxy
और xmlrpc.client.ServerProxy
एक ही ऑब्जेक्ट की ओर ले जाती है।
अगला, ऑब्जेक्ट का उपयोग कैसे किया जाता है, और पर कॉल करें, (..)
वाक्यविन्यास। आपका पुस्तकालय इस तरह सर्वर प्रॉक्सी का उपयोग करता है:
# a call to create an instance
auth_server = xmlrpclib.ServerProxy(AuthURL)
# on the instance, a call to another method
valid = auth_server.isTokenValid(token)
तो वहाँ एक श्रृंखला यहाँ है, जहां नकली कहा जाता है, और वापसी मान फिर एक और विशेषता यह है कि यह भी कहा जाता है पता लगाने के लिए प्रयोग किया जाता है। मजाक करते समय, आपको उसी श्रृंखला की तलाश करनी होगी; इसके लिए Mock.return_value
attribute का उपयोग करें। डिफ़ॉल्ट रूप से जब आप एक नकली कॉल करते हैं तो एक नया नकली उदाहरण लौटाया जाता है, लेकिन आप परीक्षण मान भी सेट कर सकते हैं।
तो अपने कोड का परीक्षण करने के लिए, आप auth_server.isTokenValid(token)
रिटर्न पर प्रभाव डालना चाहते हैं, और परीक्षण करें कि आपका कोड सही तरीके से काम करता है या नहीं। आप यह भी कहना चाहेंगे कि सही यूआरएल ServerProxy
उदाहरण में पास किया गया है।
विभिन्न परिणामों के लिए अलग-अलग परीक्षण बनाएं। शायद टोकन एक मामले में मान्य है, दूसरे में मान्य नहीं है, और आप दोनों मामलों का परीक्षण करना चाहते हैं:
class Tester(unittest.TestCase):
@patch('xmlrpc.client.ServerProxy')
def test_valid_token(self, mock_serverproxy):
# the ServerProxy(AuthURL) return value
mock_auth_server = mock_serverproxy.return_value
# configure a response for a valid token
mock_auth_server.isTokenValid.return_value = 1
# now run your library code
return_value = library.Library().function()
# and make test assertions
# about the server proxy
mock_serverproxy.assert_called_with('some_url')
# and about the auth_server.isTokenValid call
mock_auth_server.isTokenValid.assert_called_once()
# and if the result of the function is expected
self.assertEqual(return_value, 'expected return value')
@patch('xmlrpc.client.ServerProxy')
def test_invalid_token(self, mock_serverproxy):
# the ServerProxy(AuthURL) return value
mock_auth_server = mock_serverproxy.return_value
# configure a response; now testing for an invalid token instead
mock_auth_server.isTokenValid.return_value = 0
# now run your library code
return_value = library.Library().function()
# and make test assertions
# about the server proxy
mock_serverproxy.assert_called_with('some_url')
# and about the auth_server.isTokenValid call
mock_auth_server.isTokenValid.assert_called_once()
# and if the result of the function is expected
self.assertEqual(return_value, 'expected return value')
स्रोत
2017-01-07 13:56:20