#Mimicking Client Joining in Test Runner
14 messages · Page 1 of 1 (latest)
So something like:
using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.SceneManagement; // Import the SceneManager namespace
[TestFixture]
public class NewTestScript
{
[UnityTest]
public IEnumerator SanityCheck()
{
// Load the Player prefab
GameObject playerPrefab = Resources.Load<GameObject>("Prefabs/Player");
Assert.IsNotNull(playerPrefab, "Failed to load Player prefab");
// Instantiate the Player
GameObject playerInstance = GameObject.Instantiate(playerPrefab);
yield return null; // Wait one frame for initialization
// do something here like
// server.start()
// client.join(server)
Entity player = playerInstance.GetComponent<Player>();
Assert.IsNotNull(player, "Failed to get Player component");
GameObject.Destroy(playerInstance);
yield return null;
}
^^ this is an actual test script i'm running
I was able to start the server and have a single client join:
[UnityTest]
public IEnumerator ServerClientConnectionTest()
{
// Load the naub2 scene
SceneManager.LoadScene("naub2");
yield return new WaitForSeconds(1f); // Wait for the scene to load
// Get the NetworkManager instance
NetworkManager networkManager = InstanceFinder.NetworkManager;
Assert.IsNotNull(networkManager, "NetworkManager not found in the scene");
// Start the server
networkManager.ServerManager.StartConnection();
yield return new WaitUntil(() => networkManager.ServerManager.Started);
Debug.Log("Server started successfully");
// Start the client
networkManager.ClientManager.StartConnection();
yield return new WaitUntil(() => networkManager.ClientManager.Started);
Debug.Log("Client connected successfully");
// Verify the connection
Assert.IsTrue(networkManager.IsServer, "Server is not running");
Assert.IsTrue(networkManager.IsClient, "Client is not connected");
// Clean up
networkManager.ServerManager.StopConnection(true);
networkManager.ClientManager.StopConnection();
yield return new WaitUntil(() => !networkManager.ServerManager.Started && !networkManager.ClientManager.Started);
Debug.Log("Server and client stopped");
}
but I'm still uncertain on how to mimic a second client joining.
So you can use multiple Network Managers and have the others act as clients, but it may be easier with builds for stress testing
Hmmm okay, I'm more so testing the logic of my code rather than stress testing. Do you have a quick code example you could show me how to make another network manager and connect? I get an error when I try and make another one
When I manually test I make two different builds and connect via that, i just try and write automated tests for all my code if possible so it becomes easier to manage the bigger it gets
Did you set both NetworkManagers' persistence modes to allow multiple?
Do I have to create multiple NetworkManager's in the scene? I was just creating it as an object in my code and adding it to my scene
That should be fine too