menu_bookDocumentation
Unit Tests: MSTest with Engine Initialization for Component Testing
s&box supports unit testing via a UnitTests directory in your project. The engine auto-generates a test project when this directory exists (restart editor after creating it).
Basic Test
CSHARP
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class MyFirstTest
{
[TestMethod]
public void Simple()
{
Assert.AreEqual( 4, 2 + 2 );
}
}Run with dotnet test CLI or Visual Studio Test Explorer.
Engine-Dependent Tests (Game Tests)
Initialize the s&box engine for tests that need Components, Scenes, or other engine functionality:
CSHARP
[TestClass]
public class TestInit
{
public static Sandbox.AppSystem TestAppSystem;
[AssemblyInitialize]
public static void AssemblyInitialize( TestContext context )
{
TestAppSystem = new TestAppSystem();
TestAppSystem.Init();
}
[AssemblyCleanup]
public static void AssemblyCleanup()
{
TestAppSystem.Shutdown();
}
}Component Test Example
CSHARP
[TestClass]
public class CameraTests
{
[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 );
}
}Key Points
- Uses MSTest (Microsoft.VisualStudio.TestTools.UnitTesting)
- TestAppSystem bootstraps the engine for scene/component access
- scene.Push() sets the active scene scope (dispose to clean up)
- scene.ProcessDeletes() flushes pending destroy operations synchronously
Was this helpful?