繁体   English   中英

将 xUnit 项目添加到 Unity 项目时,单元测试是不确定的

[英]Unit tests are inconclusive when adding xUnit project to Unity project

我创建了一个新的 Unity 项目,并通过在 Assets 文件夹中创建一个脚本来确保Assembly-CSharp.csproj文件存在。 我打开该脚本并将其编辑为

public class AScript : MonoBehaviour {}

之后我关闭了 IDE 和 Unity。 Assembly-CSharp.csproj文件现在位于项目的根目录中。

我再次启动我的 IDE(没有 Unity)并从 .sln 文件打开项目。 我在解决方案中添加了一个新的 xUnit 项目并将第一个测试修改为

[Fact]
public void Test1()
{
    Assert.True(true);
}

为了完整起见,我在我的测试项目中引用了 Unity 项目。 在运行测试时(应该没问题),测试没有运行,因为它们是不确定的。 我在测试输出中找到了这个信息

最后一个运行错误:没有这样的文件或目录

下面我将发布整个测试输出。 有人知道如何将单元测试项目添加到 Unity 项目中吗? 如果您需要更多信息,请告诉我。

我不想使用 Unity 测试框架,因为我使用的是Entitas并且不想依赖引擎。 我只想在外面测试我的纯 C# 代码。

我也很好奇为什么 Rider 在将测试项目添加到解决方案时会想到这个

在此处输入图片说明

我让它工作了,但我不喜欢这个解决方案,所以请解释这是唯一的方法还是错误的(我认为)

当仅使用该测试项目创建全新的解决方案时,单元测试将正确运行。 现在我可以通过“添加现有项目”添加现有的 Unity 项目。 引用 Unity 项目也可以正常工作。

所以是的,这有效,但现在我的存储库中有两个解决方案? 为什么我要分开测试项目? 为什么我不能将单元测试项目添加到 Unity 的解决方案文件中?


这是整个测试输出

TestProject1.UnitTest1.Test1

测试未运行

最后一个运行错误:没有这样的文件或目录

--- 异常 #1/2 [Win32Exception] 消息 =“没有这样的文件或目录”ExceptionPath = Root.InnerException ClassName = System.ComponentModel.Win32Exception HResult = E_FAIL=EFail=80004005 Source = System.Diagnostics.Process NativeErrorCode = 2 StackTraceString =“ at System.Diagnostics.Process.ForkAndExecProcess(String filename, String[] argv, String[] envp, String cwd, Boolean redirectStdin, Boolean redirectStdout, Boolean redirectStderr, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec) at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start() at JetBrains.ReSharper.UnitTestParFramework.Processed() ) 在 JetBrains.ReSharper.UnitTestFramework.TestRunner.TestRunnerRunStrategy.<>c.b__19_1(IPreparedProcess p) 在 JetBrains.Util.Special.GeneralUtil.With[T](T control, Action 1 action) at JetBrains.ReSharper.UnitTestFramework.TestRunner.TestRunnerRunStrategy.StartTestRunner(IUnitTestRun run, ITestRunnerEnvironmentSpecifier environmentSpecifier, IUnitTestAgentCommunicationChannel channel) at JetBrains.ReSharper.UnitTestFramework.TestRunner.TestRunnerRunStrategy.Run(IUnitTestRun run) --- End of stack trace from previous location where exception was thrown --- at JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.StartCore(CancellationToken onCancel, CancellationToken onAbort) at JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.<>c.<StartCore>b__29_0(ExceptionDispatchInfo e) at JetBrains.Util.Special.GeneralUtil.WithNotNull[T](T item, Action 1 action) at JetBrains.ReSharper.UnitTestFramework.TestRunner.TestRunnerRunStrategy.StartTestRunner(IUnitTestRun run, ITestRunnerEnvironmentSpecifier environmentSpecifier, IUnitTestAgentCommunicationChannel channel) at JetBrains.ReSharper.UnitTestFramework.TestRunner.TestRunnerRunStrategy.Run(IUnitTestRun run) --- End of stack trace from previous location where exception was thrown --- at JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.StartCore(CancellationToken onCancel, CancellationToken onAbort) at JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.<>c.<StartCore>b__29_0(ExceptionDispatchInfo e) at JetBrains.Util.Special.GeneralUtil.WithNotNull[T](T item, Action 1 F) at JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.StartCore(CancellationToken onCancel, CancellationToken onAbort) at JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.Start(CancellationToken onCancel, CancellationToken onAbort) )”

--- 外 ---

--- 异常 #2/2 [LoggerException] 消息 = “没有这样的文件或目录” ExceptionPath = Root ClassName = JetBrains.Util.LoggerException InnerException = “Exception #1 at Root.InnerException” HResult = COR_E_APPLICATION=80131600 StackTraceString = “ at JetBrains.Util.ILoggerEx.Error(ILogger this, Exception exception, ExceptionOrigin origin, String comment) at JetBrains.ReSharper.UnitTestFramework 的 JetBrains.Util.ILoggerEx.LogException(ILogger this, LoggingLevel level, Exception exception, ExceptionOrigin exceptionOrigin, String comment) .Launch.UnitTestRun.Start(CancellationToken onCancel, CancellationToken onAbort) at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) at JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.Start(CancellationToken onCancel) JetBrains.ReSharper.UnitTestFramework.Launch.Stages.RunTestsStage.<>c__DisplayClass5_0.b__0() 在 System.Threading.Tasks.Task`1.InnerInvoke() 在 System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) 在 System.Threading.Tasks.Task.ExecuteFromThreadPool(Thread) threadPoolThread) 在 System.Threading.ThreadPoolWorkQueue.Dispatch() ”

不幸的是,您无法操作 csproj 或 sln 文件,因为 Unity 会在下次编译后将它们丢弃。

只需在 Unity 项目旁边创建带有测试项目的新解决方案。 将 Unity 项目作为现有项目添加到您的测试解决方案中。

既然您提到您正在使用 Entitas,您可以按照本指南来设置您的测试

https://github.com/sschmid/Entitas-CSharp/wiki/How-to-setup-a-new-Github-project-with-Unity-and-tests#setting-up-tests

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM