Can I use code coverage on mobile device? Like Android or ios

Reference to Unity - Scripting API: Coverage, I use this code:

using UnityEngine;
using UnityEngine.TestTools;

// A simple test class to get coverage information for.
public class CoverageClass
{
    public bool CoveredMethod1()
    {
        return true;
    }

    public bool CoveredMethod2()
    {
        return false;
    }
}

public class CoverageExample : MonoBehaviour
{

    void Start()
    {
        Coverage.enabled = true;
    }

    void Update()
    {

        // Create an instance of the test class and call both test methods
        // to make sure the class has had some coverage.
        CoverageClass coverageClasss = new CoverageClass();
        coverageClasss.CoveredMethod1();
        coverageClasss.CoveredMethod2();

        

        // Now get coverage statistics for all covered methods. Note that this
        // will return statistics for all methods that have executed since Coverage.ResetAll()
        // was last called, i.e. this method (CoverageExample.Start()), CoverageClass.CoveredMethod1(),
        // and CoverageClass.CoveredMethod2().
        CoveredMethodStats[] stats = Coverage.GetStatsForAllCoveredMethods();

        for (int i = 0; i < stats.Length; i++)
        {
            Debug.Log("Method Name: " + stats[i].method.ToString());
            Debug.Log("Method has " + stats[i].totalSequencePoints + " sequence points");
            int coveredSequencePoints = stats[i].totalSequencePoints - stats[i].uncoveredSequencePoints;
            Debug.Log("of which " + coveredSequencePoints + " were covered.");
        }

        Coverage.enabled = false;
    }
}

It works On the Editor, But on Android, this error is reported

Autoconnected Player "Autoconnected Player" InvalidOperationException: Coverage is not enabled.

May I ask if this can only be run on Editor, and is there any other method that can be run on Android or IOS

Go read the reference you link above, it explains precisely when you can use Code Coverage.

but you can gather coverage data in Unity at any time when the Editor is running.

This likely means that the editor has to be the thing actually running the code in question, so unless you can get the editor running on Android / iOS :slight_smile: the answer would be no.