Automated API Testing for WeChat Mini Programs Using Python, Requests, and Beautiful Report

This article demonstrates how to automate testing of WeChat mini‑program interfaces by obtaining an access token, writing test cases with the Python requests library, and generating detailed, visually appealing test reports using the Beautiful Report library.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Automated API Testing for WeChat Mini Programs Using Python, Requests, and Beautiful Report

In WeChat mini‑program development, automated testing is essential for ensuring quality, and Python is a popular language for implementing such tests. A clear and attractive test report benefits both testers and developers.

Prerequisite knowledge

Basic Python 3 programming

Fundamentals of the HTTP protocol

Understanding of WeChat mini‑program concepts and development workflow

Basic usage of the Beautiful Report library

1. Obtain access_token

Before testing the WeChat mini‑program API, an access_token must be retrieved. The following Python code demonstrates how to request the token using the requests library.

import requests
APPID = 'your appid'
APPSECRET = 'your appsecret'

def get_access_token():
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + APPID + '&secret=' + APPSECRET
    response = requests.get(url)
    response_json = response.json()
    return response_json['access_token']

2. Write test case

Using the retrieved token, a test case is written to call the user‑authorization API and verify the response. The assert statements check the HTTP status and required fields.

def test_get_user_info():
    access_token = get_access_token()
    url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + APPID + '&secret=' + APPSECRET + '&js_code=' + js_code + '&grant_type=authorization_code'
    headers = {'Content-Type': 'application/json'}
    response = requests.get(url, headers=headers)
    assert response.status_code == 200
    response_json = response.json()
    assert 'openid' in response_json
    assert 'session_key' in response_json

3. Generate test report

The Beautiful Report library is used to create a detailed HTML report. The code below sets up a test suite, adds the test case, and saves the report to a timestamped file.

import os
import time
import requests
from beautiful_report import BeautifulReport

APPID = 'your appid'
APPSECRET = 'your appsecret'

# get_access_token and test_get_user_info definitions omitted for brevity

if __name__ == '__main__':
    js_code = 'your js code'
    suite = BeautifulReport(unittest.TestSuite())
    suite.addTest(test_get_user_info(js_code))
    now = time.strftime('%Y-%m-%d_%H-%M-%S')
    report_dir = './reports'
    if not os.path.exists(report_dir):
        os.mkdir(report_dir)
    report_filename = 'test_report_' + now + '.html'
    report_path = os.path.join(report_dir, report_filename)
    suite.report(filename=report_path, description='测试微信小程序接口')

The generated report lists each test case, shows the HTTP request and response details, and includes screenshots, helping developers quickly identify and fix issues while maintaining a history of test results.

Overall, this approach provides an automated, repeatable way to test WeChat mini‑program APIs and produce comprehensive, visually appealing reports.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonWeChatBeautiful Report
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.