Backend Development 5 min read

Automated Testing of WeChat Mini Program APIs with Python, Requests, and Beautiful Report

This guide explains how to use Python3, the Requests library, and Beautiful Report to perform automated API testing for WeChat Mini Programs, covering prerequisite knowledge, obtaining an access token, writing test cases, and generating detailed HTML test reports.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Automated Testing of WeChat Mini Program APIs with Python, Requests, and Beautiful Report

In WeChat Mini Program development, automated testing is essential for ensuring quality; Python is widely used for this purpose, and a clear, detailed test report is valuable for both testers and developers.

Prerequisite Knowledge

Before starting, you should be familiar with basic Python3 programming, HTTP protocol fundamentals, the concepts and development workflow of WeChat Mini Programs, and the basic usage of the Beautiful Report library.

1. Get access_token

To test WeChat Mini Program interfaces, an access_token is required. The following code obtains it:

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']

The code sends a GET request to the token endpoint with APPID and APPSECRET, parses the JSON response, and returns the access_token.

2. Write test case

Using the requests library, the following example tests the user authorization interface:

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

The test retrieves the access_token, calls the user‑info API, and uses assert statements to verify the HTTP status and the presence of openid and session_key in the response.

3. Generate test report

Beautiful Report creates a visually appealing HTML report. The code below assembles the test suite and produces the report:

import os
import time
import requests
from beautiful_report import BeautifulReport

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

def get_access_token():
    ...

def test_get_user_info(js_code):
    ...

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 script creates a test suite, adds the test case, ensures the report directory exists, and saves the HTML report with a timestamped filename.

Summary

The generated report displays each test case's HTTP request and response details, including screenshots, helping testers and developers quickly understand results, identify issues, and improve testing efficiency and quality.

WeChat Mini ProgramAPI TestingRequestsBeautifulReport
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login 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.