ClientSimTestBase
Tsvrc.UdonTestKit.ClientSimTestBase is an abstract base class for Play Mode test fixtures that use
ClientSim. It holds a ClientSimSession in its protected Session field
and ends it after every test, and it can run every test in a TestScene of its
own.
public class PlayerCountTests : ClientSimTestBase
{
[UnityTest]
public IEnumerator SpawnRemotePlayer_InClientSim_JoinsThePlayerList()
{
yield return Session.Start();
VRCPlayerApi remote = Session.SpawnRemotePlayer("Remote");
Assert.That(VRCPlayerApi.GetPlayerCount(), Is.EqualTo(2));
}
}
The session doesn't start on its own. Each test starts it with yield return Session.Start(), so
a test can choose localPlayerIsMaster: false, or do work before ClientSim exists.
Constructors
ClientSimTestBase()runs every test in the Unity Test Framework test scene, which the whole run shares.ClientSimTestBase(TestScene scene, TestSceneLifetime lifetime = TestSceneLifetime.PerTest)runs every test inscene. WithPerTest, the scene is loaded fresh for each test and unloaded after it. WithPerFixture, it's loaded for the first test and unloaded after the last one.
Pass the scene from your fixture's own constructor:
public class LobbyTests : ClientSimTestBase
{
public LobbyTests() : base(TestScene.Empty("Lobby"))
{
}
}
Lifecycle
The base class adds four NUnit and Unity Test Framework methods. NUnit runs a base class's setup
before the derived class's and its teardown after, and Unity Test Framework runs every
[UnityTearDown] after every [TearDown]. Put together, a test with a derived class runs in this
order:
[UnitySetUp]LoadTestSceneloads the scene, if there is one, and makes it the active scene.- Your
[UnitySetUp]and[SetUp]. - The test, which starts the session.
- Your
[TearDown], while the session is still running. [TearDown]EndClientSimSessioncallsSession.End().- Your
[UnityTearDown], after ClientSim has ended and before the scene unloads. [UnityTearDown]UnloadPerTestSceneunloads aPerTestscene, destroying everything in it.
After the fixture's last test, [OneTimeTearDown] UnloadPerFixtureScene unloads a PerFixture
scene.
Session.End() runs after every test, even when the test failed or never started the session,
because ending a session that isn't running does nothing.