将 xUnit 项目添加到 Unity 项目时,单元测试是不确定的 [英] Unit tests are inconclusive when adding xUnit project to Unity project

查看:22
本文介绍了将 xUnit 项目添加到 Unity 项目时,单元测试是不确定的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

公共类 AScript : MonoBehaviour {}

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

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

[事实]公共无效测试1(){断言.真(真);}

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

<块引用>

上次运行错误:没有那个文件或目录

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

我不想使用 Unity 测试框架,因为我使用的是

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

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

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


这是整个测试输出

<块引用>

TestProject1.UnitTest1.Test1

测试未运行

上次运行错误:没有那个文件或目录

--- 异常 #1/2 [Win32Exception] 消息 =没有这样的文件或目录"ExceptionPath = Root.InnerException ClassName =System.ComponentModel.Win32Exception HResult = E_FAIL=EFail=80004005源 = System.Diagnostics.Process NativeErrorCode = 2StackTraceString = 在System.Diagnostics.Process.ForkAndExecProcess(字符串文件名,String[] argv, String[] envp, String cwd, Boolean redirectStdin,布尔重定向标准输出,布尔重定向标准,布尔setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32&stdinFd、Int32&stdoutFd、Int32&stderrFd,布尔使用终端,布尔 throwOnNoExec)在 System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)在 System.Diagnostics.Process.Start()在 JetBrains.ReSharper.UnitTestFramework.Processes.PreparedProcess.Start()在 JetBrains.ReSharper.UnitTestFramework.TestRunner.TestRunnerRunStrategy.<>c.b__19_1(IPreparedProcessp)在 JetBrains.Util.Special.GeneralUtil.With[T](T control, Action1 action) at JetBrains.ReSharper.UnitTestFramework.TestRunner.TestRunnerRunStrategy.StartTestRunner(IUnitTestRun run, ITestRunnerEnvironmentSpecifier environmentSpecifier, IUnitTestAgentCommunicationChannel channel) at JetBrains.ReSharper.UnitTestFramework.TestRunner.TestRunnerRunStrategy.Run(IUnitTestRun run) --- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在 JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.StartCore(CancellationToken onCancel, CancellationToken onAbort) 在 JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.<>c.<StartCore>b__29_0(ExceptionDispatchInfo e) at JetBrains.Util.Special.GeneralUtil.WithNotNull[T](T item, Action1 F)在 JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.StartCore(CancellationTokenonCancel, CancellationToken onAbort)在 JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.Start(CancellationTokenonCancel, CancellationToken onAbort) "

--- 外---

--- 异常 #2/2 [LoggerException] 消息 =没有这样的文件或目录"ExceptionPath = Root ClassName =JetBrains.Util.LoggerException InnerException = 异常 #1 在Root.InnerException" HResult = COR_E_APPLICATION=80131600StackTraceString = 在JetBrains.Util.ILoggerEx.LogException(ILogger this, LoggingLevel级别、异常异常、ExceptionOrigin 异常源、字符串评论)在 JetBrains.Util.ILoggerEx.Error(ILogger this, Exception exception, ExceptionOrigin origin, String comment)在 JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.Start(CancellationTokenonCancel, CancellationToken onAbort)在 System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine&状态机)在 JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.Start(CancellationTokenonCancel, CancellationToken onAbort)在 JetBrains.ReSharper.UnitTestFramework.Launch.Stages.RunTestsStage.<>c__DisplayClass5_0.b__0()在 System.Threading.Tasks.Task`1.InnerInvoke()在 System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread线程池线程、执行上下文执行上下文、上下文回调回调,对象状态)在 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- 测试

I create a new Unity project and make sure the Assembly-CSharp.csproj file exists by creating a script in the Assets folder. I open that script and edit it to

public class AScript : MonoBehaviour {}

I close the IDE and Unity after that. The Assembly-CSharp.csproj file exists now in the root directory of the project.

I start my IDE again (without Unity) and open the project from the .sln file. I add a new xUnit project to the solution and modify the first test to

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

Just for the sake of completeness I reference the Unity project in my test project. When running the test (which should be fine) the tests didn't run because they are inconclusive. I found this information in the test output

Last runner error: No such file or directory

Below I will post the whole test output. Does someone know how to add a unit test project to a Unity project? Please let me know if you need more information.

I don't want to use the Unity test framework because I'm using Entitas and don't want to rely on the engine. I just want to test my pure C# code outside.

I'm also curious why Rider comes up with this when adding the test project to the solution

I got it working but I don't like the solution, so please explain if this is the only way or if this is wrong (as I think)

When creating a completely new solution with only that test project the unit tests will run correctly. Now I can add the existing Unity project via "Add existing project". Referencing the Unity project works fine too.

So yeah this works but now I have two solutions in my repository? Why do I have to separate the test project? Why can't I just add the unit test project to Unity's solution file?


This is the whole test output

TestProject1.UnitTest1.Test1

Test not run

Last runner error: No such file or directory

--- EXCEPTION #1/2 [Win32Exception] Message = "No such file or directory" 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.UnitTestFramework.Processes.PreparedProcess.Start() at JetBrains.ReSharper.UnitTestFramework.TestRunner.TestRunnerRunStrategy.<>c.b__19_1(IPreparedProcess p) at JetBrains.Util.Special.GeneralUtil.With[T](T control, Action1 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, Action1 F) at JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.StartCore(CancellationToken onCancel, CancellationToken onAbort) at JetBrains.ReSharper.UnitTestFramework.Launch.UnitTestRun.Start(CancellationToken onCancel, CancellationToken onAbort) "

--- Outer ---

--- EXCEPTION #2/2 [LoggerException] Message = "No such file or directory" ExceptionPath = Root ClassName = JetBrains.Util.LoggerException InnerException = "Exception #1 at Root.InnerException" HResult = COR_E_APPLICATION=80131600 StackTraceString = " at JetBrains.Util.ILoggerEx.LogException(ILogger this, LoggingLevel level, Exception exception, ExceptionOrigin exceptionOrigin, String comment) at JetBrains.Util.ILoggerEx.Error(ILogger this, Exception exception, ExceptionOrigin origin, String comment) at JetBrains.ReSharper.UnitTestFramework.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, CancellationToken onAbort) at JetBrains.ReSharper.UnitTestFramework.Launch.Stages.RunTestsStage.<>c__DisplayClass5_0.b__0() at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) at System.Threading.Tasks.Task.ExecuteFromThreadPool(Thread threadPoolThread) at System.Threading.ThreadPoolWorkQueue.Dispatch() "

解决方案

Unfortunately you can't manipulate the csproj or sln files because Unity will throw them away after next compilation.

Just create new solution with a test project right next to the Unity project. Add the Unity project as an existing project to your test solution.

Since you mentioned you are using Entitas you can follow this guide to setup your tests

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

这篇关于将 xUnit 项目添加到 Unity 项目时,单元测试是不确定的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆