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