• 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
0
Question by kubimtk · Mar 17, 2018 at 04:57 PM · scripting problemtesting

Testing buttons in PlayMode Tests

In my Scene I have a Button and I want to test that the Event visible in the Inspector will be triggered and execute the correct action. In this case I have a setup Button and I want to test that the Setup Dialog appears which will be true if the dialog will be found by FindObject because it will be activated by the event handler specified in the Inspector. This is my test:

 [UnityTest]
 public IEnumerator SetupButtonTestSetupButton()
 {
     yield return PlayModeSetup();
     yield return null;
     var Settings_UI = GameObject.Find("Settings_UI");
     Assert.Null(Settings_UI);
     var setupButton = Object.FindObjectOfType<Button>();
     Assert.NotNull(setupButton);
     clicked = false;
     setupButton.onClick.AddListener(Clicked);
     setupButton.onClick.Invoke();
     //ExecuteEvents.Execute(setupButton.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler);
     Assert.True(clicked);
     yield return null;
     yield return new WaitForSeconds(1);
     Settings_UI = GameObject.Find("Settings_UI");
     Assert.NotNull(Settings_UI);
     var Settings_UITransform = Settings_UI.transform;
     Assert.NotNull(Settings_UITransform);
     Assert.True(Settings_UI.activeSelf);
 }
 
 private void Clicked()
 {
     clicked = true;
 }

Running my test an Exception is thrown at

 Assert.NotNull(Settings_UI);

That means that my Listener in the test was called but not the event specified in the Inspector. If I use the mouse to click the Button during WaitForSeconds the test goes green. How can I make the test go green by script? Thanks, Wolfgang

Comment
Add comment · Show 6
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 RobAnthem · Mar 17, 2018 at 05:29 PM 0
Share

Start by NOT using a coroutine. Also, why the heck are you using the assertion class?

The Assertion class should only be used in very rare cases. This is... very unorthodox coding. Did you follow a tutorial or something?

avatar image kubimtk RobAnthem · Mar 17, 2018 at 07:12 PM 0
Share

Play$$anonymous$$ode test without coroutines? Unit Tests without Assertions? I am quite new to Unity and Testing documentation is more than poor. I would appreciate any pointers to proper and not outdated documentation. Especially (but not only) how to secure future changes of scene object configurations breaking the intended usage. Until then: technically my test code should work but it does not. Any ideas?

avatar image hexagonius RobAnthem · Mar 17, 2018 at 07:57 PM 0
Share

A coroutine can be used and is documented. even assertions are recommended when you want errors without stopping the test execution which happens with regular LogError.

avatar image hexagonius · Mar 17, 2018 at 08:00 PM 0
Share

what is the event in the inspector doing?

avatar image kubimtk hexagonius · Mar 17, 2018 at 08:25 PM 0
Share

It calls a method with argument which deactivates all but the settings dialog where 3 ist the settings dialog number (see screen copy). I just changed from Runtime Only to Editor and Runtime but that didn't change the test result (and should not because Play$$anonymous$$ode is Runtime isn't it? Anyway: if I use the mouse to press the button during the test UI$$anonymous$$anager.ChangeUI is called and the Settings dialog appears "immediately" (the next frame latest). (And it plays a click sound which I do not test yet).

bildschirmfoto-2018-03-17-um-210810.png (110.2 kB)
avatar image hexagonius kubimtk · Mar 17, 2018 at 09:24 PM 0
Share

test running the click with static listeners only, using the coroutine and the regular version also doesn't call them?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kubimtk · Mar 18, 2018 at 01:05 PM

Oh No! So stupid: I did not address the Setup button but the first available button. Just by naming a variable it doesn't mean it references the corresponding object(grin).

Wrong:

  var setupButton = Object.FindObjectOfType<Button>();

Correct:

 var setupButtonGameObject = GameObject.Find("Settings Button");
 var setupButton = setupButtonGameObject.GetComponent<Button>();

Thank you all for your effort and your patience!

Comment
Add comment · 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

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

137 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 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 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 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 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 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

i need help on this script can anybody figure this out? 0 Answers

The type or namespace name `SceneManagment' does not exist in the namespace 1 Answer

How to get gameobject in another scene in play mode test script 2 Answers

How to access classes written in Assembly-CSharp inside Play mode Test assembly? 0 Answers

pick up item script issue 3 Answers

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