我通过pytest进行的单元测试有问题。
为了上下文化,我尝试测试redis上数据的添加和存在。
在与redis的交互中,我使用以下方法:https://github.com/underyx/flask-redis/blob/master/flask_redis/client.py
我的测试是这样的:
sample_key = b"-vmbFSk5WRKwnIRIkBLWtEd5g9Hqy3IO67Bmdnqf2Ek="
user_uuid = "8bb739e5-cf44-4e87-b1f7-b8eb1cc6c25d"
def test_check_already_connected():
assert check_already_connected(user_uuid) is False
RedisWrapper.set(sample_key, '3', user_uuid)
def test_check_already_connected1():
RedisWrapper.set(sample_key, '3', user_uuid)
print("keys")
print(RedisWrapper.get_all_keys())
assert check_already_connected(user_uuid) is True
这是我运行测试时得到的结果:
tests/test_authentication.py Error - the JSON object must be str, bytes or bytearray, not NoneType
.Error - the JSON object must be str, bytes or bytearray, not NoneType
keys
[]
F
============================================= FAILURES =============================================
__________________________________ test_check_already_connected1 ___________________________________
def test_check_already_connected1():
RedisWrapper.set(sample_key, '3', user_uuid)
print("keys")
print(RedisWrapper.get_all_keys())
> assert check_already_connected(user_uuid) is True
E AssertionError: assert False is True
E + where False = check_already_connected('8bb739e5-cf44-4e87-b1f7-b8eb1cc6c25d')
tests/test_authentication.py:26: AssertionError
=================================== 1 failed, 1 passed in 0.42s ====================================
我的redis Package :https://textup.fr/435412an
在app/init.py中初始化redis\u客户端
app = Flask(__name__)
redis_client = FlaskRedis(app)
我要测试的函数:
def check_already_connected(user_uuid):
redis_keys = RedisWrapper.get_all_keys()
if redis_keys:
for key in redis_keys:
entry = RedisWrapper.get(key)
account_uuid = entry.get('3')
if account_uuid == str(user_uuid):
return True
return False
我想问题是在我测试之前redis的初始化,但是我测试了几个解决方案都没有成功。
暂无答案!
目前还没有任何答案,快来回答吧!