<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 單元測試 也許你經常需要對你的的應用進行單元測試或者僅僅檢查 Python session 的輸出。理論上講這是很簡單的,你可以偽造一個環境,通過一個假的 start_response 遍歷應用,但是這里還有一個更好的方法。 ### Diving In Werkzeug 提供了一個 Client 對象,可以傳入一個 WSGI 應用(可選傳入一個 response),通過這個你可以向應用發出一個虛擬請求。 用三個參數調用一個 response: 應用迭代器、狀態和一個 headers。默認 response 返回一個元組。因為 response 對象有相同的簽名,所以你可以像使用 response 一樣使用他們。通過這樣一種方式進行測試功能是很理想的。 ~~~ >>> from werkzeug.test import Client >>> from werkzeug.testapp import test_app >>> from werkzeug.wrappers import BaseResponse >>> c = Client(test_app, BaseResponse) >>> resp = c.get('/') >>> resp.status_code 200 >>> resp.headers Headers([('Content-Type', 'text/html; charset=utf-8'), ('Content-Length', '8339')]) >>> resp.data.splitlines()[0] '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"' ~~~ 或默認沒有 response: ~~~ >>> c = Client(test_app) >>> app_iter, status, headers = c.get('/') >>> status '200 OK' >>> headers [('Content-Type', 'text/html; charset=utf-8'), ('Content-Length', '8339')] >>> ''.join(app_iter).splitlines()[0] '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"' ~~~ ### 環境搭建 0.5 新版功能. 交互測試應用 最簡單的方法是使用 [EnvironBuilder](# "tests.EnvironBuilder") 類。它可以創建標準 WSGI環境和請求對象。 下面的例子創建了一個上傳文件和文件表單的 WSGI 環境: ~~~ >>> from werkzeug.test import EnvironBuilder >>> from StringIO import StringIO >>> builder = EnvironBuilder(method='POST', data={'foo': 'this is some text', ... 'file': (StringIO('my file contents'), 'test.txt')}) >>> env = builder.get_environ() ~~~ 返回的環境是一個新的 WSGI 環境,可用于進一步的處理: ~~~ >>> from werkzeug.wrappers import Request >>> req = Request(env) >>> req.form['foo'] u'this is some text' >>> req.files['file'] <FileStorage: u'test.txt' ('text/plain')> >>> req.files['file'].read() 'my file contents' ~~~ 當你將一個字典傳給構造函數數據, [EnvironBuilder](# "tests.EnvironBuilder") 會自動自動找出內容類型。如過你傳的似乎一個字符串或者輸入字符流,你不得不自己來做這些處理。 默認地它將會嘗試使用 application/x-www-form-urlencoded ,如果文件被上傳則只使用 multipart/form-data : ~~~ >>> builder = EnvironBuilder(method='POST', data={'foo': 'bar'}) >>> builder.content_type 'application/x-www-form-urlencoded' >>> builder.files['foo'] = StringIO('contents') >>> builder.content_type 'multipart/form-data' ~~~ 如果傳入一個字符串(或一個輸入流),你必須自己指定內容的類型: ~~~ >>> builder = EnvironBuilder(method='POST', data='{"json": "this is"}') >>> builder.content_type >>> builder.content_type = 'application/json' ~~~ ### 測試 API *class *tests.EnvironBuilder(*path='/'*, *base_url=None*, *query_string=None*, *method='GET'*, *input_stream=None*, *content_type=None*, *content_length=None*, *errors_stream=None*, *multithread=False*, *multiprocess=False*, *run_once=False*, *headers=None*, *data=None*, *environ_base=None*, *environ_overrides=None*, *charset='utf-8'*) 這個類為了測試可以方便的創建一個 WSGI 環境。他可以從任意數據快速創建 WSGI環境或請求對象。 這個類的簽名也可用于 Werkzeug 的其他地方([create_environ()](# "tests.create_environ"), BaseResponse.from_values(), [Client.open()](# "tests.Client.open"))。因為大多數功能只可通過構造函數實現。 文件和表格數據可以被各自的 form 和 files 屬性獨立處理。但是以相同的參數傳入構造函數:data。 data 可以是這些值: - a str: 如果一個字符串被轉化為一個 [input_stream](# "tests.EnvironBuilder.input_stream"),將會設置[content_length](# "tests.EnvironBuilder.content_length") ,你還要提供一個 [content_type](# "tests.EnvironBuilder.content_type")。 - a dict: 如果是一個字典,鍵將是一個字符串,值是以下對象: - 一個 file-like 對象。他們會被自動轉化成 FileStorage 對象。 - 一個元組。 add_file() 方法調用元組項目作為參數。 0.6 新版功能: path 和 base_url 現在是 unicode 字符串,它可以使用 iri_to_uri()函數編碼。 <table class="docutils field-list" frame="void" rules="none"><col class="field-name"/><col class="field-body"/><tbody valign="top"><tr class="field-odd field"><th class="field-name">參數:</th><td class="field-body"><ul class="first last simple"><li><strong>path</strong> – 請求的路徑。在 WSGI 環境它等效于 <cite>PATH_INFO</cite>。如果 <cite>query_string</cite>沒有被定義,這里有一個問題要注意,<cite>path</cite> 后面的將被當作 <cite>query string</cite>。</li><li><strong>base_url</strong> – base URL 是一個用于提取 WSGI URL ,主機 (服務器名 + 服務端口)和根腳本的 (<cite>SCRIPT_NAME</cite>) 的 URL。</li><li><strong>query_string</strong> – URL 參數可選的字符串和字典。</li><li><strong>method</strong> – HTTP 方法,默認為 <cite>GET</cite>。</li><li><strong>input_stream</strong> – 一個可選輸入流。不要指定它,一旦輸入流被設定,你將不能更改 <a class="reference internal" href="#tests.EnvironBuilder.args" title="tests.EnvironBuilder.args"><tt class="xref py py-attr docutils literal"><span class="pre">args</span></tt></a> 屬性和 <tt class="xref py py-attr docutils literal"><span class="pre">files</span></tt> 屬性除非你將<a class="reference internal" href="#tests.EnvironBuilder.input_stream" title="tests.EnvironBuilder.input_stream"><tt class="xref py py-attr docutils literal"><span class="pre">input_stream</span></tt></a> 重新設為 <cite>None</cite> 。</li><li><strong>content_type</strong> – 請求的內容類型。在0.5 版本當你指定文件和表格數據的時候不必必須指定他。</li><li><strong>content_length</strong> – 請求的內容長度。當通過 <cite>data</cite> 提供數據不必必須指定他。</li><li><strong>errors_stream</strong> – 用于 <cite>wsgi.errors</cite> 可選的錯誤流。默認為 <tt class="xref py py-data docutils literal"><span class="pre">stderr</span></tt>。</li><li><strong>multithread</strong> – 控制 <cite>wsgi.multithread</cite>。默認為 <cite>False</cite>。</li><li><strong>multiprocess</strong> – 控制 <cite>wsgi.multiprocess</cite>。默認為 <cite>False</cite>。</li><li><strong>run_once</strong> – 控制 <cite>wsgi.run_once</cite>。默認為 <cite>False</cite>。</li><li><strong>headers</strong> – headers 一個可選的列表或者 <tt class="xref py py-class docutils literal"><span class="pre">Headers</span></tt> 對象。</li><li><strong>data</strong> – 一個字符串或者表單數據字典。看上邊的 explanation。</li><li><strong>environ_base</strong> – 一個可選的默認環境。</li><li><strong>environ_overrides</strong> – 一個可選的覆蓋環境。</li><li><strong>charset</strong> – 編碼 unicode 數據的字符集。</li></ul></td></tr></tbody></table> path 應用的地址。(又叫 PATH_INFO) charset 編碼 unicode 數據的字符集。 headers 一個帶著請求 headers的 Headers 對象。 errors_stream 用于 wsgi.errors 流的錯誤流。 multithread wsgi.multithread 的值。 multiprocess wsgi.multiprocess 的值。 environ_base 新創建環境的基本字典。 environ_overrides 用于覆蓋生成環境的帶值字典。 input_stream 可選選項輸入流。這個和 form / files 是相互獨立的。同時如果請求方法不是 POST / PUT 或其他類似方法,不要提供輸入流。 args URL 參數是 MultiDict。 base_url base URL 是一個用于提取 WSGI URL ,主機(服務器名 + 服務器端口) 和根腳本 (SCRIPT_NAME) 的 URL close() 關閉所有文件。如果把 file 對象放入 files 字典,你可以通過調用這個方法自動關閉他們。 content_length 整數的長度,反射給 [headers](# "tests.EnvironBuilder.headers")。如果你設置了 files 或 form屬性不要設置這個參數。 content_type 請求的內容類型。反射給 [headers](# "tests.EnvironBuilder.headers")。如果你設置了 files 和form 屬性就不能設置內容類型。 get_environ() 返回內置環境。 get_request(*cls=None*) 返回一個帶數據的請求。如果沒有指定請求類,將會是用 [request_class](# "tests.EnvironBuilder.request_class")。 | 參數: | **cls** – 使用 request 包裝。 | |-----|-----| input_stream 一個可選的輸入流。如果你設置它,將會清空 form 和 files。 query_string 查詢字符串。如果你設置它, [args](# "tests.EnvironBuilder.args") 屬性將不再可用。 request_class 默認的請求類 [get_request()](# "tests.EnvironBuilder.get_request")。 BaseRequest 的別名 server_name 服務器名 (只讀, 使用 host 設置) server_port 整型服務器接口(只讀,使用 host 設置) server_protocol* = 'HTTP/1.1'* 服務器使用協議。默認為 HTTP/1.1 wsgi_version* = (1, 0)* 使用的 WSGI 版本。默認為(1, 0)。 *class *tests.Client(*application*, *response_wrapper=None*, *use_cookies=True*, *allow_subdomain_redirects=False*) 這個類允許你發送請求給一個包裹的應用。 響應可以是一個類或者一個有三個參數工廠函數: app_iter, status and headers。默認的響應僅僅是一個元組。 例如: ~~~ class ClientResponse(BaseResponse): ... client = Client(MyApplication(), response_wrapper=ClientResponse) ~~~ use_cookies 參數默認是開啟的,無論 cookies 是否被存儲,他都會和請求一起傳輸。但是你也可以關閉 cookie。 如果你想要請求應用的子域名,你可以設置 allow_subdomain_redirects 為 True ,如果為 False ,將不允許外部重定向。 0.5 新版功能: use_cookies 是在這個版本添加的。老版本不提供內置 cookie 支持。 open(*options*) 和 [EnvironBuilder](# "tests.EnvironBuilder") 一樣的參數還有一些補充: 你可以提供一個[EnvironBuilder](# "tests.EnvironBuilder") 類或一個 WSGI 環境代替 [EnvironBuilder](# "tests.EnvironBuilder")類作為參數。同時有兩個可選參數 (as_tuple, buffered),可以改變返回值的類型或應用執行方法。 在 0.5 版更改: 如果為 data 參數提供一個帶文件的字典,那么內容類型必須為 content_type而不是 mimetype。這個改變是為了和 werkzeug.FileWrapper 保持一致。 > follow_redirects 參數被添加到 [open()](# "tests.Client.open"). Additional parameters: <table class="docutils field-list" frame="void" rules="none"><col class="field-name"/><col class="field-body"/><tbody valign="top"><tr class="field-odd field"><th class="field-name">參數:</th><td class="field-body"><ul class="first last simple"><li><strong>as_tuple</strong> – 在表格中返回一個元組 <tt class="docutils literal"><span class="pre">(environ,</span> <span class="pre">result)</span></tt>。</li><li><strong>buffered</strong> – 把這個設為 True 來緩沖區運行應用。這個將會為你自動關閉所有應用。</li><li><strong>follow_redirects</strong> – 如果接下來 <cite>Client</cite> HTTP 重定向,這個將會設為 True。</li></ul></td></tr></tbody></table> get(*options*) 和 open 相似,但是方法強制執行 GET。 post(*options*) 和 open 相似,但是方法強制執行 POST。 put(*options*) 和 open 相似,但是方法強制執行 PUT。 delete(*options*) 和 open 相似,但是方法強制執行 DELETE。 head(*options*) 和 open 相似,但是方法強制執行 HEAD。 tests.create_environ([*options*]) 根據傳入的值創建一個 WSGI 環境。第一個參數應該是請求的路徑,默認為 ‘/'。另一個參數或者是一個絕對路徑(在這個例子中主機是 localhost:80)或請求的完整路徑,端口和腳本路徑。 它和 [EnvironBuilder](# "tests.EnvironBuilder") 構造函數接受相同的參數。 在 0.5 版更改: 這個函數現在是一個 [EnvironBuilder](# "tests.EnvironBuilder") 包裹,在 0.5 版本被添加。需要 headers, environ_base, environ_overrides 和 charset 參數。 tests.run_wsgi_app(*app*, *environ*, *buffered=False*) 返回一個應用輸出的元組形式 (app_iter, status, headers)。如果你通過應用返回一個迭代器他將會工作的更好。 有時應用可以使用 start_ewsponse 返回的 write() 回調函數。這將會自動解決邊界情況。如果沒有得到預期輸出,你應該將 buffered 設為 True 執行buffering 如果傳入一個錯誤的應用,這個函數將會是未定義的。不要給這個函數傳入一個不標準的 WSGI 應用。 <table class="docutils field-list" frame="void" rules="none"><col class="field-name"/><col class="field-body"/><tbody valign="top"><tr class="field-odd field"><th class="field-name">參數:</th><td class="field-body"><ul class="first simple"><li><strong>app</strong> – 要執行的應用。</li><li><strong>buffered</strong> – 設為 <cite>True</cite> 來執行 buffering.</li></ul></td></tr><tr class="field-even field"><th class="field-name">返回:</th><td class="field-body"><p class="first last">元組形式 <tt class="docutils literal"><span class="pre">(app_iter,</span> <span class="pre">status,</span> <span class="pre">headers)</span></tt></p></td></tr></tbody></table>
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看