模拟,UnitTest,JSON [英] Mock, UnitTest, JSON

查看:136
本文介绍了模拟,UnitTest,JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建单元测试时遇到问题,以确保我想要的方法运行良好.尽管没有涉及使用nodetests运行它.

been having an issue with creating a unittest to make sure that the method that I'd like works well. Running it with nodetests though gave no coverage.

import unittest
from mock import Mock, patch, MagicMock
from django.conf import settings
from hackathon.scripts.steam import *


class SteamTests(unittest.TestCase):

    def setup(self):
        self.API_URL = 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/'
        self.APIKEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'
        self.userID = 'Marorin'
        self.steamnum = '76561197997115778'

    def testGetUserIDNum(self):
        '''Test for steam.py method'''

        # Pulling from setUp
        userID = self.userID
        API_URL = self.API_URL
        APIKEY = self.APIKEY   

        # constructing the URL
        self.url = API_URL + '?' + APIKEY + '&' + userID

        with patch('hackathon.scripts.steam.steamIDpulling') as mock_steamIDPulling:
            # Mocking the return value of this method.
            mock_steamIDpulling = 76561197997115778
            self.assertEqual(steamIDPulling(userID,APIKEY),mock_steamIDpulling)

提取信息的方法:

    def steamIDPulling(SteamUN,key): 
#Pulls out and returns the steam id number for use in steam queries. steaminfo = {'key': key,'vanityurl': SteamUN} 
a = requests.get('api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/';, params=steaminfo)
 k = json.loads(a.content) 
SteamID = k['response']['steamid'] 
return SteamID

推荐答案

# Mocking the return value of this method.
mock_steamIDpulling = 76561197997115778

应该是:

# Mocking the return value of this method.
mock_steamIDpulling.return_value = 76561197997115778

但是,您编写的代码实际上没有任何意义.如果您要测试steamIDpulling的返回值,请执行以下操作:

However, you code does not really make sense. If you are trying to test the return value of steamIDpulling, just do:

def testGetUserIDNum(self):
    '''Test for steam.py method'''

    # Pulling from setUp
    userID = self.userID
    API_URL = self.API_URL
    APIKEY = self.APIKEY   

    # constructing the URL
    self.url = API_URL + '?' + APIKEY + '&' + userID
    self.assertEqual(steamIDPulling(userID,APIKEY), 76561197997115778)

仅当需要更改测试方法内部的值时才需要进行模拟(除非您测试mock库).要模拟请求库,请执行以下操作:

Mocking is only needed if a value inside the tested method needs to be changed (unless your testing the mock library). To mock the request library, do:

def testGetUserIDNum(self):
    '''Test for steam.py method'''

    # Pulling from setUp
    userID = self.userID
    API_URL = self.API_URL
    APIKEY = self.APIKEY   

    # constructing the URL
    self.url = API_URL + '?' + APIKEY + '&' + userID
    with patch("requests.get") as mock_requests_get:
        mock_requests_get.return_value = """{"response": {"streamid":76561197997115778}}"""
        self.assertEqual(steamIDPulling(userID,APIKEY), 76561197997115778)

这篇关于模拟,UnitTest,JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆