Part 5: Adding Unit tests

I tutorial is completly optional and not needed for our engine but I thought it would be worth while to add some Unit Tests.

I am using mstest that comes built into visual studio 2010, you could also use nunit if you wished or any unit test framework that works with .net.

Am normally very lazy with it comes to unit tests at work I never write them unless I am made to which is really bad of me I know this and every other programmer know it as well that we should be using unit test to make our coding better but its hard to fit them in when your working to a deadline.

So I thought  I would include some in this.

Lets Start by creating a new unit test project and calling it EngineTests

Lest remane the Class UnitTest1.cs to EngineTest.cs and add the following code.

using Engine;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.Xna.Framework;
using Engine.Components;
using Microsoft.Xna.Framework.Graphics;

namespace EngineTests
{

    /// <summary>
    ///This is a test class for EngineTest and is intended
    ///to contain all EngineTest Unit Tests
    ///</summary>
    [TestClass()]
    public class EngineTest : Game
    {

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        Game game = new Game();
        GraphicsDeviceManager graphics;
        GraphicsDevice device;

        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void EngineSetup()
        {
            graphics = new GraphicsDeviceManager(game);
            graphics.PreferredBackBufferWidth = 500;
            graphics.PreferredBackBufferHeight = 500;
            graphics.IsFullScreen = false;
            graphics.ApplyChanges();
            device = graphics.GraphicsDevice;
            device.Clear(Color.CornflowerBlue);
            Engine.Engine.SetupEngine(graphics);
        }
    }
}

 

So this will set up a new engine that the rest of our test will use to run.

Next we will add some tests for the GameScreen class.

using Engine.GameScreens;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Engine.Components;

namespace EngineTests
{

    /// <summary>
    ///This is a test class for GameScreenTest and is intended
    ///to contain all GameScreenTest Unit Tests
    ///</summary>
    [TestClass()]
    public class GameScreenTest
    {

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        /// <summary>
        ///A test for GameScreen Constructor
        ///</summary>
        [TestMethod()]
        public void GameScreenConstructorTest()
        {
            string Name = "TESTSCREEN";
            GameScreen target = new GameScreen(Name);
            Assert.IsNotNull(target);
        }

        /// <summary>
        ///A test for Disable
        ///</summary>
        [TestMethod()]
        public void DisableTest()
        {
            string Name = "TESTSCREEN1";
            GameScreen target = new GameScreen(Name);
            target.Disable();
            Assert.IsNotNull(target);
        }

        /// <summary>
        ///A test for Initialize
        ///</summary>
        [TestMethod()]
        public void InitializeTest()
        {
            string Name = "TESTSCREEN3";
            GameScreen target = new GameScreen(Name);
            target.Initialize();
            Assert.AreEqual(target.Initialized, true);
        }

        /// <summary>
        ///A test for ToString
        ///</summary>
        [TestMethod()]
        public void ToStringTest()
        {
            string Name = "TESTSCREEN4";
            GameScreen target = new GameScreen(Name);
            string expected = "TESTSCREEN4";
            string actual;
            actual = target.ToString();
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for Initialized
        ///</summary>
        [TestMethod()]
        public void InitializedTest()
        {
            string Name = "TESTSCREEN6";
            GameScreen target = new GameScreen(Name);
            bool expected = true;
            bool actual;
            target.Initialized = expected;
            actual = target.Initialized;
            Assert.AreEqual(expected, actual);
        }
    }
}

Next some test for the GameSceenCollection

using Engine.GameScreens;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace EngineTests
{

    /// <summary>
    ///This is a test class for GameScreenCollectionTest and is intended
    ///to contain all GameScreenCollectionTest Unit Tests
    ///</summary>
    [TestClass()]
    public class GameScreenCollectionTest
    {

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        /// <summary>
        ///A test for GameScreenCollection Constructor
        ///</summary>
        [TestMethod()]
        public void GameScreenCollectionConstructorTest()
        {
            GameScreenCollection target = new GameScreenCollection();
            Assert.IsNotNull(target);
        }

        /// <summary>
        ///A test for GetKeyForItem
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Engine.dll")]
        public void GetKeyForItemTest()
        {
            GameScreenCollection_Accessor target = new GameScreenCollection_Accessor();
            GameScreen item = new GameScreen("TESTSCREEN7");
            string expected = "TESTSCREEN7";
            string actual;
            actual = target.GetKeyForItem(item);
            Assert.AreEqual(expected, actual);
        }
    }
}

And finally our Component class tests

using Engine.Components;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Engine.GameScreens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace EngineTests
{
/// <summary>
///This is a test class for ComponentTest and is intended
///to contain all ComponentTest Unit Tests
///</summary>
[TestClass()]
public class ComponentTest
{

private TestContext testContextInstance;

/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}

Game game = new Game();
GraphicsDeviceManager graphics;
GraphicsDevice device;

//Use TestInitialize to run code before running each test
[TestInitialize()]
public void MyTestInitialize()
{
graphics = new GraphicsDeviceManager(game);
graphics.PreferredBackBufferWidth = 500;
graphics.PreferredBackBufferHeight = 500;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
device = graphics.GraphicsDevice;
device.Clear(Color.CornflowerBlue);
Engine.Engine.SetupEngine(graphics);
}

/// <summary>
///A test for InitializeComponent
///</summary>
[TestMethod()]
[DeploymentItem("Engine.dll")]
public void InitializeComponentTest()
{
Component_Accessor target = new Component_Accessor(new GameScreen("TESTSCREEN8"));
GameScreen Parent = new GameScreen("TESTSCREEN9");

Leave a comment

Switch to our mobile site