Files
soul-yongping/scripts/test/conftest.py
卡若 76965adb23 chore: 清理敏感与开发文档,仅同步代码
- 永久忽略并从仓库移除 开发文档/
- 移除并忽略 .env 与小程序私有配置
- 同步小程序/管理端/API与脚本改动

Made-with: Cursor
2026-03-17 17:50:12 +08:00

68 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
共享 fixturesbase_url、admin_token、miniapp_token
"""
import pytest
import requests
from config import (
API_BASE,
ADMIN_USERNAME,
ADMIN_PASSWORD,
MINIAPP_DEV_USER_ID,
get_env_banner,
)
def pytest_report_header(config):
"""pytest 报告头部显示测试环境,避免误测"""
return get_env_banner().strip().split("\n")
@pytest.fixture(scope="session")
def base_url():
"""API 基础地址"""
return API_BASE
@pytest.fixture(scope="session")
def admin_token(base_url):
"""
管理端 JWT。通过 POST /api/admin 登录获取。
失败时返回空字符串,用例可 skip。
"""
try:
r = requests.post(
f"{base_url}/api/admin",
json={"username": ADMIN_USERNAME, "password": ADMIN_PASSWORD},
timeout=10,
)
data = r.json()
if data.get("success") and data.get("token"):
return data["token"]
except Exception:
pass
return ""
@pytest.fixture(scope="session")
def miniapp_token(base_url):
"""
小程序 token。通过 POST /api/miniprogram/dev/login-as 获取(仅 APP_ENV=development
若 MINIAPP_DEV_USER_ID 未配置或接口不可用,返回空字符串。
"""
if not MINIAPP_DEV_USER_ID:
return ""
try:
r = requests.post(
f"{base_url}/api/miniprogram/dev/login-as",
json={"userId": MINIAPP_DEV_USER_ID},
timeout=10,
)
data = r.json()
if data.get("success") and data.get("data", {}).get("token"):
return data["data"]["token"]
except Exception:
pass
return ""