Practical Guide to API Automation and Platform Usage
This guide explains how to integrate the DeWu API automation platform, detailing usage scenarios for developers, testers and data generators, optimal timing for case creation, maintenance‑reducing templates, reusable components, test‑plan execution, and common pitfalls with solutions, ultimately streamlining test management and boosting efficiency.
API automation has become a routine part of each iteration. This article shares practical experiences and ideas after integrating the DeWu API automation platform, covering usage scenarios, benefits, timing, maintenance, and common methods.
1. Usage scenarios & benefits
For developers – improves self‑testing efficiency and test quality. After the platform is adopted, developers can easily find relevant cases, use them for data generation or self‑testing, and provide feedback that helps discover bugs.
For testers – boosts testing efficiency through smoke testing, bug verification, and regression testing. Selecting relevant cases for smoke tests can quickly block downstream testing if core flows fail.
For anyone needing data – automation scripts can serve as lightweight data‑generation tools when a full data‑generation platform is overkill.
2. When to write automation cases
Usually after the test case review and before developers hand over for testing, when the interface and parameters are clear. If time is limited, basic checks can be added early and refined later.
3. Reducing maintenance cost
Cases are either self‑written (requiring regular upkeep) or authored by others (often suffering from inconsistent coding styles). The platform standardizes templates, allowing simple updates of parameters and assertions when requirements change.
Additional tips: design stable cases, avoid flaky failures, use pre‑scripts to set up data, and add retry counts to mitigate environment instability.
4. Practices on the automation platform
4.1 Writing scenario cases
Example: a new user receives a coupon that triggers a “amount inflation” effect, which should succeed only once.
Key steps: create a new user, add to AB whitelist, verify coupon status before claiming, and validate database records after claiming.
import json
import requests
from util.db_mysql import DBMySQL
from util.db_redis import DbRedis
def call(env_vars, g_vars, l_vars, sys_funcs, asserts, logger, **kwargs):
userId = l_vars.get('userId')
n = int(userId)%4
dbA = DBMySQL(env_vars.get("db.A"))
dbB = DBMySQL(env_vars.get("db.B"))
try:
sql_1 = "SELECT * FROM table_A WHERE user_id = %s;"%userId
user_coupon_info = dbA.select(sql_1)
asserts.assertEqual(user_coupon_info[0].get("status"), 1, msg="数据表领券状态为true")
asserts.assertEqual(user_coupon_info[0].get("type"), 0, msg="当前券类型为0")
asserts.assertIsEmpty(user_coupon_info[0].get("coupon1"), msg="无资产1")
asserts.assertIsEmpty(user_coupon_info[0].get("coupon2"), msg="无资产2")
asserts.assertIsEmpty(user_coupon_info[0].get("coupon4"), msg="无资产4")
asserts.assertIsNotEmpty(user_coupon_info[0].get("info"), msg="券包信息非空")
group = user_coupon_info[0].get("group")
asserts.assertNotEqual(group, 0, msg="用户命中对照组,无膨胀券")
sql_2 = "SELECT * FROM table_B WHERE id = 50%s and deleted=0"%group
coupon_config = dbA.select(sql_2)
content = json.loads(coupon_config[0].get("content_info"))
for i in range(3):
activityId = content[i]["activityId"]
l_vars.set('activityId_{}'.format(i+1), activityId)
sql_3 = "SELECT * FROM a_coupon_%s WHERE user_id = %s and activity_id = %s;"%(n,userId,activityId)
coupon_res = dbB.select(sql_3)
if(i==0):
asserts.assertIsEmpty(coupon_res, msg="未到账资产1")
if(i==2):
asserts.assertIsNotEmpty(coupon_res, msg="到账资产3")
finally:
dbA.close()
dbB.close()After coupon claim, verify inflation by checking the coupon table and the business account table.
import json
import requests
from util.db_mysql import DBMySQL
from util.db_redis import DbRedis
def call(env_vars, g_vars, l_vars, sys_funcs, asserts, logger, **kwargs):
select_tableB_res = l_vars.get('select_tableB_res')
asserts.assertIsNotEmpty(select_tableB_res, msg="到账资产1")4.2 Writing reusable components
Common functions (e.g., querying coupon issuance records) can be encapsulated as public components. Parameters may be passed directly or retrieved from variable space if not provided.
import json
import requests
from util.db_mysql import DBMySQL
from util.db_redis import DbRedis
def call(env_vars, g_vars, l_vars, sys_funcs, asserts, logger, **kwargs):
call_param = sys_funcs.get_call_param()
userId = call_param.get('userId')
activityId = call_param.get('activityId')
dbA = DBMySQL(env_vars.get("db.A"))
if not userId:
userId = l_vars.get(call_param.get('var_userId'))
if not activityId:
activityId = l_vars.get(call_param.get('var_activityId'))
if not userId and not activityId:
raise '请传入查询条件'
try:
if not activityId:
sql = "SELECT * FROM a_coupon_%s WHERE user_id = %s;"%(int(userId)%4,userId)
elif not userId:
sql = "SELECT * FROM a_coupon_%s WHERE activity_id = %s;"%(n,activityId)
else:
sql = "SELECT * FROM a_coupon_%s WHERE user_id = %s and activity_id = %s;"%(int(userId)%4,userId,activityId)
res = dbA.select(sql)
l_vars.set("select_tableA_res",res)
finally:
dbA.close()
return res4.3 Executing test plans
Configure a test plan on the platform, select dependent applications, set execution frequency, and bind automated cases. After execution, the platform updates case status automatically, improving efficiency.
5. Common issues and solutions
5.1 Redis returns data with a leading b' (bytes). Solution: either disable the platform’s tool and use standard Redis client with decode_responses=True , or decode the bytes manually.
import redis
redisConn = redis.Redis(host='redis.host', port=666, password='test123', db=1, decode_responses=True)Alternative using platform tool:
re = DbRedis.ger_redis(link_info)
test = re.get(test_key)
test_str = test.decode(encoding='utf-8')
key = key + test_str
re.set(key, "aaa")5.2 Database DML statements appear successful but changes are not persisted. Solution: call db.commit() for INSERT/UPDATE/DELETE; SELECT does not require a commit.
dbA = DBMySQL(db_A)
sql = "INSERT INTO t(name,age) VALUES (%s, %s);"
try:
res = db.insert(sql, ['lucy', 18])
db.commit()
finally:
dbA.close()5.3 HTTP component JSON body with Chinese characters throws errors. Solution: set request header Content-Type: application/json;charset=UTF-8 .
5.4 Switching between multiple random accounts in a single case. Store login headers in a variable and reuse them.
def call(env_vars, g_vars, l_vars, sys_funcs, asserts, logger, **kwargs):
l_vars.set("user1", l_vars.get("sys.public.login.headers"))Later reuse:
def call(env_vars, g_vars, l_vars, sys_funcs, asserts, logger, **kwargs):
l_vars.set("sys.public.login.headers", l_vars.get("user1"))Conclusion
Integrating the automation platform greatly simplifies test case management, data generation, and execution. It saves time, allowing teams to focus on exploring edge cases and improving test coverage.
DeWu Technology
A platform for sharing and discussing tech knowledge, guiding you toward the cloud of technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.