Skip to content

3. Assembly Definitions in Unity and Running Tests

Shahid Karim edited this page May 12, 2020 · 2 revisions

VERY IMPORTANT

  • Once you've followed all the steps below, try to generate a build manually.
  • If you get any errors:
    • You MUST add an asmdef file to EVERY Editor folder that is showing an error in the Console.
    • In the asmdef file uncheck Any platform then make sure only Editor is selected.
    • Keep trying to generate a build and doing the above until the build generates successfully.

To prepare the project for testing do the following:

  1. Right click on the Assets folder > Create > Assembly Definition. This will create an asmdef file in the Assets folder.
  2. Select the asmdef file, name it External, then make sure Auto Referenced and Override References are selected.
  3. Add Assembly Definition References as required (You can tell an assembly definition reference is required from reading the console)
  4. Add Assembly References as required. (You can tell an assembly reference is required when you have third party DLLs that need to be included into your game.
  5. Right click on your Scripts folder > Create > Assembly Definition. This will create an asmdef file in the Scripts folder.
  6. Select the asmdef file, name it Internal, then make sure Auto Referenced and Override References are selected.
  7. Add the same references as the one from the External asmdef file. Include the External asmdef file you created as well.
  8. Add the same Assembly References as the External asmdef file.
  9. Right click on the Assets folder > Create > Testing > Tests Assembly Folder. This will create a folder for your test scripts.
  10. Select the asmdef file, name it Tests, then make sure Auto Referenced and Override References are selected.
  11. Add only External and Internal to the Assembly Definition References.
  12. Add nunit.framework.dll if it wasn't already included in the Assembly References.

To create and run tests do the following:

  1. In the Tests folder that was created from the steps above, right click > Create > Testing > C# Test Script.
  2. Open the new Test script after renaming it to whatever test you want to do.
  3. Add [Test] above the methods that are methods you want to run as tests.
  4. Make assertions within the methods marked with [Test]
  5. In Unity, Click on Window > General > Test Runner and dock it to the screen.
  6. Once the previous steps have been followed, the methods that were marked as Test will now display on the Test Runner window.
  7. To run the tests click Run All or double click on the test you want to run.

To make the tests run using the CI/CD pipeline do the following: