Search Unity

Real testing experience and examples

Discussion in 'Testing & Automation' started by drWoZD, May 26, 2021.

  1. drWoZD

    drWoZD

    Joined:
    Sep 20, 2017
    Posts:
    1
    Hello everyone!
    I'm pretty new to writing tests in Unity but I really want to start adding tests to my projects becouse on my last job we had manual testing nightmare:(

    And It just interesting for me to see some real project with tests integrates.
    Are there any open source projects with tests?

    Or maybe someone could share own expirience of writing tests in Unity?
     
    timeapusok likes this.
  2. timeapusok

    timeapusok

    Joined:
    Apr 15, 2020
    Posts:
    59
    drWoZD and VolodymyrBS like this.
  3. sbergen

    sbergen

    Joined:
    Jan 12, 2015
    Posts:
    53
    Here would be my main takeaways from real testing experience:
    1. Write as much as feasible as plain C# code, and test it like you'd test any C# code.
    2. To make Unity code (e.g. MonoBehaviours) testable, you'll really want to use some dependency injection framework, e.g. Zenject. This is an excellent talk on the subject (including notes about testing): Unity Architecture in Pokémon Go
    3. Using a mocking framework like NSubstitute or Moq will be very useful, especially if you have DI in use. However, I'm not aware of a way to make these work in player tests, so you'll have to run them in the Editor (they can still be play mode tests, though).
    4. High level tests including Unity code will inherently be asynchronous, and my experience is that the difficulty of figuring out why they started getting flaky (they will at some point) is your worst enemy. You'll want to write your tests in a way where it's easy to figure out what was happening when things failed. This was my main motivation to write Responsible, which includes a small project with example tests that I tried to make as realistic as possible, while still keeping the sample project very small and easy to understand.