Skip to main content

UTest 自动化测试框架 - 快速上手

本框架基于 ubox-py-sdk,提供简洁的测试用例编写与执行能力:步骤与断言、截图、录制、logcat 采集、性能采集与报告导出等。

1. 环境与安装

  • Python 3.9+(推荐按 run.sh 中方式创建虚拟环境)
  • 依赖通过 requirements.txt 安装

安装依赖(清华源示例):

uv pip install -U -r requirements.txt --index-url https://pypi.tuna.tsinghua.edu.cn/simple

2. 基础配置(config.yml)

关键项:

  • task.serial_num:设备序列号
  • task.os_type:android | ios | hm
  • task.app_name:被测应用包名或安装包
  • test.selected_tests:要执行的用例名称列表(为空执行全部)
  • test.selected_dir:要执行的测试用例目录路径(相对于test_cases目录,如"test-a"或"test-a/b",为空执行全部)

用例选择优先级:resource参数 > test.selected_tests > test.selected_dir(高优先级覆盖低优先级)

3. 运行测试

  • 更改配置文件后直接运行(读取 config.yml):
uv run start_test.py

执行后输出目录:

  • test_result/log/<用例名>/:用例日志(perf.json、logcat.txt 等)
  • test_result/case/<用例名>/pic/:截图
  • Excel 报告:在 test_result/log 根目录生成

4. 如何编写用例

参考:

  • 真实业务:test_cases/user_tests_0.py(腾讯视频小游戏)
  • Demo:test_cases/user_tests_1.py(框架能力演示)

示例骨架:

from core.test_case import TestCase, StepStatus, FailureStrategy
from ubox_py_sdk import Device

class MyTest(TestCase):
    def __init__(self, device: Device):
        super().__init__(name="我的用例", description="用例描述", device=device)
        self.failure_strategy = FailureStrategy.STOP_ON_FAILURE

    def setup(self):
        # (可选)启动被测应用
        pkg = self.get_package_name()
        if pkg:
            self.start_step("启动应用", f"启动: {pkg}")
            ok = self.device.start_app(pkg)
            self.assert_true("应用应成功启动", ok)
            self.end_step(StepStatus.PASSED if ok else StepStatus.FAILED)
        # 录制(路径自动记录到结果)
        self.start_record()
        # logcat(仅记录文件路径,不需要手动停止)
        self.start_logcat()

    def run_test(self):
        # 步骤与断言
        self.start_step("准备场景", "示例")
        self.assert_true("环境应就绪", True)
        self.end_step(StepStatus.PASSED)

        # 关键阶段开启性能采集
        self.start_step("开启性能采集", "开始perf")
        started = self.start_perf()
        self.assert_true("性能采集应成功启动", started)
        self.end_step(StepStatus.PASSED if started else StepStatus.FAILED)

        # 业务操作...
        # ...
        # 停止性能采集:停止后自动解析 get_log_dir()/perf.json
        self.stop_perf()

    def teardown(self):
        # 停止录制
        self.stop_record()
        # (可选)关闭应用并回到桌面
        pkg = self.get_package_name()
        if pkg:
            self.device.stop_app(pkg)
        self.device.press("HOME")

要点与要求:

  • 用例名称/描述:在 init 的 name、description 设置。
  • 步骤管理:start_step/end_step;在步骤内断言失败会自动标记结果,end_step 不是必须显式调用,但建议在成功路径显式结束以便报告更清晰。
  • 断言:assert_true、assert_equal、assert_contains 等;失败按策略决定是否中断。
  • 截图:失败/通过(可配置)时自动截图,也可随时 take_screenshot("name")。
  • 录制:start_record()/stop_record();只记录文件路径,报告展示。
  • logcat:start_logcat();记录输出文件路径(logcat.txt),报告展示。
  • 性能:start_perf()/stop_perf();设备端在停止时写入 get_log_dir()/perf.json,框架自动解析并汇总 FPS/Jank/CPU/GPU/温度/功耗/内存/网络等指标到报告。

5. 报告

  • 自动生成 Excel 报告,包含:
    • 用例信息与状态
    • 步骤详情与截图
    • 录制与 logcat 文件路径
    • 性能监控关键指标(从 perf.json 解析的汇总)
    • 整体退出码(用于判断测试执行结果)

6. 退出码说明

测试框架的退出码分为两层:任务级退出码与用例级退出码。

6.1 任务级退出码(report_info.exit_code)

表达整个测试任务的最终结果。断言失败(业务不通过)与脚本执行报错(流程出问题)严格区分,不合并:

退出码 含义 status 说明
0 成功 success 所有测试用例执行完成且全部通过
5 断言失败 failed 执行流程正常,仅存在断言失败/应用崩溃/应用无响应用例
2 脚本执行报错 error 存在用例执行异常(非断言异常),或框架准备/运行阶段异常
3 安装失败 error 应用安装阶段失败
10 手机掉线 error 设备离线/初始化失败

说明:断言失败属于正常测试结论,退出码为 5;只有脚本/框架执行本身出错才返回 2。 若同时存在断言失败与执行报错的用例,以执行报错(2)为准。

6.2 用例级退出码(test_results[].exit_code)

表达单个测试用例的具体结果,用于精确定位每个用例的失败原因:

退出码 含义 说明
0 成功 用例通过
2 脚本异常 用例执行过程中抛出非断言的异常
5 脚本断言失败 用例中的断言失败
17 应用崩溃 该用例执行期间检测到应用崩溃
18 应用无响应(ANR) 该用例执行期间检测到应用无响应

退出码显示位置:

  • 任务级:Excel"测试概览"工作表的"退出码"字段、JSON 的 report_info.exit_code
  • 用例级:JSON 的 test_results[].exit_code

7. 建议

  • 步骤小而清晰、断言贴近业务、仅在关键路径采集性能、出错即截图便于复盘。

编译

uv build

发布包

uv publish --publish-url https://mirrors.tencent.com/repository/pypi/tencent_pypi/simple
uv publish

Metadata

Release files for utest-auto-core 0.3.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for utest-auto-core 0.3.6
File Size Uploaded Provenance Actions
utest_auto_core-0.3.6.tar.gz 199.3 kB Without provenance Details for utest_auto_core-0.3.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for utest-auto-core 0.3.6
File Interpreter ABI Platform Actions
utest_auto_core-0.3.6-py3-none-any.whl Python 3 none any Details for utest_auto_core-0.3.6-py3-none-any.whl

Total release size: 297.1 kB

Release files / utest_auto_core-0.3.6.tar.gz

Download URL utest_auto_core-0.3.6.tar.gz
Size 199.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c835a00eb6d48b928a18ca6e1b2fa5fdc7c868c0b37b63fae7ae41907101aa2e
BLAKE2b-256 checksum
How to use checksums
07f2c10872fa6a225a57e2437c38ba7413f95d6a84c80537fa4f5cdbf19253e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / utest_auto_core-0.3.6-py3-none-any.whl

Download URL utest_auto_core-0.3.6-py3-none-any.whl
Size 97.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c2389788da862cf7cec173a1c948bf01c9e7b069da4880f56debf27e50e67abe
BLAKE2b-256 checksum
How to use checksums
20ea4bb616212c8353ca3a20bcc16ccb5ec84840153ea52842bd5afdf264876b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for utest-auto-core 0.3.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.6 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page