本文档说明如何使用细粒度的pytest标记来运行不同类型的测试。
bt_api_py项目包含大量测试,其中很多是公开行情数据测试(不需要鉴权),, 有些是需要鉴权的测试。为了方便测试和CI/CD流程 , 我们引入了细粒度的测试标记系统。
这些测试访问公开市场数据, 不需要API密钥或账户配置:
ticker: Ticker/Tick 行情数据测试kline: K线/Bar数据测试orderbook: 订单簿/深度数据测试public_trade: 公开成交记录测试
这些测试需要真实的API密钥和账户配置:
auth_account: 账户和余额测试auth_order: 订单管理测试auth_position: 持仓管理测试auth_private_trade: 私有成交记录测试
network: 需要网络访问的测试integration: 集成测试slow: 慢速测试(> 1s)
# 运行所有ticker测试
pytest tests -m ticker
# 运行所有kline测试
pytest tests -m kline
# 运行所有orderbook测试
pytest tests -m orderbook
# 运行所有public_trade测试
pytest tests -m public_trade
# 运行所有公开行情数据测试
pytest tests -m "ticker or kline or orderbook or public_trade"
# 排除需要鉴权的测试
pytest tests -m "not (auth_account or auth_order or auth_position or auth_private_trade)"# 运行账户测试(需要配置.env文件)
pytest tests -m auth_account
# 运行订单测试
pytest tests -m auth_order
# 运行持仓测试
pytest tests -m auth_position
# 运行所有鉴权测试
pytest tests -m "auth_account or auth_order or auth_position or auth_private_trade"我们提供了便捷脚本来运行测试:
# 运行所有行情数据测试
./scripts/run_market_tests.sh all
# 只运行ticker测试
./scripts/run_market_tests.sh ticker
# 只运行kline测试(详细输出)
./scripts/run_market_tests.sh kline -v
# 运行所有行情测试(8个worker,生成HTML报告)
./scripts/run_market_tests.sh all -n 8 --html# 运行所有鉴权测试(需要配置.env)
./scripts/run_auth_tests.sh all
# 只运行订单测试
./scripts/run_auth_tests.sh order
# 只运行账户测试(详细输出)
./scripts/run_auth_tests.sh account -v
# 运行所有鉴权测试(生成HTML报告)
./scripts/run_auth_tests.sh all --htmlname: Tests
on: [push, pull_request]
jobs:
public-market-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: pip install -e ".[dev]"
- name: Run public market data tests
run: pytest tests -m "ticker or kline or orderbook or public_trade" -n 4
auth-tests:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: pip install -e ".[dev]"
- name: Run auth tests
env:
BINANCE_API_KEY: ${{ secrets.BINANCE_API_KEY }}
BINANCE_SECRET_KEY: ${{ secrets.BINANCE_SECRET_KEY }}
OKX_API_KEY: ${{ secrets.OKX_API_KEY }}
OKX_SECRET_KEY: ${{ secrets.OKX_SECRET_KEY }}
OKX_PASSPHRASE: ${{ secrets.OKX_PASSPHRASE }}
run: pytest tests -m "auth_account or auth_order" -n 2当前标记统计(2026-03-08):
auth_account: 67 个测试auth_order: 36 个测试auth_position: 30 个测试auth_private_trade: 8 个测试ticker: 4 个测试kline: 1 个测试orderbook: 1 个测试public_trade: 7 个测试
# 快速验证(只跑不需要鉴权的测试)
pytest tests -m "ticker or kline" -n 4
# 完整验证(包括鉴权测试)
pytest tests -m "not network" -n 4# PR 应该至少跑通公开行情测试
pytest tests -m "ticker or kline or orderbook or public_trade" -n 4# 发布前应该运行所有测试(包括鉴权测试)
pytest tests -m "not network" -n 4
pytest tests -m "auth_account or auth_order or auth_position" -n 2原因: 测试被标记为 network 但没有运行network测试
解决:
# 确保包含network标记
pytest tests -m "network and ticker"原因: 没有配置 .env 文件或API密钥无效
解决:
- 创建
.env文件 - 配置正确的API密钥
- 确保账户有足够余额
- 确保IP在白名单中
- 2026-03-08: 初始版本,添加细粒度测试标记
- 2026-03-08: 添加便捷测试运行脚本