Run Unity tests in Rider 2018.1

In our last couple of posts about new Unity features in Rider 2018.1, we’ve seen that updating our Unity editor plugin to be smarter now allows us to control play mode, and bring the Unity console directly into Rider.

In this series:

The intent here is to reduce the amount of time you spend switching between Rider and the Unity editor. If you’re working with code, let’s stick to the code editor. And so we can single step frames from the debugger, and see log entries directly in the code editor, and Rider makes the stack traces clickable to easily navigate to the right places in your code.

We believe that reducing this context switching will help you concentrate on your game logic, rather than your tooling, which we also hope will help improve the quality of your game or visualisation. But we want to do more than hope, so we’ve now integrated Unity’s unit tests into Rider’s test runner, making it easier than ever to test your code.

This allows you to run tests that interact with Unity’s APIs, and which can step single frames, all from within Rider. And of course, you can explore the results of your Unity specific tests, just like you would normal tests – you can filter by result, and click on stack traces to navigate your code.

Rider test tool window

For those not familiar with Unity’s unit tests, there is a constraint that these tests must be run inside the editor application instance. This is because Unity isn’t actually a .NET application, but is a native application, with a Mono based scripting layer, and many of Unity’s APIs are native APIs. If you try and call those APIs in a normal .NET test runner, you’ll see the following rather unfriendly error message:

It’s not possible to mock these calls, but instead, you must run the tests inside the editor instance.

Unity editor test runner window

Fortunately, our new Unity editor plugin allows us to do just that, listening to requests from Rider to start running the tests, and then reporting back to Rider, where we can integrate the progress and results seamlessly into the standard test runner.

And Unity support is fully integrated with our unit testing experience. We discover unit tests from the code and highlight test classes and methods right inside the editor. Clicking on the gutter icon, or pressing Alt+Enter will show a menu we can use to run the tests.

NUnit test Alt+Enter menu

You’ll notice that the menu shows Debug as well as Run. Currently, debugging isn’t supported, even though the item is available in the menu. If you select Debug, it will simply run the tests. An easy workaround is to attach the editor via the normal debug button in the top right of the main window, and then run the tests – you’ll be able to set breakpoints and debug as normal. We hope to get debugging enabled again as soon as possible.

Debug toolbar

As if running tests directly from the editor wasn’t reason enough to love this feature, all of Rider’s usual unit testing tools are available, too. You can run all tests in a project or a selection of test classes or even just a single test. You can also group how the tests are shown in the tool window, and filter based on status – passed, failed, skipped.

But best of all, unlike in the Unity editor, where the result output is a block of plain text, Rider will parse the test output, and make stack traces, or even just a class or method name into hyperlinks.

Test runner stack trace navigation

And Rider also includes support for tests marked with the [UnityTest] attribute, not just the traditional nunit [Test] attribute. These are tests with a return type of IEnumerator, where a yield return can skip frames in the editor.

Support for UnityTest attribute

Finally, Rider also includes file templates to quickly create a new test class.

File templates menu

The integrated test runner is supported with Unity 5.6 and above, which is when the test runner was first integrated into the Unity editor. Rider has no special support for Unity tests prior to Unity 5.6, but will run any tests it finds as standard nunit tests.

We’d love to hear your feedback on this and the other new features in Rider 2018.1. Please leave comments here, and you can add feature requests and issues to GitHub or YouTrack. And of course, don’t forget to download Rider 2018.1 and try it out!

This entry was posted in How-To's and tagged , , . Bookmark the permalink.

17 Responses to Run Unity tests in Rider 2018.1

  1. George cook says:

    Thanks so much guys. These are such great developments. You guys really need to get a leading unity Dec to present with you at Unite. I’m sure a lot of people have no idea what they are missing and now your product is ready for primetime.

  2. Trond says:

    I can not get this to work. When i try to run these tests, they just get stuck as “Pending”. What am i doing wrong?

    • Geoff says:

      Same problem. Running Unity 2017.1.2f1.

      • Matt Ellis says:

        Sorry to hear that – is this still an issue? Currently tests require Unity to be running and connected before things will work. We also noticed an issue with the way we installed the plugin, so it might be worth restarting Rider and trying again.

        • quabug says:

          same problem with rider 2018.1 and unity 2018.1
          always pending while unity is running and rider connected to unity

        • hhatinen says:

          Same problem here as well. Debugging works fine so it seems like it connects to unity ok.

        • Felix says:

          Exact same problem here (Rider 2018.1, Unity 2018.1).

          • Matt Ellis says:

            A number of issues with more recent builds of Unity have been fixed in the nightly builds. They should be available soon in Rider 2018.1.1

  3. Geoff says:

    2018.1 EAP is working for me as advertised, but it does not support running with tests annotated as TestCase or TestCaseSource. I’ve heard, but not tried, tests with Values parameters also do not work from Rider. These things do work when running tests from within Unity. Looking forward to having all the kinks worked out, as this is a great feature!

    • Matt Ellis says:

      This has been fixed in recent nightly builds, so should come out in Rider 2018.1.1 (or possibly 2018.1.2)

  4. lesny says:

    Great function! Are there any plans to also have this in ReSharper with Unity plugin??

  5. Garth Smith says:

    This is great! Anything that helps with testing is much appreciated!

    Does anyone know if there is a way to get a code coverage report from a Unity project?

  6. Jerome says:

    Whenever I run Unity tests from within Rider, I always get “Inconclusive: Test not run” in the unit tests window. I’ve also tried creating a new test using the Rider “Play mode test” template, and those never run either.

    When I initiate the test, you can see Unity briefly act like it’s switching scenes like normal, but then it quickly stops.

    The bottom right corner of Rider shows that I’m connected to Unity.

    JetBrains Rider 2018.2.3
    Build #RD-182.4231.496, built on September 13, 2018
    JRE: 1.8.0_152-release-1248-b8 amd64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Windows 10 10.0
    Unity 2017.3.1f1

    • Matt Ellis says:

      Unfortunately, play mode tests aren’t supported, only edit mode tests. We’re currently working on an implementation and hope to have it ready for a future release.

Leave a Reply

Your email address will not be published. Required fields are marked *