How do I setup and teardown asset prefabs?

I'm new to unity and i'm trying to generate some playmode tests.

The tests work individually, but when run in sequence they all fail. This is due to the instantiated prefabs overlapping and messing with the tests.

The prefabs constitute the level and ai in the scene.

A general outline of my test is as follows:

public class Test
    {
        private GameObject prefab =
            AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/prefab.prefab");

        [UnityTest]
        public IEnumerator Do_Test()
        {
            var prefabInstance =
                Object.Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
            yield return null;
            var Component = prefabInstance.GetComponent<Component>();
            // Assert some stuff with component, etc...
        }

        [UnityTest]
        public IEnumerator Do_Test_two()
        {
            var prefabInstance =
                Object.Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
            yield return null;
            var Component = prefabInstance.GetComponent<Component>();
            // Assert some other stuff with component, etc... (this will fail)
        }
    }

I have tried to add setup and teardown methods to delete the prefabs but they don't work:

 public class Test
    {
        private GameObject prefab;

        private GameObject prefabInstance;

        [UnitySetUp]
        public IEnumerator Init()
        {
            prefab =
                AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/prefab.prefab");
            yield return null;
            prefabInstance =
                GameObject.Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
            yield return null;
        }

        [TearDown]
        public void Dispose()
        {
           // how can I delete the prefabs here?
        }
}

If I try to add an Object.Destroy method in the teardown section I get the error:

'[Error] Destroying assets is not permitted to avoid data loss.'

Is there a proper way to setup and teardown GameObjects while testing?

Do you need all the yield return null statements? If you change the [UnityTest] and [UnitySetUp] attributes to [Test] and [SetUp] respectively you can run your tests as normal methods rather than as coroutines.

These are to make sure that the GameObjects are instantiated. Also, as far as i'm aware [UnityTest] is required to run tests in playmode. The coroutine is used to skip frames at runtime

UnityTest is not required for playmode, but indeed it is needed if you want to actually skip frames.

When you did the Object.Destroy() call, you were applying that to the prefab, or the prefabInstance? You should only do it to the prefabInstance.

I found the solution to my problem. It was a test for a scene using Unity's ML Agents machine learning framework. Training is coordinated by a singleton class and this meant there was some continuity between tests behind the scenes (I'm not sure what). I fixed it using:

[TearDown]
        public void TearDown()
        {
            Academy.Instance.Dispose();
        }

Glad you figured it out :)