• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by jamiltron · May 01, 2014 at 03:26 PM · testingawaketestunit

Is there a way to ensure Awake is called in Unit tests?

I have been using the Unity Test Tools but I am having an issue where I am creating a gameObject in the test script, attaching a component that I want to test, but Awake is not being called. Is there any way to ensure Awake gets called when the component is added via my test script? I really don't want to muddy up my component and add a public method that calls Awake or anything.

Thank you!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Deign · May 03, 2014 at 02:51 AM 0
Share

You could make Awake a public method as well. Otherwise you could also use reflection:

 $$anonymous$$ethodInfo method = scriptToTest.GetType().Get$$anonymous$$ethod("Awake", BindingFlags.NonPublic | BindingFlags.Instance);
 method.Invoke(this, new object[] { });

Or something to this effect, I forget the exact syntax, but google can help you there.

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by liortal · May 20, 2014 at 08:44 PM

Unit testing are about testing things in isolation. Having a test rely on the fact that there are callbacks that should be called by the Unity engine makes the unit test complex and some would say that it is not a unit test at all.

You have a few options:

  1. Define the Awake() method as public, so it can be called from the outside.

  2. Define the Awake() method as internal, and use the InternalsVisibleToAttribute. This allows you to set a specific assembly that internals will be visible to. See this link for more info: InternalsVisibleTo. If your tests are defined in the editor assembly, allow internals to be visible to the editor assembly.

  3. Call Awake() using reflection (ugly!!)

Your better option would be to use the Unity Test Tools and create an Integration test. This allows you to create a real game object with a real component on it. The engine will run it as part of a test scene and will call the Awake() method on your component. You would only need to define the passing criteria for this test (by calling Testing.Pass() for example when the test passes).

For more info on the Test Tools, check out this blog post: http://wp.me/p4kypp-6s

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image youblistermypaint · Apr 01, 2016 at 10:49 AM 0
Share

Thanks for the answer. I understand the isolation aspect of unit tests in theory, but in practicality pure isolation is almost impossible. This is especially true in Unity, given that most Unity objects are not mockable.

avatar image
0

Answer by Dissolute · May 01, 2018 at 07:35 PM

As of Unity 5.5, you can have Awake() run in an EditMode unit test by setting the runInEditMode flag to true on your behaviour.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image acollister · Sep 27, 2018 at 09:47 AM 0
Share

I'm using this approach in my edit mode unit tests, but finding that Start is not being invoked on my runInEdit$$anonymous$$ode component UNLESS I have the Rider debugger attached, which is quite baffling. Anyone else seen this..?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Is there a limit to Test Runner play mode test duration? 2 Answers

How to import the object from server to unity 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Start() not called upon instantation? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges