【问题标题】:Trying to auto-run UnitTests within Unity尝试在 Unity 中自动运行 UnitTest
【发布时间】:2018-04-11 07:06:49
【问题描述】:

我正在尝试做这样的事情

class CustomPreProcessBuild : IPreprocessBuild
{
    public void OnPreprocessBuild(BuildTarget _target, string _builtPath)
    {
        Debug.Log("CustomPreProcessBuild-OnPreprocessBuild for target " + _target + " at path: " + _builtPath);

        // TODO: run tests
        Debug.Log("Reminder! Run unit tests before building the project");
    }
}

正如我的代码示例所述,我想在执行 Build 命令之前自动运行所有单元测试。 有没有办法从 Unity 中的 C# 代码自动运行所有单元测试?

我使用的是 2017.3

【问题讨论】:

    标签: c# unit-testing unity3d nunit


    【解决方案1】:

    据我所知,您目前还不能从脚本调用测试。尽管我对您正在运行的版本并不太熟悉。但是,不久前我确实看到了一篇关于 Batch 类的反思的帖子,目的是调用。不过,现在似乎无法为我的生活找到它。

    您可以从命令行运行它们,所以我想您可以尝试通过来自System.Diagnosticsnamespace 的进程运行它。不确定 Unity 自身运行的效果如何,因为测试运行程序使用 Unity exe,但仍然值得一试!

    【讨论】:

    • 给定的批处理解决方案(在统一文档上)适用于 windows,而我的团队成员也在 windows 和 mac 上。所以我想在构建之前从 C# 脚本自动运行所有测试
    • 还有那个批处理解决方案不是自动运行的,可以在c#脚本中调用
    • 然后,我知道的唯一其他选择是查看是否可以通过进程从命令行运行它们。它可能/可能不起作用,但正如我所说,值得一看,因为它可以为您解决问题。祝你好运!
    • 在这种情况下,它不会是自动运行
    【解决方案2】:

    您需要使用 Unity 测试运行程序。这是文档: https://docs.unity3d.com/Manual/testing-editortestsrunner.html

    如果您不使用此测试运行程序,您将无法测试 MonoBehavior 衍生产品(您的自定义组件)。

    【讨论】:

    • 我正在使用它,但我想从 C# 脚本自动运行它
    • 你不能,因为你不能在 Unity 之外实例化 Monobehaviour。如果您只想测试不是从 Monobehaviour 派生的 C# 类,则可以使用 NUnit test runner 之类的东西运行测试
    • 是的,我想测试不使用 MonoBehaviour 的 UnitTest。你能根据我给出的示例代码描述如何做到这一点
    • 我从来没有这样做过,但显然这个 IDE 有一些支持:jetbrains.com/rider
    【解决方案3】:

    这是我创建的脚本,或多或少可以满足您的需求。它可以通过编辑器调用(通过测试菜单项)或通过命令行调用。我在这里只创建了运行编辑模式单元测试的脚本,因为您需要在整个项目中设置程序集定义要求以另外运行播放模式测试。如果你想同时运行它们,你只需要通过 TestRunnerApi 运行它们并将结果拼接在一起。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    using System;
    using UnityEditor;
    using UnityEditor.TestTools.TestRunner.Api;
    
    namespace UnitTesting
    {
        public static class UnitTestingProcessor
        {
            /************************************************************/
            #region Properties
    
            static TestRunnerApi _api;
            static TestRunnerApi Api
            {
                get
                {
                    if (_api == null) _api = ScriptableObject.CreateInstance<TestRunnerApi>();
                    return _api;
                }
            }
    
            static TestCallbacks _callbacks;
            static TestCallbacks Callbacks
            {
                get
                {
                    if (_callbacks == null) _callbacks = new TestCallbacks();
                    return _callbacks;
                }
            }
    
            #endregion
            /************************************************************/
            #region Functions
    
            #region Processor Functions
    
            [MenuItem("Tests/Run Edit Mode Unit Tests", false, 10)]
            public static void StartUnitTestingProcessor()
            {
                RunEditModeTests();
            }
    
            private static void Exit(int exitCode)
            {
                if (Application.isBatchMode) EditorApplication.Exit(exitCode);
            }
    
            #endregion
            
            #region Test Functions
    
            private static void RunEditModeTests()
            {   
                Api.RegisterCallbacks(Callbacks);
    
                Filter filter = new Filter();
                filter.testMode = TestMode.EditMode;
                ExecutionSettings executionSettings = new ExecutionSettings(filter);
    
                Api.Execute(executionSettings);
            }
    
            private static void FinishEditModeTests(ITestResultAdaptor result)
            {   
                Api.UnregisterCallbacks(Callbacks);
    
                if (result.ResultState == "Passed") 
                {
                    Debug.Log("Edit Mode Unit Tests Succeeded");
                    Exit(0);
                }
                else
                {   
                    Debug.LogError("Edit Mode Unit Tests Failed");
                    Exit(1);
                }
            }
    
            #endregion
            
            #endregion
            /************************************************************/
            #region Subclasses
    
            private class TestCallbacks : ICallbacks
            {
                public void RunStarted(ITestAdaptor testsToRun) 
                {
                    Debug.Log($"EditMode Run Started");
                }
    
                public void RunFinished(ITestResultAdaptor result) 
                {
                    Debug.Log($"EditMode Run Finished");
                    FinishEditModeTests(result);
                }
    
                public void TestStarted(ITestAdaptor test) 
                {
                    // Debug.Log($"Test {test.Name} Started");
                }
    
                public void TestFinished(ITestResultAdaptor result)
                {
                    if (result.HasChildren) return; 
                    
                    if (result.ResultState == "Passed") return;
                    
                    Debug.LogError($"{result.ResultState}: {result.Test.Name}");
                }
            }
    
            #endregion
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 2014-06-16
      • 2019-03-06
      • 2014-02-01
      • 2016-05-04
      • 2014-06-06
      相关资源
      最近更新 更多