Unit testing 在无客户端的flask request unittest中设置cookie

Unit testing 在无客户端的flask request unittest中设置cookie,unit-testing,flask,Unit Testing,Flask,我正在为我的flask应用程序编写单元测试。我有两种方法不是为了返回响应,而是积极使用cookies。例如: def get_timezone(): try: return int(request.cookies.get('timezone_offset', 0)) except (RuntimeError, ValueError): pass return 0 我的测试看起来像: from my_main import my_flask_app with my_

我正在为我的flask应用程序编写单元测试。我有两种方法不是为了返回响应,而是积极使用cookies。例如:

def get_timezone():
  try:
    return int(request.cookies.get('timezone_offset', 0))
  except (RuntimeError, ValueError):
    pass

  return 0
我的测试看起来像:

from my_main import my_flask_app
with my_flask_app.test_Request_context(**request_environ_params):
  tz_value = 4
  # HERE. How to setup cookie value to `tz_value`?

  tz = myapp.utils.get_timezone()
  self.assertEqual(tz, tz_value)

因此,我想设置cookie(如何设置我已经知道的其他请求参数)用于测试目的。如何做到这一点?

测试客户机有一个
set\u cookie
方法,做类似的工作吗

with app.test_client() as c:
    c.set_cookie('localhost', 'COOKIE_NAME', 'cookie_value')
    resp = c.get('/my_url')

测试客户机有一个
set\u cookie
方法,这样做有效吗

with app.test_client() as c:
    c.set_cookie('localhost', 'COOKIE_NAME', 'cookie_value')
    resp = c.get('/my_url')

我认为这将是有用的:

with app.app_context():
    with app.test_request_context():
        resp = make_response(redirect('your_url'))
        resp.set_cookie('Cookie_name', 'Cookie_Value')

我认为这将是有用的:

with app.app_context():
    with app.test_request_context():
        resp = make_response(redirect('your_url'))
        resp.set_cookie('Cookie_name', 'Cookie_Value')
我想这对你有帮助。 cookie应该位于
标题中,这是
**请求环境参数的键

cookie = http.dump_cookie("Cookie_name",'Cookie_value')
with app.test_request_context(path="/", method="GET", headers={"COOKIE":cookie}):
    # do_something...
我想你能知道我的答案

:p(我英语不好)

我想这对你有帮助。 cookie应该位于
标题中,这是
**请求环境参数的键

cookie = http.dump_cookie("Cookie_name",'Cookie_value')
with app.test_request_context(path="/", method="GET", headers={"COOKIE":cookie}):
    # do_something...
我想你能知道我的答案


:p(我的英语不好)

我正在测试服务器端功能,这些功能无法使用
test\u client
进行测试,因此此方法根本不适用。我正在测试服务器端功能,这些功能无法使用
test\u client
进行测试,因此此方法根本不适用。