s&box
RustCounter-Strike 2s&boxDeadlock
Wiki
Get s&box on Steam
Browse
OverviewGetting StartedCreating GamesEngine & ToolsDocumentationCommunity GamesResources
s&boxDocumentationCodeAdvanced TopicsUnit Tests
SOURCE 2 · C#
Build and play games with s&box.

Facepunch's game engine and creation platform — make games in C#, or jump straight into community games.

Get s&box on Steam

s&box Wiki

  • Overview
  • Getting Started
  • Creating Games
  • Engine & Tools
  • Documentation
  • Community Games
  • Resources

Tools & Community

  • Getting Started
  • Creating Games
  • Engine & Tools
  • Resources

Platforms

  • RustBattle
  • CSBattle
  • Rust Game Wiki
  • CS2 Skins Wiki
  • Deadlock Wiki

About

  • Official Site
  • Developer Wiki
  • GitHub
  • Get on Steam
NOT AFFILIATED WITH FACEPUNCH STUDIOS · © 2026 s&box Wiki (community)
Docs/Code
Code

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.

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 you're 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 );
	}
}

Source: Facepunch/sbox-docs (CC-BY-4.0) · updated 2026-03-04. Read it rendered on the official docs.

More in Code

Advanced Topics
Api Whitelist
Cheat Sheet
Code
Code Basics
Code Generation
Console Variables
Hotloading
← All documentation

Community wiki — not affiliated with Facepunch Studios. For the latest and most authoritative information, see the official developer wiki.