Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ coverage.xml
platform-tests.xml
platform-partition.json
htmlcov/
test_reports/
.pytest_cache/
.ruff_cache/
.mypy_cache/
Expand Down
14 changes: 11 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ may point to an existing Chromium executable instead of downloading one.
errors. Ownership/teardown guidance lives in [tests](tests/AGENTS.md).
- Coverage is measured with branches enabled. The overall floor and independent
critical-file floors are documented with [CI](.github/workflows/AGENTS.md).
- `tests/run_tests.sh` assumes a developer-specific Conda path and writes
`test_reports/`; prefer direct pytest. `.pt-snap/`, `.tmp/`, `.worktrees/`,
and `tmp/` are ignored and excluded from root Ruff/Black checks.
- `bash tests/run_tests.sh` is an optional Bash entry point using the active
`python -m pytest`; set `PYTHON=/path/to/python` to choose another executable.
It does not activate environments or install dependencies. Full mode retains
the configured coverage floor and writes ignored `test_reports/` output;
`--reports-dir DIR` changes that destination. `--quick` omits coverage/report
generation and is **not** a substitute for CI's full coverage gate. Use
`--help` for file/keyword/marker selection and `--` for extra pytest arguments.
Pytest's exit status is preserved, including failure and no-tests-collected.
Without Bash, use the direct Python/pytest commands above. `.pt-snap/`,
`.tmp/`, `.worktrees/`, and `tmp/` are ignored and excluded from root
Ruff/Black checks.
- `python -m build` writes `dist/` and build intermediates and may download build
requirements. See [packaging](src/AGENTS.md) before using it as verification.

Expand Down
303 changes: 126 additions & 177 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,193 +1,142 @@
#!/bin/bash
#!/usr/bin/env bash

# 测试执行与报告生成脚本
# 用于运行所有测试并生成详细的测试报告
# Use the caller's Python environment; never activate or install an environment.
set -eo pipefail

set -e
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PYTHON="${PYTHON:-python}"
REPORTS_DIR="test_reports"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
show_help() {
cat <<HELP
Usage: bash tests/run_tests.sh [OPTIONS] [all] [-- PYTEST_ARGS...]

# 项目根目录
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TESTS_DIR="${PROJECT_ROOT}/tests"
REPORTS_DIR="${PROJECT_ROOT}/test_reports"
Run pytest with the active Python from PATH, or the executable set in PYTHON.
Requires Bash and dependencies already installed in that Python environment.
Paths are resolved from the repository root, regardless of the caller's CWD.

# 激活 conda 环境
activate_conda() {
echo -e "${BLUE}>>> 激活 conda 环境...${NC}"
if [ -f "/Users/test1/liuyekang/miniconda3/etc/profile.d/conda.sh" ]; then
. /Users/test1/liuyekang/miniconda3/etc/profile.d/conda.sh
conda activate openclaw
echo -e "${GREEN}✓ Conda 环境激活成功${NC}"
else
echo -e "${RED}✗ Conda 初始化脚本不存在${NC}"
exit 1
fi
}
Options:
-h, --help Show this help without running Python
-q, --quick Omit coverage and report generation (not a CI gate)
-f, --file FILE Select a test file/node (repeatable); bare names use tests/
-k, --keyword EXPR Select tests by pytest keyword expression
-m, --mark EXPR Select tests by pytest marker expression
-x, --stop Stop after the first failure
--reports-dir DIR Coverage output directory (default: test_reports/)
-- Forward remaining arguments unchanged to pytest

With no --file selection, run tests/. Full mode retains the configured coverage
floor and writes HTML/XML reports under the ignored test_reports/ directory.
Quick mode adds no coverage options and creates no report directory. Arbitrary
pytest options belong after --. Select files/nodes with -f: positional selectors
passed after -- are additive to the default tests/ selection. The pytest exit
code is returned unchanged.

# 创建报告目录
create_report_dir() {
echo -e "${BLUE}>>> 创建报告目录...${NC}"
mkdir -p "${REPORTS_DIR}/htmlcov"
mkdir -p "${REPORTS_DIR}/xml"
echo -e "${GREEN}✓ 报告目录创建成功${NC}"
Examples:
bash tests/run_tests.sh
bash tests/run_tests.sh --quick -f test_package.py
bash tests/run_tests.sh -m 'not slow and not nightly' -- -q
PYTHON=/path/to/venv/bin/python bash tests/run_tests.sh --quick -- --collect-only
HELP
}

# 运行测试
run_tests() {
local test_pattern="${1:-tests/}"

echo -e "${BLUE}>>> 运行测试:${test_pattern}${NC}"
echo "========================================"

pytest "${test_pattern}" \
--cov=pt_snap_cli \
--cov-report=html:"${REPORTS_DIR}/htmlcov" \
--cov-report=xml:"${REPORTS_DIR}/xml/coverage.xml" \
--cov-report=term-missing \
-v \
--tb=short \
"${@:2}"

local exit_code=$?

if [ $exit_code -eq 0 ]; then
echo -e "${GREEN}✓ 所有测试通过${NC}"
else
echo -e "${RED}✗ 部分测试失败${NC}"
fi

return $exit_code
usage_error() {
printf 'Error: %s\nUse --help for usage.\n' "$1" >&2
exit 2
}

# 生成报告摘要
generate_summary() {
echo ""
echo "========================================"
echo -e "${BLUE}>>> 测试报告摘要${NC}"
echo "========================================"

if [ -f "${REPORTS_DIR}/xml/coverage.xml" ]; then
echo -e "${GREEN}✓ 覆盖率报告已生成${NC}"
echo " - HTML 报告:${REPORTS_DIR}/htmlcov/index.html"
echo " - XML 报告:${REPORTS_DIR}/xml/coverage.xml"
require_value() {
if [[ $# -lt 2 || -z "$2" || "$2" == -* ]]; then
usage_error "$1 requires a value"
fi

echo ""
}

# 显示帮助信息
show_help() {
echo "用法:$0 [选项] [测试模式]"
echo ""
echo "选项:"
echo " -h, --help 显示帮助信息"
echo " -q, --quick 快速模式(不生成覆盖率报告)"
echo " -f, --file FILE 运行指定测试文件"
echo " -k, --keyword KEYWORD 运行匹配关键字的测试"
echo " -m, --mark MARK 运行标记的测试(如 slow)"
echo " -x, --stop 遇到失败立即停止"
echo ""
echo "测试模式:"
echo " all 运行所有测试(默认)"
echo " unit 只运行单元测试"
echo " integration 只运行集成测试"
echo ""
echo "示例:"
echo " $0 # 运行所有测试并生成报告"
echo " $0 -q # 快速运行所有测试"
echo " $0 -f test_cli.py # 运行指定测试文件"
echo " $0 -k test_render # 运行包含关键字的测试"
echo " $0 -m slow # 运行标记为 slow 的测试"
echo " $0 -x # 遇到失败立即停止"
normalize_path() {
# Git Bash accepts native drive/UNC paths when their separators are forward
# slashes. Leave POSIX filenames (which can contain backslashes) unchanged.
case "$1" in
[A-Za-z]:\\*|\\\\*) printf '%s\n' "${1//\\//}" ;;
*) printf '%s\n' "$1" ;;
esac
}

# 主函数
main() {
local quick_mode=false
local pytest_args=()

# 解析命令行参数
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-q|--quick)
quick_mode=true
shift
;;
-f|--file)
pytest_args+=("tests/$2")
shift 2
;;
-k|--keyword)
pytest_args+=("-k" "$2")
shift 2
;;
-m|--mark)
pytest_args+=("-m" "$2")
shift 2
;;
-x|--stop)
pytest_args+=("-x")
shift
;;
all)
shift
;;
unit|integration)
echo -e "${YELLOW}警告:$1 模式暂未实现,将运行所有测试${NC}"
shift
;;
*)
echo -e "${RED}未知选项:$1${NC}"
echo "使用 -h 查看帮助"
exit 1
;;
esac
done

# 进入项目目录
cd "${PROJECT_ROOT}"

echo "========================================"
echo -e "${BLUE} 测试执行与报告生成脚本${NC}"
echo "========================================"
echo ""

# 激活环境
activate_conda

# 创建报告目录
create_report_dir

# 运行测试
if [ "$quick_mode" = true ]; then
echo -e "${YELLOW}>>> 快速模式:跳过覆盖率报告${NC}"
run_tests "${pytest_args[@]}"
else
echo -e "${BLUE}>>> 完整模式:生成覆盖率报告${NC}"
run_tests "${pytest_args[@]}"
fi

local test_result=$?

# 生成报告摘要
generate_summary

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}测试完成!${NC}"
echo -e "${BLUE}========================================${NC}"

exit $test_result
}
quick_mode=false
test_paths=()
pytest_args=()
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
-q|--quick)
quick_mode=true
shift
;;
-f|--file)
require_value "$@"
file_path="$(normalize_path "$2")"
case "$file_path" in
/*|[A-Za-z]:/*|tests/*|./tests/*) test_paths+=("$file_path") ;;
*) test_paths+=("tests/$file_path") ;;
esac
shift 2
;;
-k|--keyword)
require_value "$@"
pytest_args+=("-k" "$2")
shift 2
;;
-m|--mark)
require_value "$@"
pytest_args+=("-m" "$2")
shift 2
;;
-x|--stop)
pytest_args+=("-x")
shift
;;
--reports-dir)
require_value "$@"
REPORTS_DIR="$(normalize_path "$2")"
shift 2
;;
all)
shift
;;
--)
shift
pytest_args+=("$@")
break
;;
*)
usage_error "unknown option or mode: $1"
;;
esac
done

cd "$PROJECT_ROOT"
if ! command -v "$PYTHON" >/dev/null 2>&1; then
printf 'Error: Python executable not found: %s\n' "$PYTHON" >&2
exit 127
fi

if [[ ${#test_paths[@]} -eq 0 ]]; then
test_paths=("tests/")
fi

coverage_args=()
if [[ "$quick_mode" == false ]]; then
mkdir -p "$REPORTS_DIR/htmlcov" "$REPORTS_DIR/xml"
coverage_args=(
--cov=pt_snap_cli
"--cov-report=html:$REPORTS_DIR/htmlcov"
"--cov-report=xml:$REPORTS_DIR/xml/coverage.xml"
--cov-report=term-missing
)
printf 'Coverage reports: %s\n' "$REPORTS_DIR"
fi

# 执行主函数
main "$@"
# exec preserves pytest's success, failure, interruption and collection exit codes.
exec "$PYTHON" -m pytest "${test_paths[@]}" "${coverage_args[@]}" \
-v --tb=short "${pytest_args[@]}"
Loading
Loading