Version: 6000.3
语言: 中文
在安装前或拆卸后执行作
测试作的执行顺序

在构建时设置和清理

有时,您可能希望在生成测试之前对 Unity 或文件系统进行更改,然后在测试运行后清理这些更改。可以通过以下方式之一将生成前设置和生成后清理阶段合并到测试中:

PrebuildSetupPostBuildCleanup如果应用属性的测试或测试类包含在当前测试运行中,则属性将运行。当您运行所有测试或定义包含测试的筛选器时,将包含该测试。如果多个测试引用相同的生成前设置或生成后清理,则它只运行一次。

以下示例实现IPrebuildSetup在测试类上定义在生成测试之前要执行的工作:

[TestFixture]
public class CreateSpriteTest : IPrebuildSetup
{
    Texture2D m_Texture;
    Sprite m_Sprite;
    
    public void Setup()
    {

#if UNITY_EDITOR

        var spritePath = "Assets/Resources/Circle.png";

        var ti = UnityEditor.AssetImporter.GetAtPath(spritePath) as UnityEditor.TextureImporter;

        ti.textureCompression = UnityEditor.TextureImporterCompression.Uncompressed;

        ti.SaveAndReimport();

#endif
    }

    [SetUp]
    public void SetUpTest()
    {
        m_Texture = Resources.Load<Texture2D>("Circle");
    }

    [Test]
    public void WhenNullTextureIsPassed_CreateShouldReturnNullSprite()
    {

        // Check with Valid Texture.

        LogAssert.Expect(LogType.Log, "Circle Sprite Created");

        Sprite.Create(m_Texture, new Rect(0, 0, m_Texture.width, m_Texture.height), new Vector2(0.5f, 0.5f));

        Debug.Log("Circle Sprite Created");

        // Check with NULL Texture. Should return NULL Sprite.

        m_Sprite = Sprite.Create(null, new Rect(0, 0, m_Texture.width, m_Texture.height), new Vector2(0.5f, 0.5f));

        Assert.That(m_Sprite, Is.Null, "Sprite created with null texture should be null");

    }
}

注意:使用#if UNITY_EDITOR如果你想访问仅编辑器API,但设置/清理在游戏模式程序集中。

执行顺序

所有设置都按确定性顺序一个接一个地运行。首先运行使用属性定义的设置。然后,任何实现接口的测试类在其命名空间中按字母顺序运行,这与测试的运行顺序相同。

在播放器构建中,清理会在测试后立即运行。在 Unity 编辑器中,清理仅在相关测试完成后运行。

其他资源

在安装前或拆卸后执行作
测试作的执行顺序