Search Unity

Attribute for a test only public method.

Discussion in 'Testing & Automation' started by LCLapierre, Oct 8, 2019.

  1. LCLapierre

    LCLapierre

    Joined:
    Apr 29, 2019
    Posts:
    15
    Hello,

    I'm currently writing unit tests for my classes and there are multiple classes that need a specific setup so we can see the widget on screen. Those are UI tests so the canvases and the settings for the tests are quite different than in game, but I just want to make sure that the visuals are updated correctly and that the tests don't crash. Anyways, what I did so far is create a method InitForTests and put this method inside a #if UNITY_EDITOR #endif block. The problem here is that this code executes in play mode of course and not in standalone (good behaviour).

    I was wondering if there was an attribute I could put on my method so it only compiles or is only used for when running tests to avoid other programmers of calling this method by mistake.

    Thanks!
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,659
    If it's called
    InitForTests
    , is it really plausible that someone would call it by mistake?

    You could use the [EditorBrowsable] attribute to hide the method from autocomplete, maybe.