---
title: "游戏自动化测试的参数化覆盖方法"
author: "Perrin Yong"
author_profile: https://www.pystone.net/profile/
published_by: "Perrin Yong"
canonical: https://www.pystone.net/notes/game-automation-parameterized-coverage/
type: note
content_role: unspecified
visibility: public
id_stability: rename-stable
source_path: "10-计算机、信息技术与工程/03-软件工程与质量保障/CI-CD与质量保障/AI辅助质量保障/游戏自动化测试的参数化覆盖方法.md"
content_hash: 56a2f1d3454e3d60b847e6b818c9978202eec31bdd3a346c91accec22e110798
knowledge_version: 224c990773de.5fa8af6e39fa
site_commit: 224c990773de166d23a886306577dd90379529ce
notes_commit: 5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a
---
# 游戏自动化测试的参数化覆盖方法

> 创建时间：2023/6/11 18:50

以下是一个使用Python编写的简单自动化测试脚本示例，该脚本使用pytest测试框架。这个例子展示了如何在脚本中设置不同的输入参数和测试条件，以测试一个虚构的游戏中的战斗功能。这个测试场景涉及到多个因素，包括角色等级、技能、敌人类型和数量。通过自动化测试，我们可以高效地覆盖这些组合，完成人工测试难以承担的任务。

python

import pytest

from game import Game, Character, Skill, Enemy

skill_data = [

Skill("Fireball", damage=50, cost=10),

Skill("Ice Shard", damage=40, cost=8),

Skill("Heal", heal=30, cost=5),

]

enemy_data = [

(Enemy("Goblin", health=30, damage=10), 1),

(Enemy("Orc", health=50, damage=15), 2),

(Enemy("Dragon", health=200, damage=30), 1),

]

## 使用pytest.mark.parametrize装饰器设置输入参数和测试条件@pytest.mark.parametrize("level", level_data)@pytest.mark.parametrize("skill", skill_data)@pytest.mark.parametrize("enemy, count", enemy_data)def test_battle(level, skill, enemy, count): game = Game()

character = Character(level=level)

game.add_character(character)

for _ in range(count):

game.add_enemy(enemy)

## 模拟角色使用技能攻击敌人 for _ in range(5): # 假设角色使用技能5次 character.use_skill(skill, enemy)

## 验证敌人是否被击败，或者角色能否在五次技能使用后仍然存活 assert game.is_enemy_defeated() or character.is_alive()

## 运行pytest测试# pytest test_game.py

这个示例展示了如何通过自动化测试脚本设置不同的输入参数和测试条件。在这个场景中，我们需要测试5个不同等级的角色、3种技能和3种敌人组合。总共有45种组合（5 * 3 * 3），如果手动执行这些测试，需要的时间和精力将非常巨大。而使用自动化测试脚本，我们可以轻松地覆盖这些组合，发现潜在的问题，并确保游戏功能在各种情况下都能正常运行。

以下是一个使用Airtest和Poco进行手机游戏自动化测试的例子，演示了如何在脚本中设置不同的输入参数和测试条件。

角色穿戴不同装备组合，并使用不同的技能组合进行战斗

python

from airtest.core.api import *

from poco.drivers.unity3d import UnityPoco

import itertools

## 连接设备并启动游戏init_device('Android')

start_app('com.example.game')

## 初始化Poco对象poco = UnityPoco()

## 等待游戏主界面出现poco('MainScreen').wait_for_appearance()

## 穿戴不同的装备组合equipment_combinations = [

['HelmetA', 'ArmorA', 'BootsA'],

['HelmetB', 'ArmorB', 'BootsB'],

['HelmetC', 'ArmorC', 'BootsC'],

]

## 使用不同的技能组合skill_combinations = [

['SkillA', 'SkillB'],

['SkillA', 'SkillC'],

['SkillB', 'SkillC'],

]

## 遍历所有装备组合和技能组合for eq_combination in equipment_combinations:

## 打开背包 poco('InventoryButton').click()

sleep(1)

## 穿戴当前装备组合 for equipment in eq_combination:

poco(equipment).click()

sleep(1)

## 返回游戏主界面 poco('BackButton').click()

sleep(1)

for sk_combination in skill_combinations:

## 打开技能面板 poco('SkillButton').click()

sleep(1)

## 激活当前技能组合 for skill in sk_combination:

poco(skill).click()

sleep(1)

## 返回游戏主界面 poco('BackButton').click()

sleep(1)

## 开始战斗 poco('BattleButton').click()

sleep(5) # 假设战斗耗时5秒 # 检查战斗结果 result = poco('BattleResult').get_text()

print(f'Equipment: {eq_combination}, Skills: {sk_combination}, Result: {result}')

## 返回游戏主界面 poco('BackButton').click()

sleep(1)

## 停止游戏stop_app('com.example.game')

使用不同消耗品组合进行战斗

python

from airtest.core.api import *

from poco.drivers.unity3d import UnityPoco

## 连接设备并启动游戏init_device('Android')

start_app('com.example.game')

## 初始化Poco对象poco = UnityPoco()

## 等待游戏主界面出现poco('MainScreen').wait_for_appearance()

## 定义消耗品组合consumable_combinations = [

['HealthPotion', 'ManaPotion'],

['HealthPotion', 'StaminaPotion'],

['ManaPotion', 'StaminaPotion'],

]

## 遍历所有消耗品组合for cons_combination in consumable_combinations:

## 打开背包 poco('InventoryButton').click()

sleep(1)

## 使用当前消耗品组合 for consumable in cons_combination:

poco(consumable).click()

sleep(1)

## 返回游戏主界面 poco('BackButton').click()

sleep(1)

## 开始战斗 poco('BattleButton').click()

sleep(5) # 假设战斗耗时5秒 # 检查战斗结果 result = poco('BattleResult').get_text()

print(f'Consumables: {cons_combination}, Result: {result}')

## 返回游戏主界面 poco('BackButton').click()

sleep(1)

## 停止游戏stop_app('com.example.game')

下面是一个用于测试战斗场景的示例，包括M个难度、N章节的全部关卡，以及多种装备和技能组合方案。

python

## 导入所需库import time

from itertools import product

from airtest.core.api import *

from poco.drivers.unity3d import UnityPoco

## 连接设备connect_device("Android:///")

poco = UnityPoco()

def test_combat_scenarios(difficulties, chapters, levels, equipment_combinations, skill_combinations): for difficulty, chapter, level in product(difficulties, chapters, levels):

## 1. 选择难度、章节和关卡 select_difficulty_chapter_level(difficulty, chapter, level)

for equipment_combination in equipment_combinations:

## 2. 装备道具 equip_items(equipment_combination)

for skill_combination in skill_combinations:

## 3. 选择技能 set_skills(skill_combination)

## 4. 开始战斗 start_battle()

## 5. 检查战斗结果 check_battle_result()

def select_difficulty_chapter_level(difficulty, chapter, level): # 根据难度，章节和关卡选择相应选项，这里需要根据游戏界面实现具体操作。 passdef equip_items(equipment_combination): # 根据传入的装备组合，执行装备道具的操作，这里需要根据游戏界面实现具体操作。 passdef set_skills(skill_combination): # 根据传入的技能组合，执行技能选择的操作，这里需要根据游戏界面实现具体操作。 passdef start_battle(): # 开始战斗的操作，这里需要根据游戏界面实现具体操作。 passdef check_battle_result(): # 检查战斗结果的操作，这里需要根据游戏界面实现具体操作。 pass# 定义测试参数difficulties = ['Easy', 'Medium', 'Hard']

chapters = range(1, N + 1)

levels = range(1, M + 1)

equipment_combinations = [

## 不同的装备组合，例如： ['Sword1', 'Shield1', 'Helmet1'],

['Sword2', 'Shield2', 'Helmet2'],

## ...]

skill_combinations = [

## 不同的技能组合，例如： ['Skill1', 'Skill2', 'Skill3'],

['Skill4', 'Skill5', 'Skill6'],

## ...]

## 运行测试用例test_combat_scenarios(difficulties, chapters, levels, equipment_combinations, skill_combinations)

在这个测试用例中，我们使用itertools库的product函数生成不同的难度、章节和关卡的组合。对于每一种组合，我们都尝试不同的装备和技能组合，然后开始战斗并检查战斗结果。

请注意，这个测试用例需要根据实际游戏界面和操作进行调整。实现具体的操作时，应根据游戏界面和元素进行Poco操作。在运行测试用例之前，确保已经正确设置了设备连接。不同的游戏可能需要修改参数设置，以适应游戏的实际内容。
