<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之旅 廣告
                # 行為驅動開發(BDD)通過行為測試 REST API > 原文: [https://www.guru99.com/bdd-testing-rest-api-behave.html](https://www.guru99.com/bdd-testing-rest-api-behave.html) ## 什么是 BDD 測試? **BDD(行為驅動的開發)測試**是一種敏捷軟件開發技術,是 TDD 的擴展,即測試驅動開發。 在 BDD 中,測試用例以自然語言編寫,即使非程序員也可以閱讀。 在本 BDD 教程中,我們將看到使用 Behave 和 Python 對 REST API 進行 BDD 測試 * [BDD 測試如何工作?](#2) * [什么是 REST API 測試?](#3) * [行為是什么?](#4) * [在 Windows 上設置行為測試框架:](#5) * [POST 場景示例:](#6) * [步驟實施](#7) * [運行測試:](#8) * [報告](#9) ## BDD 測試如何工作? 考慮您被分配在網上銀行應用程序中創建資金轉賬模塊。 有多種測試方法 1. 如果來源帳戶中有足夠的余額,則應進行資金轉帳 2. 如果目的地 A / C 詳細信息正確,則應進行資金轉帳 3. 如果用戶輸入的交易密碼/ rsa 代碼/安全認證正確,則應進行資金轉帳 4. 即使是銀行假期也應進行轉帳 5. 資金轉移應在帳戶持有人設定的未來某個日期進行 [測試場景](/test-scenario.html)變得更加復雜和復雜,因為我們考慮了其他功能,例如在 Y 天/月的間隔內轉移金額 X,當總額達到 Z 時停止計劃轉移,依此類推 開發人員的一般趨勢是在以后開發功能并編寫測試代碼。 如上述案例所示,[測試用例](/test-case.html)的開發非常復雜,開發人員將推遲[測試](/software-testing.html)直到發布,這時他將進行快速但無效的測試。 為了克服此問題(行為驅動開發),設想了 BDD。 它使開發人員的整個測試過程變得容易 在 BDD 中,無論編寫什么內容,都必須進入 ***給定時間*** 步驟。 讓我們考慮上面 BDD 中的相同示例 ``` Given that a fund transfer module in net banking application has been developed And I am accessing it with proper authentication ``` ``` WhenI shall transfer with enough balance in my source account Or I shall transfer on a Bank Holiday Or I shall transfer on a future date And destination a/c details are correct And transaction password/rsa code / security authentication for the transaction is correct And press or click send button ``` ``` Then amount must be transferred And the event will be logged in log file ``` 寫,讀和理解不容易嗎? 它涵蓋了資金轉移模塊的所有可能的測試用例,并且可以輕松修改以容納更多的用例。 此外,它更像是為資金轉移模塊編寫文檔。 ## 什么是 REST API 測試? 如今,由于 REST 已成為構建 API 的一種流行樣式,因此自動化 REST API 測試用例和 UI 測試用例變得同等重要。 因此,基本上,這些 REST API 測試涉及分別使用 POST,GET,PUT 和 DELETE 方法測試 CRUD(創建-讀取-更新-刪除)操作。 ## 行為是什么? 行為是流行的 Python BDD 測試框架之一。 讓我們看看行為如何發揮作用: 功能文件由您的業務分析師/贊助商/與其中包含您的行為方案的任何人一起編寫。 它具有自然語言格式,用于描述特征或特征的一部分以及預期結果的代表性示例 這些場景步驟與用 Python 編寫的步驟實現映射 并且可選地,有一些環境控制(在步驟,場景,功能或整個射擊比賽之前和之后運行的代碼)。 讓我們開始使用 Behave 設置自動化測試框架: ## 在 Windows 上設置行為測試框架: ### 安裝: * 從 [https://www.python.org/](https://www.python.org/) 下載并安裝 Python 3 * 在命令提示符處執行以下命令以安裝行為 * 點安裝行為 * IDE:我使用了 PyCharm 社區版 [https://www.jetbrains.com/pycharm/download](https://www.jetbrains.com/pycharm/download/#section=windows) ### 項目設置: * 創建一個新項目 * 創建以下目錄結構: ![](https://img.kancloud.cn/c4/bf/c4bf29b16705d88844efd4450078e534_707x96.png) ### 功能文件: 因此,讓我們構建功能文件 **Sample_REST_API_Testing.feature** ,該功能文件具有對“ posts”服務執行 CRUD 操作的功能。 在我們的示例中,我使用了 [http://jsonplaceholder.typicode.com/](http://jsonplaceholder.typicode.com) 發布了示例 REST 服務。 ## POST 場景示例: ``` Scenario: POST post example ->Here we are considering creating new post item using 'posts' service Given: I set post posts API endpoint ->This is prerequisite for the test which is setting URL of posts service When: I set HEADER param request content type as "application/json." And set request body And send POST HTTP request ->This is actual test step of sending a post request Then: Then I receive valid HTPP response code 201 And Response body "POST" is non-empty-> This is verification of response body ``` 同樣,您可以按以下方式編寫其余方案: ![](https://img.kancloud.cn/03/50/0350eb6d2aef87b8ee13545863388ed0_594x604.png) **Sample_REST_API_Testing.feature** ``` Feature: Test CRUD methods in Sample REST API testing framework Background: Given I set sample REST API url Scenario: POST post example Given I Set POST posts api endpoint When I Set HEADER param request content type as "application/json." And Set request Body And Send a POST HTTP request Then I receive valid HTTP response code 201 And Response BODY "POST" is non-empty. Scenario: GET posts example Given I Set GET posts api endpoint "1" When I Set HEADER param request content type as "application/json." And Send GET HTTP request Then I receive valid HTTP response code 200 for "GET." And Response BODY "GET" is non-empty Scenario: UPDATE posts example Given I Set PUT posts api endpoint for "1" When I Set Update request Body And Send PUT HTTP request Then I receive valid HTTP response code 200 for "PUT." And Response BODY "PUT" is non-empty Scenario: DELETE posts example Given I Set DELETE posts api endpoint for "1" When I Send DELETE HTTP request Then I receive valid HTTP response code 200 for "DELETE." ``` ## 步驟執行 現在,對于上述場景中使用的功能步驟,您可以在“步驟”目錄下的 Python 文件中編寫實現。 行為框架通過與特征文件謂詞匹配的修飾符來標識 Step 函數。 例如,特征文件場景中的給定謂詞搜索具有修飾符“給定”的階躍函數。 類似的匹配發生在時間和時間。 但是,在“但是”,“并且”的情況下,Step 函數采用與上一步相同的修飾符。 例如,如果給定給出“ And”,則匹配的步進函數裝飾器為@given。 例如,當執行 POST 的步驟時,可以執行以下步驟: ``` @when (u'I Set HEADER param request content type as "{header_conent_type}"') Mapping of When, here notice “application/json” is been passed from feature file for "{header_conent_type}” . This is called as parameterization def step_impl (context, header_conent_type): This is step implementation method signature request_headers['Content-Type'] = header_conent_type Step implementation code, here you will be setting content type for request header ``` 同樣,步驟 python 文件中其他步驟的實現如下所示: ![](https://img.kancloud.cn/06/c1/06c138f272a7a30b3d43eecc19e5d7b6_921x936.png) **sample_step_implementation.py** ``` from behave import given, when, then, step import requests api_endpoints = {} request_headers = {} response_codes ={} response_texts={} request_bodies = {} api_url=None @given(u'I set sample REST API url') def step_impl(context): global api_url api_url = 'http://jsonplaceholder.typicode.com' # START POST Scenario @given(u'I Set POST posts api endpoint') def step_impl(context): api_endpoints['POST_URL'] = api_url+'/posts' print('url :'+api_endpoints['POST_URL']) @when(u'I Set HEADER param request content type as "{header_conent_type}"') def step_impl(context, header_conent_type): request_headers['Content-Type'] = header_conent_type #You may also include "And" or "But" as a step - these are renamed by behave to take the name of their preceding step, so: @when(u'Set request Body') def step_impl(context): request_bodies['POST']={"title": "foo","body": "bar","userId": "1"} #You may also include "And" or "But" as a step - these are renamed by behave to take the name of their preceding step, so: @when(u'Send POST HTTP request') def step_impl(context): # sending get request and saving response as response object response = requests.post(url=api_endpoints['POST_URL'], json=request_bodies['POST'], headers=request_headers) #response = requests.post(url=api_endpoints['POST_URL'], headers=request_headers) #https://jsonplaceholder.typicode.com/posts # extracting response text response_texts['POST']=response.text print("post response :"+response.text) # extracting response status_code statuscode = response.status_code response_codes['POST'] = statuscode @then(u'I receive valid HTTP response code 201') def step_impl(context): print('Post rep code ;'+str(response_codes['POST'])) assert response_codes['POST'] is 201 # END POST Scenario # START GET Scenario @given(u'I Set GET posts api endpoint "{id}"') def step_impl(context,id): api_endpoints['GET_URL'] = api_url+'/posts/'+id print('url :'+api_endpoints['GET_URL']) #You may also include "And" or "But" as a step - these are renamed by behave to take the name of their preceding step, so: @when(u'Send GET HTTP request') def step_impl(context): # sending get request and saving response as response object response = requests.get(url=api_endpoints['GET_URL'], headers=request_headers) #https://jsonplaceholder.typicode.com/posts # extracting response text response_texts['GET']=response.text # extracting response status_code statuscode = response.status_code response_codes['GET'] = statuscode @then(u'I receive valid HTTP response code 200 for "{request_name}"') def step_impl(context,request_name): print('Get rep code for '+request_name+':'+ str(response_codes[request_name])) assert response_codes[request_name] is 200 @then(u'Response BODY "{request_name}" is non-empty') def step_impl(context,request_name): print('request_name: '+request_name) print(response_texts) assert response_texts[request_name] is not None # END GET Scenario #START PUT/UPDATE @given(u'I Set PUT posts api endpoint for "{id}"') def step_impl(context,id): api_endpoints['PUT_URL'] = api_url + '/posts/'+id print('url :' + api_endpoints['PUT_URL']) @when(u'I Set Update request Body') def step_impl(context): request_bodies['PUT']={"title": "foo","body": "bar","userId": "1","id": "1"} @when(u'Send PUT HTTP request') def step_impl(context): # sending get request and saving response as response object # response = requests.post(url=api_endpoints['POST_URL'], headers=request_headers) #https://jsonplaceholder.typicode.com/posts response = requests.put(url=api_endpoints['PUT_URL'], json=request_bodies['PUT'], headers=request_headers) # extracting response text response_texts['PUT'] = response.text print("update response :" + response.text) # extracting response status_code statuscode = response.status_code response_codes['PUT'] = statuscode #END PUT/UPDATE #START DELETE @given(u'I Set DELETE posts api endpoint for "{id}"') def step_impl(context,id): api_endpoints['DELETE_URL'] = api_url + '/posts/'+id print('url :' + api_endpoints['DELETE_URL']) @when(u'I Send DELETE HTTP request') def step_impl(context): # sending get request and saving response as response object response = requests.delete(url=api_endpoints['DELETE_URL']) # response = requests.post(url=api_endpoints['POST_URL'], headers=request_headers) #https://jsonplaceholder.typicode.com/posts # extracting response text response_texts['DELETE'] = response.text print("DELETE response :" + response.text) # extracting response status_code statuscode = response.status_code response_codes['DELETE'] = statuscode #END DELETE ``` ## 運行測試: 現在,我們完成了測試腳本開發部分,因此讓我們運行測試: 在命令提示符處執行以下命令以運行我們的功能文件 C:\ Programs \ Python \ Python37 > **表現-f pretty** C:\ <您的項目路徑> \ features \ feature_files_folder \ **Sample_REST_API_Testing.feature** 這將顯示測試執行結果,如下所示: ![](https://img.kancloud.cn/8a/bd/8abd4cafea02036cf2d229b71ee17cc5_1402x650.png) **控制臺上的報告顯示** 讓我們在這里再看一件很酷的事情。 由于用戶始終希望以更易讀,更易懂的格式查看測試結果,因此,在 Allure 的幫助下,以 HTML 格式生成報告。 ## 報告 首先,您需要安裝 Allure Behave 格式化程序[ [https://docs.qameta.io/allure/](https://docs.qameta.io/allure/) ]: 現在執行以下命令: 對于報告 > **表現-f json -o** <到您的報告文件夾的路徑> **Sample_REST_API_Testing.feature** <誘人文件夾路徑> > **誘人服務** <到您的報告文件夾的路徑> 這將以可呈現和信息性的格式生成您的測試結果報告,如下所示: ![](https://img.kancloud.cn/c1/f7/c1f7312e0a4170fef0b731e716d9258a_672x312.png) **HTML 格式的測試報告** ![](https://img.kancloud.cn/60/a5/60a590bdc7487b24e3bfc38777230637_1053x502.png) **測試報告顯示單個方案結果** ## 摘要: * BDD 是行為驅動的開發。 它是敏捷軟件開發的技術之一。 * 如今,REST 已成為構建 API 的一種流行樣式,自動化 REST API 測試用例和 UI 測試用例也變得同樣重要。 * BDD 具有自然語言格式,用于描述特征或特征的一部分,并帶有預期結果的代表性示例 * 行為框架通過與特征文件謂詞匹配的裝飾器來識別 Step 函數
                  <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>

                              哎呀哎呀视频在线观看