<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 編寫測試 您可以使用JavaScript為Postman API請求編寫測試腳本。通過測試,您可以確保您的API能夠按預期運行,確定服務之間的集成可靠運行,并驗證新開發是否沒有破壞任何現有功能。當您的API項目出現問題時,您還可以使用測試代碼來幫助調試過程。 > 例如,您可以編寫測試以通過發送不完整數據的請求來驗證API的錯誤處理。 您可以將測試添加到單個請求,文件夾和集合。 要將測試添加到請求中,請打開請求,然后在“測試”選項卡中輸入代碼。測試將在請求運行后執行。您將能夠在**測試結果**選項卡中的響應數據旁邊看到輸出。 ![要求測試標簽](https://assets.postman.com/postman-docs/request-test-tab.jpg) ## [](https://learning.postman.com/docs/writing-scripts/test-scripts/#writing-test-scripts)編寫測試腳本 您的測試腳本可以使用動態變量,對響應數據執行測試斷言,并在請求之間傳遞數據。在**測試**的請求選項卡,您可以手動輸入您的JavaScript或使用**片段**,你會看到在代碼編輯器的右側。 測試將在收到響應后執行,因此,當您單擊**Send時**,當響應數據從API返回時,Postman將運行您的測試腳本。 > 如果您需要在請求運行之前執行代碼,請改用“[請求前腳本”](https://learning.postman.com/docs/writing-scripts/pre-request-scripts/)。有關在請求運行時腳本如何執行的更多信息,請參見[腳本簡介](https://learning.postman.com/docs/writing-scripts/intro-to-scripts/)。 要執行測試以驗證請求返回的數據,可以使用`pm.response`對象。您可以使用`pm.test`函數定義測試,并提供一個名稱和函數,該函數返回一個布爾值(`true`或`false`)來指示測試是通過還是失敗。您可以在聲明中使用[ChaiJS BDD](https://www.chaijs.com/api/bdd/)語法`pm.expect`來測試響應詳細信息。 該`.test`函數的第一個參數是文本字符串,它將出現在測試結果輸出中,因此您可以使用它來識別測試,并將測試的目的傳達給查看結果的任何人。 例如,在“**測試”**選項卡中為任何請求輸入以下內容,以測試響應狀態代碼是否為`200`。 ~~~js pm.test("Status test", function () { pm.response.to.have.status(200); }); ~~~ ![測試狀態示例](https://assets.postman.com/postman-docs/example-test-status.jpg) 單擊**發送**以運行您的請求,然后在響應部分中打開**測試結果**。選項卡標題顯示通過了多少測試,總共運行了多少。您還可以在通過,跳過和失敗的測試結果之間切換。 ![測試結果](https://assets.postman.com/postman-docs/test-result-status.jpg) 如果請求返回了`200`狀態碼,則測試將通過,否則將失敗。嘗試在測試腳本中更改預期的狀態代碼,然后再次運行請求。 ![測試結果失敗](https://assets.postman.com/postman-docs/failed-test-status.jpg) 使用`pm.expect`語法可以為您的測試結果消息提供不同的格式-嘗試各種替代方法以獲得您認為最有用的輸出。 ![測試結果失敗](https://assets.postman.com/postman-docs/expect-test-syntax.jpg) > 使用**“**[簡介](https://documenter.getpostman.com/view/1559645/RzZFCGFR?version=latest)**”中**的**“在郵遞員**中**運行”**按鈕[來編寫測試集合,](https://documenter.getpostman.com/view/1559645/RzZFCGFR?version=latest)以將包含一些示例測試腳本的模板導入Postman并進行代碼試驗。 您的代碼可以測試請求[環境](https://learning.postman.com/docs/sending-requests/managing-environments/),如以下示例所示: ~~~js pm.test("environment to be production", function () { pm.expect(pm.environment.get("env")).to.equal("production"); }); ~~~ 您可以使用不同的語法變體以您認為可讀的方式編寫測試,并且適合您的應用程序和測試邏輯。 ~~~js pm.test("response should be okay to process", function () { pm.response.to.not.be.error; pm.response.to.have.jsonBody(""); pm.response.to.not.have.jsonBody("error"); }); ~~~ 測試可以使用適合于響應數據格式的語法來確定請求響應的有效性。 ~~~js pm.test("response must be valid and have a body", function () { pm.response.to.be.ok; pm.response.to.be.withBody; pm.response.to.be.json; }); ~~~ 您的腳本可以包含您需要的許多測試,并且當您單擊“**保存”**時,這些腳本將與其余的請求詳細信息一起**保存**。如果您共享一個收藏集或發布文檔/“在郵遞員中運行”按鈕,則查看或導入模板的任何人都將包含您的測試代碼。 ### [](https://learning.postman.com/docs/writing-scripts/test-scripts/#using-snippets)使用摘要 你會看到一個選擇常用的測試代碼摘錄的**片段**,以測試編輯器的右側。單擊添加一個,它將出現在編輯器中。代碼段可以加快腳本入門的速度-您可以在添加代碼段以滿足自己的測試要求之后對其進行編輯。 ![添加了代碼段](https://assets.postman.com/postman-docs/added-test-snippet.jpg) ## [](https://learning.postman.com/docs/writing-scripts/test-scripts/#testing-collections-and-folders)測試集合和文件夾 您可以將測試腳本添加到集合,文件夾或集合中的單個請求中。與集合關聯的測試腳本將在集合中的每個請求之后運行。與文件夾關聯的測試腳本將在文件夾中的每個請求之后運行。這樣,您可以在每個請求之后重用通常執行的測試。 > 將腳本添加到集合和文件夾后,您可以測試API項目中的工作流程。這有助于確保您的請求涵蓋典型場景,從而為應用程序用戶提供可靠的體驗。 您可以通過單擊集合或文件夾名稱旁邊的**查看更多操作**(...),然后選擇**編輯**來更新集合和文件夾腳本。選擇“**測試”**選項卡以添加或更新腳本。您也可以在首次創建集合時添加集合腳本。 ![收集測試](https://assets.postman.com/postman-docs/collection-test-script.jpg) 當你[運行一個集合](https://learning.postman.com/docs/running-collections/intro-to-collection-runs/),你將看到由集合亞軍輸出的測試結果。 ![收集測試](https://assets.postman.com/postman-docs/collection-tests-run.jpg) ## 使用多個斷言 您的測試可以在單個測試中包含多個斷言,您可以使用此斷言將相關斷言分組在一起。 ~~~js pm.test("The response has all properties", () => { //parse the response json and test three properties const responseJson = pm.response.json(); pm.expect(responseJson.type).to.eql('vip'); pm.expect(responseJson.name).to.be.a('string'); pm.expect(responseJson.id).to.have.lengthOf(1); }); ~~~ 如果任何包含的斷言失敗,則整個測試將失敗。所有斷言必須成功才能通過測試。 ## 解析響應主體數據 為了對響應執行斷言,首先需要將數據解析為斷言可以使用的JavaScript對象。 要解析JSON數據,請使用以下語法: ~~~js const responseJson = pm.response.json(); ~~~ 要解析XML,請使用以下命令: ~~~js const responseJson = xml2Json(pm.response.text()); ~~~ ### 測試響應主體 您可以在響應正文中檢查特定值: ~~~js pm.test("Person is Jane", () => { const responseJson = pm.response.json(); pm.expect(responseJson.name).to.eql("Jane"); pm.expect(responseJson.age).to.eql(23); }); ~~~ ### [](https://learning.postman.com/docs/writing-scripts/script-references/test-examples/#testing-status-codes)測試狀態碼 您可以測試響應狀態代碼: ~~~js pm.test("Status code is 201", () => { pm.response.to.have.status(201); }); ~~~ 如果要測試狀態碼是否為一組,可以將它們全部包含在數組中并使用`oneOf`: ~~~js pm.test("Successful POST request", () => { pm.expect(pm.response.code).to.be.oneOf([201,202]); }); ~~~ ### 測試頭 您可以檢查響應頭是否存在: ~~~js pm.test("Content-Type header is present", () => { pm.response.to.have.header("Content-Type"); }); ~~~ 您還可以測試具有特定值的響應頭: ~~~js pm.test("Content-Type header is application/json", () => { pm.expect(pm.response.headers.get('Content-Type')).to.eql('application/json'); }); ~~~ ### 測試cookie 您可以測試響應中是否存在Cookie: ~~~js pm.test("Cookie JSESSIONID is present", () => { pm.expect(pm.cookies.has('JSESSIONID')).to.be.true; }); ~~~ 您還可以測試特定的Cookie值: ~~~js pm.test("Cookie isLoggedIn has value 1", () => { pm.expect(pm.cookies.get('isLoggedIn')).to.eql('1'); }); ~~~ ### [](https://learning.postman.com/docs/writing-scripts/script-references/test-examples/#testing-response-times)測試響應時間 您可以測試響應時間是否在指定范圍內: ~~~js pm.test("Response time is less than 200ms", () => { pm.expect(pm.response.responseTime).to.be.below(200); }); ~~~ ### 針對變量聲明響應值 您可以檢查響應屬性是否與變量(在這種情況下為環境變量)具有相同的值。 ~~~js pm.test("Response property matches environment variable", function () { pm.expect(pm.response.json().name).to.eql(pm.environment.get("name")); }); ~~~ ### 斷言值類型 您可以測試響應的任何部分的類型。 ~~~js /* response has this structure: { "name": "Jane", "age": 29, "hobbies": [ "skating", "painting" ], "email": null } */ const jsonData = pm.response.json(); pm.test("Test data type of the response", () => { pm.expect(jsonData).to.be.an("object"); pm.expect(jsonData.name).to.be.a("string"); pm.expect(jsonData.age).to.be.a("number"); pm.expect(jsonData.hobbies).to.be.an("array"); pm.expect(jsonData.website).to.be.undefined; pm.expect(jsonData.email).to.be.null; }); ~~~ ### 斷言值在集合中 您可以根據有效選項列表檢查響應值。 ~~~js pm.test("Value is in valid list", () => { pm.expect(pm.response.json().type) .to.be.oneOf(["Subscriber", "Customer", "User"]); }); ~~~
                  <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>

                              哎呀哎呀视频在线观看