Sign in to follow this  
Cycles

Unit Testing - how would I test the game loop?

Recommended Posts

Hi, I'm slowly starting to get to grips with unit testing more now, and some things that I don't think I could have tested before I now have tests for. However, something that i'm not sure how to test is my main game loop. My game works by adding tasks such as rendering/input/etc to the engine. Then, when I run the game loop it should run each of these tasks. I want to have a unit test like so:
        [Test]
        public void TestRunningEngineDelegatesResponsibilityToSubtasks()
        {
            MockRenderingDevice mockRenderer = new MockRenderingDevice();
            InputHandler mockInputDevice = new InputHandler();

            MockTask mockTask = new MockTask();
            engine.Initialize(mockRenderer, mockInputDevice, "Test");
            engine.AddTask(mockTask);

            engine.Run(); // (1)

            Assert.That(mockTask.HasRun);
        }

But, at (1) in the source code, this would begin a main game loop and the only way the loop can terminate is when the window is closed. I considered letting the mockTask call Engine.Terminate(); in some way - but this would mean letting tasks be aware of the Engine they are running in, which has not been necessessary anywhere (except to make the unit test work...) Any ideas?

Share this post


Link to post
Share on other sites
I would separate the 'end when window closes' functionallity from the 'executes task' functionality. That is, 1- test that the engine can run tasks and be shut down by triggering a certain event, 2- check that closing the window can be used to trigger an event, and 3- plug the event generated by the window closing into the event which causes the engine to end its loop.

Share this post


Link to post
Share on other sites
I think I understand what you're saying... Would like this mean changing the engines public api from:

public void Run();

To:

public void Step();
public event EventHandler<WindowClosedEventArgs> WindowClosed;
public void Shutdown();

public Run(); (Runs the event loop forever, hooks WindowClosed to Shutdown, calls Step())

Have I understood you correctly there? I can see how that will make it easier to test, but exposing the Step() function doesn't seem right - clients outside of the Engine class shouldn't need to know about this function - or be able to call it.

Share this post


Link to post
Share on other sites
There's no reason to expose the step function. What is important is that you allow the user of the engine to specify when the engine stops running by means of an event. This would allow the engine to be used even without a window, and would give you more control over the engine during unit tests.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this