#Mimicking Client Joining in Test Runner

14 messages · Page 1 of 1 (latest)

atomic hound
#

I am currently writing unit tests for my project and I want to know how I can simulate/programatically have a player join the world.

For example, the provided UI lets you select client/server, and you have one client run the server + client and the other one is the client.

#

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

atomic hound
#

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.

chrome cloak
#

So you can use multiple Network Managers and have the others act as clients, but it may be easier with builds for stress testing

atomic hound
#

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

chrome cloak
atomic hound
chrome cloak
#

That should be fine too

atomic hound
#

So how would I go about configuring it to allow multiple?

#

Nvm

#

I just found the option under persistence

#

going to tryy this