- Home /
How to load a scene in PlayMode tests
I need to load a scene to use it for PlayMode unit tests.
But when I add the following code:
[SetUp]
public void SetUp(){
SceneManager.LoadScene("MainScene");
}
all the tests fail.
Even stuff that should always be true fails in test runner, like the line
Assert.AreEqual(true, true);
My question is: how can I load scenes in PlayMode tests?
Answer by jtok4j · Sep 10, 2020 at 05:35 PM
Greetings,
Just curious whether you have a line which calls SetUp() ?
Example:
Setup() ;
Would need to be called like the above in a script when something happens. So, if you never actuall call/run Setup() it will never do the scene loading. Sorry, it's a bit hard to troubleshoot without seeing any other code used.
Keep on Creating!
Unity TestRunner automatically calls the function with attribute [SetUp].
Now add this line, ( after: Scene$$anonymous$$anager.LoadScene("$$anonymous$$ainScene"); ) and check the console when in Play$$anonymous$$ode after you thing TestRunner has called the SetUp function.
debug.log("Just called the Setup() Function") ;
This will print "Just called the Setup() Function" when the Setup function is called. If this doesn't appear in the Console, you'll know that Setup isn't actually being called.
Thanks, but you misunderstood my question. The scene definitely loads, I can see it load on the screen.(Yes I also tested it with Debug.Log() ). The problem is that loading the scene causes all unit tests to always fail.
$$anonymous$$y question is why does this happen and how can I load scenes in unit tests and have the tests work normally.
Answer by unity_399E6EDE23F69583CC01 · May 19 at 11:04 AM
In case anyone stumbles upon this, I found another tag called [OneTimeSetUp] to use instead of [SetUp]. I don't have a anything to test this with so I don't know if it works, but it's worth a shot