I. Introduction
In automated testing, we often encounter some problems:
(1) There are hundreds of test cases, how is the login status maintained?
(2) Each tested interface depends on the parameters of other interfaces. As shown in the figure below, there are more than a dozen front interface classes. In addition, there are some commonly used libraries, such as datetime, json, requests, os, etc. Do I need to write more than a dozen from XXX import XXX for each test case class?
So, how can you simplify the writing of the interface import and import all the dependencies and libraries at once?
Second, solve the problem
1. The effect of solving the problem of token sharing, as shown below
/api/auth/jwt/token is the login interface, you only log in once, and all interface test cases are executed
2. Simplify import
In the article [python+unittest interface automated testing (5)]-design test cases, one step is to import the front interface.
As shown in the figure below, only from PreProcessing import * can import more than a dozen pre-interfaces in the previous article at one time
Three, realize
Next, we will use some features of the python package to realize token sharing and simplify the interface import.
First of all, we need to understand what features the python package has
(1) When importing __init__.py, if there is runnable code, the code will be executed
(2) Import will only be imported once, that is, no matter how many times it is imported, it will actually only run once
Therefore, we only need to create a package of preprocessing methods, such as the PreProcessing package, encapsulate all the methods that need to be used in this package, and then in __init__.py
1. Import all the methods that need to be imported
# Coding = UTF-8 __all__ = [ 'Core Base Core', 'Core Core', 'Core Dept', 'Cores' ' Core User ', ' LisApplication ', ' RtidLisSetProjectMapping ', ' RtipLisAntibiotic ', ' RtipLisMidClinicalApply ', ' RtipLisOut ' ' RtipLisQueuingWindows', 'RtipLisReport', 'RtipLisReportCheckCondition', 'RtipLisReportProject', 'RtipLisReportUnit', 'RtipLisUnitProjectMapping' ]
import requests import unittest import json from random import choice import datetime import random import string import pymysql import os import time
2. Realize the login method to obtain the token
admin_params = {'username': '***', 'password': '***'} base_url = 'http://***'
def get_token(params): """ 登录 """ login_url = base_url + '/api/auth/jwt/token' login_headers = { # 'Content-Length': '39', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36', 'Content-Type': 'application/json;charset=UTF-8', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9' } try: r = requests.post(url=login_url, json=params, headers=login_headers) response = json.loads(r.text) req_token = response['data']['token'] return req_token except KeyError: print('Get Token Failed') req_token = get_token(params)
3. In addition, you can also define some common parameters
LEVEL ='debug' # Log level PTYPE = (1, 2) # Type SEX_CODE = (1, 2, 3, 4) # Gender AGE_UNIT_CODE = ('day','month','year') # Age unit USE_FLAG = (1, 0) # Enable flag PRINT_NOW = (0, 1) # Now print the barcode BUSINESS_TYPE = (1, 2, 3, 4, 5, 6, 7) # Report item business type 1 common
Finally, in all test classes, the content declared in the above __init__.py can be used
# coding=utf-8 import sys sys.path.append('..') import PreProcessing as p from PreProcessing import * class Accept(p.unittest.TestCase): @classmethod def setUpClass(cls): cls.header, cls.org_id, cls.user_id = p.header, p.org_id, p.user_id print('header:', cls.header) cls.url_test = p.base_url + '/api/rtip/lis/application/accept' def setUp(self): t = LisApplication.LisApplication(p.base_url, self.__class__.org_id, self.__class__.header) self.app_detail = t.get_app_detail('1') print('app_detail', self.app_detail) self.app_detail['applicationStatus'] = '2'# Update the application status to 2 accepted c = CoreBaseCore.CoreBaseCore(p.base_url, self.__class__.org_id, self.__class__.header) self.doctor_id = c.get_core_technician() print('doctor_id', self.doctor_id) def test_accept_sample_001(self): pass def tearDown(self): pass @classmethod def tearDownClass(cls): pass if __name__ == '__main__': p.unittest.main()