If you add a UnitTests
directory to your project folder, we will automatically generate a Unit Test project for you.
In order for the project to be generated you need to restart your editor if you have it open.
Add a new File to the unit test project call it MyFirstTest.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class MyFirstTest
{
[TestMethod]
public void Simple()
{
Assert.AreEqual( 4, 2 + 2 );
}
}
You can run the tests using the dotnet test
command via CLI. If your using Visual Studio you can use the Test Explorer to run your tests.
If your test relies on engine functionality, for example if you want to test a component. You can initialize the engine using the following code:
global using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class TestInit
{
[AssemblyInitialize]
public static void ClassInitialize( TestContext context )
{
Sandbox.Application.InitUnitTest();
}
[AssemblyCleanup]
public static void AssemblyCleanup()
{
Sandbox.Application.ShutdownUnitTest();
}
}
Just drop it into any file in the unit test project e.g. InitTests.cs
using Sandbox;
[TestClass]
public class Camera
{
[TestMethod]
public void MainCamera()
{
var scene = new Scene();
using var sceneScope = scene.Push();
Assert.IsNull( scene.Camera );
var go = scene.CreateObject();
var cam = go.Components.Create<CameraComponent>();
Assert.IsNotNull( scene.Camera );
go.Destroy();
scene.ProcessDeletes();
Assert.IsNull( scene.Camera );
}
}