Search Unity

Get Script path from a Test Script

Discussion in 'Testing & Automation' started by Maeslezo, Sep 2, 2019.

  1. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    98
    Hello,

    I have a test script with several unit tests.

    I would need the path of this script, because I want to load something, and I want the path to be relative to the script.

    Normally I use MonoScript.FromMonoBehaviour or MonoScript.FromScriptableObject to get the Monoscript, and from there, I can get the path.

    The problem is that a Test Script is neither a monobehaviour nor a scriptable object, so I can not use them.

    I know I can hardcode the full path, but the problem is that this test suite is going to be distributed and it could be placed anywhere into the project.

    Any idea?
     
  2. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,184
    Try
    new System.Diagnostics.StackTrace(1).GetFrame(0).GetFileName()
    .
     
  3. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    98
    It doesn't work. It returns null

    upload_2019-9-3_11-53-30.png
     
  4. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,184
    Ah, interesting. Do any of the frames in the StackTrace have usable file info? I can only see the first 4 in your screenshot there.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,214
    Why does MonoScript only support FromMonoBehaviour and FromScriptableObject? A FromType would be very useful - I'm guessing that you don't have that data internally, or?
     
  6. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,184
    Correct. We're only able to do it for FromMonoBehaviour and FromScriptableObject because we enforce that the type name matches the file name in those cases.
     
  7. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    98
    I don't think so
    upload_2019-9-5_10-13-30.png
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,214
    Looks like you have a really, really, really deep reflection IEnumerator. Which is kinda how test frameworks work.

    If you follow the rule "test class name must match .cs file name", you can do AssetDatabase.Find("t:script").Select(AssetDatabase.GUIDToAssetPath), iterate the results, and find one that has the same same.

    The problem is of course that there might be duplicate names, but that's probably not very likely?
     
  9. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    98
    It is a good workarround, but actually I thought there was an official way to do it.

    Thank you!
     
  10. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,184
    The thing that surprises me is that TestSimplePasses is not at the top. Possibly it's being inlined by the compiler or something like that.

    You could also try calling a method using the [CallerMemberName] attribute - like a function that uses that on a parameter and just returns the parameter.
     
  11. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    459
    Maeslezo likes this.
  12. superpig

    superpig

    Quis aedificabit ipsos aedificatores? Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,184
    Doh! Stupid mistake. Thanks @M_R.
     
  13. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    98
  14. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    262
    Note that you can get filenames for any type by using Mono.Cecil (which is available as package in Unity) and it's ability to read pdb debug symbols (that's what the debugger uses to have you open script files at specific lines during exceptions).
     
  15. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,231