python+requests接口自动化测试2--pytest框架编写测试用例

2021/6/22 14:27:02

本文主要是介绍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"])

 



这篇关于python+requests接口自动化测试2--pytest框架编写测试用例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程