1. 程式人生 > 其它 >python+requests介面自動化測試2--pytest框架編寫測試用例

python+requests介面自動化測試2--pytest框架編寫測試用例

使用pytest建立登入模組測試用例類

import pytest

def
get_timestamp():
  ...

def get_nonce():
  ...

def get_sign():
  ...
class BaseRequest:  # 請求方法類
  ...

class Test_Login(object): # 測試用例類需繼承object def setup_class(self): print("用例執行前執行,主要用於初始化工作") def teardown_class(self): print("用例執行結束後執行")

  # 登入成功
def test_userLogin_ok(self): # 測試用例 url = "https://xxx/login/password" nonce = get_nonce() timestamp = get_timestamp() params = {"username": "admin", "password": "4297f44b13955235245b2497399d7a93"} sign = get_sign(params, ....) header = {"Content-Type
": "application/json", "client": "xxx", "sign": sign, "timestamp": timestamp,
          
"nonce": nonce} br = BaseRequest(url) re = br.sureMethod(header, "post", params) print(re.json())

  # 使用者名稱錯誤
  
def test_userLogin_fail(self):
      url = "https://xxx/login/password"
  nonce = get_nonce()
  timestamp = get_timestamp()
  params = {"username": "admin11", "password": "4297f44b13955235245b2497399d7a92"
}
  sign = get_sign(params, key, "", timestamp, nonce)
  header = {"Content-Type": "application/json", "client": "xxx", "sign": sign, "timestamp": timestamp,
          "nonce": nonce}
  br = BaseRequest(url)
  re = br.sureMethod(header, "post", params)
  print(re.json())
if __name__ == '__main__': 
  pytest.main([
'-sv', "api.py"])