Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unit testing in unity

Discussion in 'Scripting' started by DoomDude99, Jul 2, 2019.

Thread Status:
Not open for further replies.
  1. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    How are unit tests done in unity? I created a separate project in the same solution but the classes I want
    to test seem outside of scope (even though they are public and referenced through the namespace name):

    For example, the class Config.cs:

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6.  
    7. namespace Assets.Scripts.WeatherAPI
    8. {
    9.     public static class Config
    10.     {
    11.         public static string CONFIG_FILE_PATH = "config.json";
    12.  
    13.  
    14.         public static void Main(string[] args)
    15.         {
    16.  
    17.         }
    18.     }
    19. }
    is not visible in project GetWeatherWebTest, file UnitTest1.cs:

    Code (CSharp):
    1. using System;
    2. using Microsoft.VisualStudio.TestTools.UnitTesting;
    3. using Assets.Scripts.WeatherAPI.Config;
    4.  
    5. namespace GetWeatherWebTest
    6. {
    7.     [TestClass]
    8.     public class GetWeatherTest
    9.     {
    10.         [TestMethod]
    11.         public void GetWeatherClient()
    12.         {
    13.             string path = Config.CONFIG_FILE_PATH;
    14.             Console.WriteLine(path);
    15.         }
    16.     }
    17. }
    Code (CSharp):
    1. Severity    Code    Description    Project    File    Line    Suppression State
    2. Error    CS0246    The type or namespace name 'Assets' could not be found (are you missing a using directive or an assembly reference?)    GetWeatherWebTest    D:\Projects\Unity\WallPaper1\GetWeatherWebTest\UnitTest1.cs    3    Active
    3.  
    4. Severity    Code    Description    Project    File    Line    Suppression State
    5. Error    CS0103    The name 'Config' does not exist in the current context    GetWeatherWebTest    D:\Projects\Unity\WallPaper1\GetWeatherWebTest\UnitTest1.cs    13    Active
    6.  
    are thrown upon run/debug. The solution space looks like:

    unit-testing-unity.png
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    Unity (re)builds the solutions and projects based on your folder layout, so generating them from VS isn't going to work very well.

    To create a Unit test project, set up an assembly definition file in a folder, and check the "Test Assemblies":

    upload_2019-7-2_11-24-53.png

    upload_2019-7-2_11-25-17.png

    Create Unit tests under that folder. You can run them either from VS or through the Unity test runner:


    upload_2019-7-2_11-26-15.png

    Note that the Test Runner also has a button to set up all of this for you. There's both EditMode tests (normal Unit Tests that just runs functions) and PlayMode tests (for integration testing where the game is running)
     
    Doug_B, jvo3dc and DoomDude99 like this.
  3. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    Thanks. I managed to create a test assembly but don't know how to run individual tests. Can you debug these tests in vs?
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    I think so, but I haven't used VS for quite some time. It would have to be supported by VS tools for Unity.
     
  5. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    Thanks. I created a "Test Assembly Folder" and a a script inside:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using NUnit.Framework;
    4. using UnityEngine;
    5. using UnityEngine.TestTools;
    6. using Assets.Scripts.WeatherAPI;
    7.  
    8. namespace Tests
    9. {
    10.     public class WebRequestTests
    11.     {
    12.         // A Test behaves as an ordinary method
    13.         [Test]
    14.         public void WebRequestTestsSimplePasses()
    15.         {
    16.             // Use the Assert class to test conditions
    17.             System.Diagnostics.Debugger.Launch();
    18.             string url = "https://www.yahoo.com/news/weather/";
    19.             string res = WeatherGetRequest.getWeather(url);
    20.             DeveloperConsole.print("Got web page: " +  res);
    21.         }
    22.  
    23.         // A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use
    24.         // `yield return null;` to skip a frame.
    25.         [UnityTest]
    26.         public IEnumerator WebRequestTestsWithEnumeratorPasses()
    27.        {
    28.             // Use the Assert class to test conditions.
    29.             // Use yield to skip a frame.
    30.             System.Diagnostics.Debugger.Launch();
    31.             string url = "https://www.yahoo.com/news/weather/";
    32.             string res = WeatherGetRequest.getWeather(url);
    33.             DeveloperConsole.print("Got web page: " + res);
    34.             yield return null;
    35.         }
    36.     }
    37. }
    38.  
    but when I try to run unity (start the app) the following error is thrown:

    Code (CSharp):
    1. error CS0246: The type or namespace name 'Assets' could not be found (are you missing a using directive or an assembly reference?)
    2.  
    even though in VS there is no error. VS can't see the tests though (can't run or debug any).
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    There's a caveat with Assembly Definitions - they can only see code in other assembly definition files. So you'll have to structure the code base around that. See the docs.
     
    DoomDude99 likes this.
  7. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    I decided to go for a standalone C# project that allows more convenient unit testing (I'll just move the assembly into unity when it's done).

    Do you know how you can add a dependency (installed with NuGet) into unity? for a JSON parser.
     
  8. Janneman96

    Janneman96

    Joined:
    Oct 21, 2019
    Posts:
    2
    I hate the Unity testrunner. Sometimes I can't run selected tests anymore when using tests with parameters and it doesn't show execution time. I've only been using it for a short while, but I'd really like a viable way to use Microsoft.VisualStudio.TestTools.UnitTesting for my C# files.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    That's not a good reason to necro an old thread like this though.

    If you want to discuss Unit Testing or the test runner in Unity then please use the dedicated forum for it here.

    Hopefully though, it'll be more constructive than your last two posts if I'm honest.

    Thanks.
     
    Kurt-Dekker likes this.
Thread Status:
Not open for further replies.