- Home /
Unit testing for MonoBehaviours
I'd like to add unit testing to my scripts that derive from MonoBehaviour. But I'm having trouble implementing it. I've tried NUnit and currently I'm trying out NUnitLite, but in the end it doesn't really matter what library I use. I just want some unit tests for my MonoBehaviour scripts. I have no problem testing other classes, just the MonoBehaviour derivatives refuse to cooperate.
My question: Is it even possible to unit test MonoBehaviours? If so, how? Thanks for reading about my issue.
Answer by jschipilow · Oct 20, 2011 at 08:08 PM
All right, so I figured something out. Here's what I did if anyone else has same issue.
I downloaded SharpUnit and it works, but not how I first expected. Instead of adding [Test] attributes above your class functions directly. You'll need to create a new separate class that inherits SharpUnit's TestCase, with this class you'll load your Mono game object and perform tests on it.
Answer by Zadre · Jan 31, 2016 at 12:57 AM
With Unity 5.3.1 you can do the following:
using System; using UnityEngine; using NUnit.Framework;
[TestFixture] public class TileFacadeTest { [Test] public void TestFacade() { GameObject gameObject = new GameObject(); TileFacade tileFacade = gameObject.AddComponent(); tileFacade.tileValue = 1; Assert.AreEqual(tileFacade.tileValue, 1); } }
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
You are trying to create a MonoBehaviour using the 'new' keyword. 2 Answers
Why OnMouseDrag is not being called? 2 Answers
Unity scaling 1 Answer
How to inspect non-MonoBehaviour classes in the debugger 1 Answer