Unit Tests
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.
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.
Your First Test
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.
Game 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
{
public static Sandbox.AppSystem TestAppSystem;
[AssemblyInitialize]
public static void AssemblyInitialize( TestContext context )
{
TestAppSystem = new TestAppSystem();
TestAppSystem.Init();
}
[AssemblyCleanup]
public static void AssemblyCleanup()
{
TestAppSystem.Shutdown();
}
}
Just drop it into any file in the unit test project e.g. InitTests.cs
Example Component Test
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 );
}
}
Referenced API
Canonical API pages mentioned in this guide.
No summary available.
Every scene should have at least one Camera.
No summary available.
Utility info for tools usage.
Throws an exception when the given object is not null.
Throws an exception when the given object is not null.
Throws an exception when the 2 given objects are not equal to each other.
Create a GameObject on this scene. This doesn't require the scene to be the active scene.