Version: 6000.3
语言: 中文
5. 碰撞测试
7. 场景验证测试

6. 资产变更测试

学习目标

本练习将教你一种游戏测试中的流行模式,以验证资产是否随时间变化。

锻炼

正如您在 LostCrypt 中注意到的那样,当您拿起魔杖时,您的角色会装备盔甲。
编写一个测试来检查萨拉拿起魔杖后是否装备了盔甲。

  1. 创建一个WandTests.cs类和实现MainScene\_CharacterReachesWandAndEquipsArmor测试。
  2. 尝试观察如何Sara Variant或者更具体地说puppet_sara 游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
    请参阅术语表
    在你拿起魔杖的那一刻发生变化。

提示

  • 你可以重复使用“伸手魔杖测试”中的代码来处理角色拿起魔杖的逻辑。或者,您可以尝试以编程方式触发此作。
  • 请记住,如果您的测试无法访问某些 Unity 内部 API,您可能需要在PlayModeTests装配定义。

溶液

PlayModeTests.asmdef

{
    "name": "PlayModeTests",
    "rootNamespace": "",
    "references": [
        "Unity.InputSystem",
        "Unity.InputSystem.TestFramework",
        "TestInputControl",
        "UnityEngine.TestRunner",
        "Unity.2D.Animation.Runtime"
    ],
    "includePlatforms": [],
    "excludePlatforms": [],
    "allowUnsafeCode": false,
    "overrideReferences": true,
    "precompiledReferences": [
        "nunit.framework.dll"
    ],
    "autoReferenced": false,
    "defineConstraints": [
        "UNITY_INCLUDE_TESTS"
    ],
    "versionDefines": [],
    "noEngineReferences": false
}

WandTests.cs

using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.SceneManagement;
using UnityEngine.Experimental.U2D.Animation;

public class WandTests
{
    private Transform _characterTransform;
    private float _testTimeout = 25.0f;
    private float _wandLocation = 21.080f;

    [UnityTest]
    public IEnumerator MainScene_CharacterReachesWandAndEquipsArmor()
    {
        SceneManager.LoadScene("Assets/Scenes/Main.unity", LoadSceneMode.Single);
        
        // Skip first frame so Sara have a chance to appear on the screen
        yield return null;
        var puppet = GameObject.Find("puppet_sara");
        var spriteLibrary = puppet.GetComponent<SpriteLibrary>();
        
        Assert.AreEqual(spriteLibrary.spriteLibraryAsset.name, "Sara");

        var elapsedTime = 0.0f;
        yield return GoRight();
        while (GetCurrentCharacterPosition() <= _wandLocation)
        {
            yield return null;
            elapsedTime += Time.deltaTime;
            if (elapsedTime > _testTimeout)
            {
                Assert.Fail($"Character did not reach location position in {_testTimeout} seconds.");
            }
        }

        // Wait for Wand pickup animation to be over.
        yield return new WaitForSeconds(12);

        Assert.AreEqual(spriteLibrary.spriteLibraryAsset.name, "Sara_var01");
    }

    private float GetCurrentCharacterPosition()
    {
        // Get Main character's Transform which is used to manipulate position.
        if (_characterTransform == null)
        {
            _characterTransform = GameObject.Find("Sara Variant").transform;
        }

        return _characterTransform.position.x;
    }

    private IEnumerator GoRight()
    {
        TestInputControl.MoveLeft = false;
        yield return null;
        TestInputControl.MoveRight = true;
    } 
}
5. 碰撞测试
7. 场景验证测试