Search Unity

Running unit tests on C# scripts

Discussion in 'Scripting' started by DoomDude99, Jun 11, 2019.

  1. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    What are some ways of testing C# scripts without instantiating the game? I have a set of utility scripts that are part of a library that are not necessarily related to unity.
     
  2. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    531
    The simplest way would be to set up a test call to your scripts and invoke them from the inspector context menu, something like this:

    Code (CSharp):
    1. [ContextMenu("TestMyMethod")]
    2. void TestMyMethod()
    3. {
    4.     // test code here, Debug.Log the results
    5. }
    This way you can execute code without having to hit play, just right click the script in the inspector panel and the test option will be there.

    This is not a proper unit testing solution, of course, but if you just need to do a quick one-off test, this tends to work well.

    Cheers
     
    DoomDude99 likes this.
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    It depends on what you mean by "instantiate the game". I assume you mean without entering play mode, but that having the Unity Editor open is okay? Edit Mode tests are probably what you want:

    https://docs.unity3d.com/Manual/testing-editortestsrunner.html

    The caveat to using tests of this sort is that you'll need to make sure the code you want to access is within an Assembly Definition. This can put some constraints on the way you organize your code.
     
    DoomDude99 likes this.
  4. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    Yes, entering play mode takes a bit. I want to have them tested separately before using them in my game.
     
  5. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    Thanks. A battery of tests can actually be put in one method (avoiding clutter in the context menu).
     
  6. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    763
    There's a package in the Package Manager for testing.
     
    DoomDude99 likes this.
  7. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Have you actually used that package? It seems very new, and my assumption is that it's Unity's effort to remove the test runner from the base install of Unity and relocate it to a package, as they're doing with a lot of other optional features. Is the package manager's Test Runner any different than the built-in test runner? Documentation on that package doesn't seem to work right now.
     
    DoomDude99 likes this.
  8. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    763
    Colleagues at work used the one from the Asset Store, which is no longer available. I assume the asset store one has been moved to the package manager.

    The old AS version was good, but our projects run for 2 to 4 weeks of dev time, it adds a lot of extra work (for us) to keep the test scripts up to date, as well as the project itself - though my colleagues did praise it.
     
    DoomDude99 likes this.