Unit testing in editor - how to remove folder that is imported during the test

Hi all,

I’m trying to create a test for an editor plugin that beneath other functionality imports required unity packages from a given location. The test shall verify that the correct packages were imported by cheking the assets in the file system. The test itself already works fine, but I have an issue when cleaning up: The problem is that the test cannot delete the folder that it imported after its work is done (using NUnit / TearDown function I’m trying to delete a folder named “CommonImport” directly located in the “Assets” folder). Using the System.IO.Directory.Delete I’m getting an excepion:

System.UnauthorizedAccessException : Access to the path ‘D:<some_path>\Unity\MyProject\Assets\CommonImport\TestAsset1’ is denied.

Using the AssetDatabase.DeleteAsset there is no error but the folder is still there after the call completes. I tried to remove the files in the folder first, but still the result is the same.

So my question: What is the correct way to remove this folder OR - in my opinion even better - can I somehow tell the test to change the asset folder / package import target to a different location during its runtime?

Thanks,
Thomas

First try to figure out why that raises an unauthorized access. If it’s permission issues that should be solvable but I doubt that since both tests and the editor should run with the same user privileges.

I’m thinking that Teardown actually runs right after importing the packages because that causes a domain reload, therefore the test didn’t actually complete the way you think it does (it gets interrupted by the domain reload). Could that be the case?

Otherwise, if there’s a way to force a synchronous import (like in AssetDatabase.Refresh) I would favor that in tests.

There’s also a callback interface:
https://docs.unity3d.com/Packages/com.unity.test-framework@1.3/api/UnityEditor.TestTools.TestRunner.Api.ICallbacks.html

You could implement that and cleanup after the test run completes.

Thanks for your reply. I tried around a bit and indeed calling the AssetDatabase.Refresh() together with a loop that uses yield and waits until the first imported file is seen, I was able to fix the problem. The test now passes and works fine.

Thanks,
Thomas