Fakeredis测试用例

时间:2020-08-14 16:56:55

标签: python redis

我正在尝试为我的Redis设置编写测试用例。

这是我的app.py

#!/usr/bin/python

from flask import Flask, render_template, jsonify
from redis import Redis
import os
import json

app = Flask(__name__)
host = os.environ['REDIS_SERVICE']
redis = Redis(host=host, port=6379)


@app.route("/", methods=["GET"])
@app.route("/index", methods=["GET", "POST"])
def index():
    counter = redis.incr("hits")
    return render_template("index.html",counter=counter)

@app.route("/getdata", methods=["GET"])
def get_data():
    data = redis.get("hits")
    result = int(data)
    return jsonify({'count': result})


if __name__ == "__main__":
    app.run(
        host="0.0.0.0",
        port=8000,
        debug=True
    )

我尝试进行伪造的redis测试,将命中率设置为1。但是,它仍从我的默认localhost redis服务器读取,我已将其导出为export REDIS_SERVICE = 127.0.0.1。任何建议是我做错了什么?

import unittest
import fakeredis
import json
from app import app
import app
from mock import patch, MagicMock

class BasicTestCase(unittest.TestCase):
    def setUp(self):
        self.app = app.app.test_client()
        redis_patcher = patch('app.redis', fakeredis.FakeStrictRedis().set('hits',1))
        

    def test_home(self):
        response = self.app.get('/getdata', content_type='html/text')
        data = json.loads(response.get_data(as_text=True))
        print(data)
        self.assertEqual(response.status_code, 200)

if __name__ == '__main__':
    unittest.main()

0 个答案:

没有答案