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"])

 

上一篇:某宝x-sign测试记录(1)


下一篇:ACL2016最佳论文:CNN/日常邮件阅读理解任务的彻底检查