Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feature Request Mock Time class for deterministic tests

Discussion in 'Testing & Automation' started by NabilStendardo, May 11, 2021.

  1. NabilStendardo

    NabilStendardo

    Joined:
    May 29, 2020
    Posts:
    2
    Since a very important aspect of automated testing is determinism, this idea, if implemented, would allow to remove an important cause of non-determinism in automated game testing, that is Time.deltaTime and everything derived from it.

    The idea would be a set of API calls that would allow to programmaticallly set Time.deltaTime (and thus derive other fields of Time from it) regardless of the time it actually took the system to render a given frame, which can allow us to run tests that will deterministically pass or fail regardless of the frame rate of the machine.

    The API call should be able to take either a scalar constant value ("this test runs at 60 fps"), a Func<int,float> (the int parameter being the frame number from the moment you call the function) or an IEnumerator to be able to set either a seeded-prng formula, or to simulate an fps drop at a given time. There would need to be some function to reset the deltaTime to its default behaviour and/or some scoping mechanism to make sure that it doesn't affect the entire editor session (I am not an expert in C#/Unity internals myself, I am sure the devs would figure out how to scope it).
     
    neonblitzer and JesseSTG like this.
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    JesseSTG likes this.
  3. NabilStendardo

    NabilStendardo

    Joined:
    May 29, 2020
    Posts:
    2
    Thanks a lot for your prompt reply. I guess this would work (even though the use case mentioned in the docs isn't the same as the testing use case I mentioned). First of all, this mentions it affects Time.time, but does it affect Time.deltaTime, since it's what most game components would use to compute their motion? Also, do any standard provided components use Time.unscaledTime?
     
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    Yeah, the documentation is a bit focused on the 'video capture' use case, but it should be applicable to any situation where you want a deterministic time-per-frame.

    Yes, it affects Time.deltaTime too.

    Some components can be configured to - for example the Animator component can be configured with
    updateMode = AnimatorUpdateMode.UnscaledTime
    . I think for those components you'd want to modify them as part of test setup to use Normal mode instead, which would make them subject to Time.deltaTime.