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
1
Question by pb0o6 · Apr 14, 2017 at 03:28 PM · editor-scriptingtestingtestnunit

How to Run Editor Test Runner test from editor script in 5.6?

Basically, I would like a post build script that runs all of our unit tests and prints out the results in the debug log. However, in Unity 5.6, I can not find any way of doing this programmatically (aside from the command line). Is there any way to trigger these tests from a custom editor script?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tomek-Paszek · Apr 25, 2017 at 01:36 PM

The class is not public but if you really want to, you can reflect the Batch class and invoke it.

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
avatar image
0

Answer by mplavcan · Apr 18, 2018 at 09:07 PM

Apologies for recognizing an old thread, but this is still the most relevant match in the answers database, even after a year, and I have additional detail to add to this approach:

Reflecting the code in Batch is not sufficient. Here' s an implementation of that:

 static void RunAllUnitTests()
 {
     string testPlatform = "editmode";
     string resultFilePath = "ManualRunUnitTestRun.xml";
     string[] testFilters = { };
     string[] testCategories = { };
 
     var assembly = Assembly.Load("UnityEditor.TestRunner");
     var batchType = assembly.GetType("UnityEditor.TestTools.TestRunner.Batch");
     var runTestsMethod = batchType.GetMethod("RunTests", BindingFlags.Static | BindingFlags.NonPublic);        
     runTestsMethod.Invoke(null, new object[] {testPlatform, resultFilePath, testFilters, testCategories});
 }

While this code does correctly run the unit tests, any invocation of Batch.RunTests() completes by quitting the editor. Until this is addressed, test runs cannot be automated.

(The automated quit is there because the command-line run of -editorTests, yes?)

Since the class is intentionally hidden, it's not a "bug", per se, but it is hindering automation of testing. I would like to see the quit behavior separated from the test run, so an automated build script could both run the tests and build the code, without need for multiple editor invocations.

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
avatar image
0

Answer by chrpetry · Aug 03, 2018 at 08:35 AM

Is there any news on this?
I would also like to execute tests via script (right before a build script? :) )
A boolean result or a List of boolean test results would be great!

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
avatar image
0

Answer by mityugovmaxim · Jul 23, 2019 at 11:42 AM

     public static void RunTests(string[] _Categories, Action<ITestResult> _Finished = null)
     {
         var engineAssembly = Assembly.Load("UnityEngine.TestRunner");
         var editorAssembly = Assembly.Load("UnityEditor.TestRunner");
         
         Type      filterType       = engineAssembly.GetType("UnityEngine.TestTools.TestRunner.GUI.TestRunnerFilter");
         FieldInfo filterCategories = filterType.GetField("categories");
         object    filter           = Activator.CreateInstance(filterType);
         filterCategories.SetValue(filter, _Categories);
         
         Type       launcherType   = editorAssembly.GetType("UnityEditor.TestTools.TestRunner.EditModeLauncher");
         MethodInfo launchMethod   = launcherType.GetMethod("Run");
         FieldInfo  runnerField    = launcherType.GetField("m_EditModeRunner", BindingFlags.NonPublic | BindingFlags.Instance);
         
         object     launcher       = Activator.CreateInstance(launcherType, filter);
         object     runner         = runnerField.GetValue(launcher);
         Type       runnerType     = runner.GetType();
         
         FieldInfo  finishedField     = runnerType.GetField("m_RunFinishedEvent", BindingFlags.NonPublic | BindingFlags.Instance);
         object     finishedCallback  = finishedField.GetValue(runner);
         Type       finishedType      = finishedCallback.GetType();
         MethodInfo finishedSubscribe = finishedType.GetMethod("AddListener");
         finishedSubscribe.Invoke(finishedCallback, new object[] { CreateAction(_Finished) });
         
         launchMethod.Invoke(launcher, new object[] { });
     }
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 chrpetry · Jul 24, 2019 at 06:03 AM 0
Share

That looks somewhat promising. Where is the "CreateAction()" function from? I can't find any assembly for this.

avatar image mplavcan · Jul 24, 2019 at 10:29 PM 0
Share

I am unable to execute the above code in 2018.3:

  NullReferenceException: Object reference not set to an instance of an object

Also: ITestResult is an internal interface, so it would also need to be extracted via reflection. CreateAction() is probably unnecessary, as it could be handled a different way. It appears to be an extension method of $$anonymous$$ethodInfo:

Can you please explain how the above example addresses the existing issues?

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

9 People are following this question.

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

Related Questions

How to import plugins inside tests in Unity Test Runner 1 Answer

Unity 5.6 Unit Testing 1 Answer

Testing editor scripting 1 Answer

How can I run Unity tests with some defined nUnit category 2 Answers

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


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