為什么使用yaml設計測試用例:
1、不需要會寫代碼就能夠實現接口測試
2、使用固定的關鍵字,簡單的寫一個yaml文件,執行命令,生成測試結果和報告
什么情況下可以使用yaml設計測試用例
1、單接口測試
環境准備:
1、安裝yaml pip install pyyaml
2、pytest必須安裝這個版本 pytest pip install pytest==4.5.0
3、安裝tavern pip install tavern
怎么使用yaml設計測試用例:
1、test_name表示測什么
2、stages表示描述信息
3、request中寫入請求地址、請求方法、請求參數
4、response中寫入期望結果
4、生成測試報告,安裝pip install pytest-html
5、在當前路徑下生成login.html測試報告,cmd窗口中,在當前路徑下,執行pytest -v test_login.tavern.yaml --html=login.html
下面這段代碼是兩個測試用例:
1、密碼為空時,返回錯誤信息
2、性別為其他的值時,返回錯誤信息
1 test_name: 驗證密碼為空 2 3 stages: 4 - name: 驗證密碼為空 5 request: 6 url: http://127.0.0.1:5000/login/ 7 method: POST 8 data: 9 username: lisi 10 age: 12 11 sex: 男 12 response: 13 status_code: 400 14 body: 15 message: 16 password: 賬號密碼不能為空 17 18 --- 19 test_name: 驗證密碼為空 20 21 stages: 22 - name: 驗證性別只能是男或女 23 request: 24 url: http://127.0.0.1:5000/login/ 25 method: POST 26 data: 27 username: lisi 28 age: 12 29 sex: sad 30 response: 31 status_code: 400 32 body: 33 message: 34 sex: 性別只能是男或者女