arjwolf
arjwolf

Reputation: 191

Unity3d Unit Test Issue

Hi there I am currently writing a small and simple unit test in unity and I can seem to make the test fail whatever I do.

I have a simple MainMenuActions class which changes the Active states of panels it looks like the following:

public class MainMenuActions : MonoBehaviour {

        public GameObject panelMainMenu = new GameObject("Panel");

public void singlePlayerPressedFromMainMenu()
    {
        panelMainMenu.SetActive(false);
    }

}

The function singlePlayerPressedFromMainMenu() is set on a button in unity to change the active state of the panel.

In my Unit test I have the following:

using UnityEngine;
using UnityEditor;
using NUnit.Framework;

public class MainMenuTests {

    [Test]
    public void ClickingSinglePlayer_HidePanel_PanelHides()
    {

        //Arrange
        var menu = new MainMenuActions();

        //Act
        menu.singlePlayerPressedFromMainMenu();

        //Assert
        menu.panelMainMenu.activeSelf.ToString().Equals("True");
    }
}

This test passes, but its suppose to fail. When debugging I see that activeSelf is indeed being set to false but still my test passes. What am I missing?

Upvotes: 0

Views: 148

Answers (1)

Matt
Matt

Reputation: 1484

You seem to have used NUnit in the wrong way. Did you forget to use Assertion? Here's an examplary test case from https://www.codeproject.com/Articles/3781/Test-Driven-Development-in-NET:

namespace UnitTestingExamples
{
  using System;
  using NUnit.Framework;

  [TestFixture]
  public class SomeTests
  {
    [Test]
    public void TestOne()
    {
      int i = 4;
      Assertion.AssertEquals( 4, i );
    }
  }
}

Note that it is using Assertion. If one test fails, the assertion will be triggered.

It could be a simple mistake from you, but I will explain more why this is literally doing nothing in more details for other readers.

In your code, menu.panelMainMenu.activeSelf.ToString().Equals("True"); will return false. But no body cares about this return value because it is not consumed by anyone. In other words, you are just skipping the left hand side of the following line:

void MyCompilerIsHappy
{
    bool myBool = menu.panelMainMenu.activeSelf.ToString().Equals("True");
    menu.panelMainMenu.activeSelf.ToString().Equals("True");
}

What it will happen if you miss the LHS is that it will return "false" or "true" whichever it may be, but its return value is not used at all. The compiler will not complain when a return value from methods are not consumed. However, it will do complain if you do something like this:

void MyCompilerIsNotHappy
{
    true;
    false;
    5;
    "some string here";
}

The reason why compilers would not complain when a return value from a method is unused is that such methods may have business logic that may alter the state of the application, and there are cases such return values need not to be used and programmers intentionally choose not to use.

Here are some Unit Test Tutorials:

http://www.nunit.org/index.php?p=quickStart&r=2.4

https://www.codeproject.com/Articles/3781/Test-Driven-Development-in-NET

https://web.archive.org/web/20210125143830/https://www.4guysfromrolla.com/articles/011905-1.aspx

Upvotes: 3

Related Questions