The project structure

1.common Stores common methods

Py precondition class public. Py gets the file specified directory class 'Copy the code
  1. Base holds the underlying method classes

    method.py

  2. Data Store data

    data.xls

  3. Tests holds the use case class PyTest

    test_excel.py
    Copy the code
Utils stores the utility class operationExcel. PyCopy the code

The code

  1. Low-level Request encapsulation (Base)
Import Requests class ApiRequest(object): def send_requests(self, method, url, data=None, params=None, headers=None, cookies=None, json=None, files=None, auth=None, timeout=None, proxies=None, verify=None, cert=None): self.r = requests.request(method=method, url=url, data=data, params=params, headers=headers, cookies=cookies, json=json, files=files, auth=auth, timeout=timeout, proxies=proxies, verify=verify, cert=cert) return self.rCopy the code
  1. Get the case set under the specified directory (Common)
Os.path. dirname(os.path.dirname(__file__)) # fileDir(data) def fileDir(data): Base_path =os.path.dirname(os.path.dirname(__file__)) return os.path.join(base_path,data) # Return the obtained directory ## returns a new string generated by concatenating the elements in the sequence with the specified character. Def filePath(fileDir="data", fileName="data.xls"): """ :param fileDir: Directory :param fileName: fileName: return: Return os.path.join(os.path.dirname(os.path.dirname(__file__)), fileDir, fileName)Copy the code

Os. path usage 2. Join usage

  1. Write test cases (data) data.xls

  1. Automated Tests (PyTests)
import pytest import json from utils.operationExcel import OperationExcel from common.login import login_token from base.method import ApiRequest class Test_gwyc_api(): # def testApi(self): # data = OperationExcel().getExcelData() # for i in data: # assert i == 1 # # assert 1==2 @pytest.mark.parametrize('data', Def test_excel(self, data, login_token): # Get the token assert login_token! Data [OperationExcel().case_headers] if len(STR (headers).split()) == 0: headers = None elif len(str(headers)) >= 0: Headers [' x-auth-token '] = headers # data[OperationExcel().case_data] if len(str(params).split()) == 0: params = None elif len(str(params)) == 0: Case_code = int(data[OperationExcel().case_code]) def case_result_assert(r): Assert int(R.son ()['code']) == case_code # status code # TODO response value # Expected dictionary vs. dictionary comparison # case_result = json.dumps(data[OperationExcel().case_result].strip()) # r_result = json.dumps(r.json(), ensure_ascii=False) # assert data[OperationExcel().case_result] in json.dumps(r.json(), If data[OperationExcel().case_method] == 'get': r = ApiRequest().send_requests( method='get', url=data[OperationExcel().case_url], data=params, headers=headers) case_result_assert(r=r) elif data[OperationExcel().case_method] == 'post': r = ApiRequest().send_requests( method='post', url=data[OperationExcel().case_url], data=params, headers=headers) case_result_assert(r=r) if __name__ == '__main__': pytest.main(['-m', 'test_gwyc_api_all.py'])Copy the code

Summary: The PyTest use case class Test must begin with a capital letter

  • Pytest.mark.parametrize is a built-in DDT for PyTest
  • The use of decorators
  • The use of inner classes, how are inner classes different from decorators
  • Expected todo has not decided how to assert better
  1. Login.py Preconditions class (Common)
Import pytest import requests @pytest.fixture(scope='module') #fixture def login_token() def login_token(): url = "http://****/sso-login" headers = {"Content-Type":"application/json"} data = {"name":(None, "usename"),"password":(None, "11111")} r = requests.post(url=url, files=data) return str(r.json()["data"]["X-AUTH-TOKEN"])Copy the code