---
title: "【ChatGPT】游戏自动化测试实践案例 - 更多案例"
author: "Perrin Yong"
author_profile: https://www.pystone.net/profile/
published_by: "Perrin Yong"
canonical: https://www.pystone.net/notes/chatgpt-game-autotest-more-cases/
type: note
content_role: unspecified
visibility: public
id_stability: rename-stable
source_path: "99-资料来源、摘录与归档/02-AI对话与研究原始输出/【ChatGPT】游戏自动化测试实践案例 - 更多案例.md"
content_hash: d7be0c3e66d1085acf7b27f7003c30cfcc9552f2bd92cbc2dc78e51a4fa19df8
knowledge_version: 224c990773de.5fa8af6e39fa
site_commit: 224c990773de166d23a886306577dd90379529ce
notes_commit: 5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a
---
# 【ChatGPT】游戏自动化测试实践案例 - 更多案例

> **note · ChatGPT 问答录**
> 本文保存一次 ChatGPT 问答原始输出，仅作为资料来源；其中结论未自动视为已验证知识，使用前应结合可靠来源复核。


> 创建时间：2023/6/11 19:06

#### 目标：测试游戏的可用性和稳定性

场景：测试游戏在连续运行多个小时后是否仍然稳定可用。

实例：

python

import time

def run_game_stability_test(duration_hours): # 登录游戏 login("valid_user", "valid_password")

start_time = time.time()

while time.time() - start_time < duration_hours * 3600:

try:

navigate_to("shop")

navigate_to("battle")

navigate_to("profile")

## 检查游戏是否仍然稳定可用 assert poco("game_main_ui").exists(), "游戏崩溃或不稳定！" except Exception as e:

print(f"游戏稳定性测试中发生错误：{e}")

breakrun_game_stability_test(duration_hours=4)

#### 目标：测试游戏的兼容性

场景：测试游戏在不同设备和操作系统版本上的表现。

实例：在不同的设备和操作系统版本上运行测试脚本，检查游戏是否能正常运行并且没有明显的界面问题或功能缺陷。

python

devices_and_os_versions = [

("device_1", "Android_10"),

("device_2", "Android_11"),

("device_3", "iOS_14"),

("device_4", "iOS_15"),

]

for device, os_version in devices_and_os_versions:

print(f"正在测试设备：{device}，操作系统版本：{os_version}")

## 运行测试脚本 run_tests_on_device(device)

#### 目标：测试游戏内的教程和引导

场景：测试新玩家在完成游戏教程后能否正确进入游戏主界面。

实例：

python

def test_game_tutorial(): # 开始游戏教程 start_tutorial_button = poco("start_tutorial_button")

start_tutorial_button.click()

## 完成教程任务 complete_tutorial_task("task_1")

complete_tutorial_task("task_2")

complete_tutorial_task("task_3")

## 检查是否进入游戏主界面 assert poco("game_main_ui").exists(), "完成教程后未进入游戏主界面！"

test_game_tutorial()

#### 目标：测试游戏内的社交功能

场景：测试玩家可以添加好友、发送消息和删除好友。

实例：

python

def test_social_features(): # 添加好友 add_friend("friend_username")

assert poco("friend_added").exists(), "添加好友失败！" # 发送消息 send_message("friend_username", "Hello, friend!")

assert poco("message_sent").exists(), "发送消息失败！" # 删除好友 remove_friend("friend_username")

assert not poco("friend_removed").exists(), "删除好友失败！"

test_social_features()

在使用 Airtest 进行游戏自动化测试时，可以使用 assert_exists() 和 assert_not_exists() 函数来替换原生的 Python 断言语句。以下是根据您的要求修改过的示例：

#### 示例1：验证登录功能

python

from airtest.core.api import assert_exists, assert_not_exists

def test_login(): login("valid_user", "valid_password")

assert_exists(poco("user_logged_in"), "登录失败！")

#### 示例2：验证登录功能（无效凭证）

python

def test_login_invalid_credentials(): login("invalid_user", "invalid_password")

assert_not_exists(poco("user_logged_in"), "登录应该失败，但成功了！")

#### 示例3：购买物品

python

def test_shop_purchase(): # 等待商店界面加载完成 shop_button = poco("shop_button")

shop_button.wait_for_appearance()

## 进入商店并购买物品 shop_button.click()

purchase_button = poco("purchase_button")

purchase_button.wait_for_appearance()

purchase_button.click()

## 检查购买结果 purchase_success_message = poco("purchase_success_message")

purchase_success_message.wait_for_appearance()

assert_exists(purchase_success_message, "购买失败！")

#### 示例4：更新个人资料

python

def test_profile_update(): try:

## 更新个人资料 update_profile("new_nickname", "new_email")

## 检查更新结果 assert_exists(poco("update_success_message"), "更新个人资料失败！")

except Exception as e:

print(f"测试过程中发生错误：{e}")

#### 示例5：开始战斗

python

def test_battle_start(): # 等待战斗开始按钮出现（最多等待 10 秒） battle_start_button = poco("battle_start_button")

battle_start_button.wait_for_appearance(timeout=10)

## 开始战斗 battle_start_button.click()

battle_scene = poco("battle_scene")

battle_scene.wait_for_appearance(timeout=20)

## 检查战斗是否开始 assert_exists(battle_scene, "战斗没有开始！")

#### 示例6：完成游戏教程

python

def test_game_tutorial(): # 开始游戏教程 start_tutorial_button = poco("start_tutorial_button")

start_tutorial_button.click()

## 完成教程任务 complete_tutorial_task("task_1")

complete_tutorial_task("task_2")

complete_tutorial_task("task_3")

## 检查是否进入游戏主界面 assert_exists(poco("game_main_ui"), "完成教程后未进入游戏主界面！")

test_game_tutorial()

#### 示例7：社交功能测试

python

def test_social_features(): # 添加好友 add_friend("friend_username")

assert_exists(poco("friend_added"), "添加好友失败！")

## 发送消息 send_message("friend_username", "Hello, friend!")

assert_exists(poco("message_sent"), "发送消息失败！")

## 删除好友 remove_friend("friend_username")

assert_not_exists(poco("friend_removed"), "删除好友失败！")

test_social_features()
