【Unity学习笔记】Unity TestRunner使用

发布时间:2024年01月21日

转载请注明出处:🔗https://blog.csdn.net/weixin_44013533/article/details/135733479
作者:CSDN@|Ringleader|

参考:

使用步骤

  1. 打开Unity Test Runner(菜单栏中的 Windows?General?Test Runner)

  2. 添加测试文件夹,在此文件夹下添加assembly definition (菜单栏中: Create > Assembly Definition)
    在这里插入图片描述

  3. 点击Assembly文件的Inspector窗口,添加 nunit.framework.dll、UnityEngine.TestRunner、 UnityEditor.TestRunner, 测试Input System时额外添加 Unity.InputSystem 和Unity.InputSystem.TestFramework。
    在这里插入图片描述

  4. 文件夹下添加代码,或使用NewInputSystem官方提供的测试用例 Unity-Technologies/InputSystem
    在这里插入图片描述

  5. 运行测试
    在这里插入图片描述

使用案例

放一个NewInputSystem的使用案例:

using System.Diagnostics.CodeAnalysis;
using NUnit.Framework;
using UnityEngine.InputSystem;

#pragma warning disable CS0649

// As should be obvious from the number of tests in here, the action system rivals the entire combined rest of the system
// in terms of complexity.
namespace Scenes.TestAction
{
    [SuppressMessage("ReSharper", "AccessToStaticMemberViaDerivedType")]
    public class TestAction : InputTestFixture
    {
        [Test]
        [Category("Actions")]
        public void Actions_WhenShortcutsDisabled_AllConflictingActionsTrigger()
        {
            var keyboard = InputSystem.AddDevice<Keyboard>();

            var map1 = new InputActionMap("map1");
            var action1 = map1.AddAction(name: "action1");
            action1.AddCompositeBinding("2DVector")
                .With("Up", "<Keyboard>/w")
                .With("Down", "<Keyboard>/s")
                .With("Left", "<Keyboard>/a")
                .With("Right", "<Keyboard>/d");

            var map2 = new InputActionMap("map2");
            var action2 = map2.AddAction(name: "action2");
            action2.AddCompositeBinding("2DVector")
                .With("Up", "<Keyboard>/w")
                .With("Down", "<Keyboard>/s")
                .With("Left", "<Keyboard>/a")
                .With("Right", "<Keyboard>/d");
            var action3 = map2.AddAction(name: "action3", binding: "<Keyboard>/w");

            map1.Enable();
            map2.Enable();

            Press(keyboard.wKey);

            // All Actions were triggered
            Assert.That(action1.WasPerformedThisFrame());
            Assert.That(action2.WasPerformedThisFrame());
            Assert.That(action3.WasPerformedThisFrame());
        }

    }

}

文章来源:https://blog.csdn.net/weixin_44013533/article/details/135733479
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。