Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 TinyTitanAndrew · Jan 04, 2018 at 01:43 PM · testingplaymodetest

TestRunner playmode IPrebuildSetup test doesn't run

Currently I've got my script set up to run using IPrebuildSetup so I can call the Setup() function to load a scene before the test runs, which so far means my test doesn't run at all. I've put in several Debug.Logs that never fire, so it's clear the test just doesn't run at all.

Here's the code:


 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEditor;
 using UnityEngine.TestTools;
 using UnityEditor.SceneManagement;
 using NUnit.Framework;
 using System.Collections;
 using System.IO;
 using System.Linq;
 using System.Collections.Generic;
 using System;
 
 public class TrainPlayModeTest : IPrebuildSetup
 {
     Game LocInstance;
     SavedPlayer Player;
 
     public void Setup()
     {
         EditorSceneManager.OpenScene(Application.dataPath + "/Scenes/sceneGame.unity");
     }
 
     [UnityTest]
     public IEnumerator TrainPlayModeTestWithEnumeratorPasses() 
     {
         Debug.Log("**The test started**");
         if(Game.Instance != null)
         {
             if(LocInstance == null)
             {
                 LocInstance = Game.Instance;
                 Player = LocInstance.Player;
             }
         }
 
         if(Player != null)
         {
             Debug.Log("Test success, player is not null");
             yield break;
         }
 
         Assert.Fail("Player was null, test failed");
         yield return null;
     }
 }

The scene will successfully switch in Setup() however TrainPlayModeTestWithEnumeratorPasses() never actually fires. Any thoughts?

Edit: Pay no mind to the amount of usings I have, I tend to clean those up when I'm done trying out various methods to get things to work lol

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Warnecke · Jan 05, 2018 at 09:18 AM

The Setup method given with IPrebuildSetup is executed in the editor before the game is build. The EditorSceneManager.OpenScene will change what scene is loaded in the editor. The scene that playmode or the standalone player uses should be set right before the test run. This can be done by using the NUnit Setup and TearDown tags.

 using System.Collections;
 using NUnit.Framework;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.TestTools;
 
 public class TrainPlayModeTest
 {
     private string initialScenePath;
     [SetUp]
     public void Setup()
     {
         Debug.Log("Load Scene");
         initialScenePath = SceneManager.GetActiveScene().path;
         SceneManager.LoadScene("Scenes/sceneGame");
     }
 
     [TearDown]
     public void TearDown()
     {
         SceneManager.LoadScene(initialScenePath);
     }
  
     [UnityTest]
     public IEnumerator TrainPlayModeTestWithEnumeratorPasses2() 
     {
         Debug.Log("**The test started**");
         
         yield return null;
     }
 }

Two things to note:

  • The scene that was initially loaded when the Setup of the test is performed should be restored on TearDown. This is done to avoid interfearing with other tests that will be run afterwards.

  • The scene that you load should be added to the build. This is done in the Build Settings window.

Comment
Add comment · Show 2 · 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 TinyTitanAndrew · Jan 08, 2018 at 04:20 PM 0
Share

This works for getting the scene loaded so I definitely thank you for that, however the scripts for the scene start executing immediately (because of pre-processing functions) and therefore start throwing nullrefs, is there any way to halt that until the scene is loaded?

avatar image HaraldNielsen TinyTitanAndrew · Jan 08, 2018 at 06:20 PM 0
Share

The Test Runner basically just executes the method in a coroutine. So it would work just as if you loaded a scene normally. Im pretty sure you need to skip a frame before its loaded. You could also just use the async method and yield until its done.

Documentation https://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene.html

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.

Follow this Question

Answers Answers and Comments

73 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

Related Questions

How to implement mandatory cleanup for the Unity PlayMode tests? 1 Answer

Unity Test Runner doesn't cleanup previous test results 1 Answer

How to load a scene in PlayMode tests 2 Answers

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

Testing Network Code in Unity 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges