0
\$\begingroup\$

I have followed the Unity Test Framework documentation to create an EditMode test assembly and a test script. When I try to test a function like Utils.SomeMethod(), the class is not recognized.

My IDE gives me the option to Reference Assembly-CSharp and use Utils which seems to successfully import the class as I gain autocomplete ability on the function names. But as soon as I try to run the test, the text changes back to red and I get a message that The name Utils does not exist in the current context.

What do I need to do to be able to test my class?

\$\endgroup\$
0
\$\begingroup\$

You should create an Assembly Definition for your main code. By default, Unity creates a predefined one called Assembly-CSharp, but this can't be manually referenced. The docs give a good understanding of what Assembly Definitions are.

Then you can right-click the project folder and choose to create a test assembly, adding your main assembly as a reference. You do this by clicking the little plus under the References section. I always make sure the test assembly is set to only the Editor, so it gets excluded from builds.

If you use Nsubstitute, you’ll need to reference that dll as well if it’s in your Plugins folder.

To see if it works, create a test; it should appear in the Unity Test Runner window and be able to run.

\$\endgroup\$
2
  • \$\begingroup\$ So the part I'm unclear about is making an assembly for my main code. How do I do that? \$\endgroup\$
    – Matt
    Nov 24 '20 at 0:50
  • \$\begingroup\$ Right click in your base folder. It could be as simple as your scripts folder. and click create assemble definition file \$\endgroup\$
    – Adam B
    Nov 24 '20 at 1:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .