Home > Enterprise >  Unity and MSTest
Unity and MSTest

Time:12-25

I am attempting to copy a group of C# classes into a Unity project, along with some associated unit tests written with the MSTest Framework that rely on a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework. My understanding is that this framework is built into Visual Studio, but Unity projects don't seem to recognize references to it.

Following Unity's documentation enter image description here

Does anyone know if an MSTest project can be used in conjunction with Unity? Or will I need to rewrite my test classes using one of the listed frameworks such as NUnit?

CodePudding user response:

There are two kinds of tests in Unity:

  1. Basic unit tests (marked with [Test] when using NUnit) which test code synchronously in a single frame, and are generally best for testing single classes.

  2. Unity tests (marked with [UnityTest]) which run as full coroutines and are able to run tests over multiple frames. That allows them to load scenes, observe GameObject behavior, run the physics engine, etc.

If your tests are intended to be Unity tests, you will have to port to Unity's test framework which is NUnit-based (or write your own framework). If they're intended to be basic unit tests, using NUnit will allow you to load and run them using the Test Runner panel in Unity. If you choose not to use NUnit, Unity may not be able to run them from the Test Runner panel, but it should still be possible in principle to run them externally using other tools, by leveraging Unity's Visual Studio project that it generates when you double click a script in the Library. (It may not add the required MSTest references automatically to the generated project, in which case you may want to automate some tweaks to the project to set it up the way you want.)

  • Related