Unity 之 Assertion(断言)另一种 Debug

简介: 在日常开发中,我们会经常使用Debug或对应的封装输出日志,在Unity Debug输出到屏幕并保存到本地笔者也曾有过介绍,今天笔者和大家聊聊另一种Debug输出 --- 【断言】,,笔者所介绍的断言库早在unity5.1版本中就已经出现。

在日常开发中,我们会经常使用Debug或对应的封装输出日志,在Unity Debug输出到屏幕并保存到本地笔者也曾有过介绍,今天笔者和大家聊聊另一种Debug输出 --- 【断言】,,笔者所介绍的断言库早在unity5.1版本中就已经出现。在单元测试中断言的使用频率还是很高的。

官方博客:https://blogs.unity3d.com/cn/2015/08/25/the-unity-assertion-library/

官网API:https://docs.unity3d.com/ScriptReference/Assertions.Assert.html


有说错或不准确的地方欢迎留言指正


在使用上还是比较简单的,主要API如下:

  • Assert.AreEqual 与 Assert.AreNotEqual
  • Assert.AreApproximatelyEqual 与 Assert.AreNotApproximatelyEqual
  • Assert.IsTrue 与 Assert.IsFalse
  • Assert.IsNull 与 Assert.IsNotNull
检测是否违背指定的相等或不相等原则
    public void TestAssertEqual()
    {
        Assert.AreEqual("123", "456", "断言检测违背相等原则");

        Assert.AreNotEqual("123", "123", "断言检测违背不相等原则");
    }
img_b3a81722594220f8269fda167fc3100c.png
检测是否约等于或不等于是否成立
    public void TestAreApproximatelyEqual()
    {
        // 默认允许误差小于 0.00001f,这里我们制定误差为 0.01f
        Assert.AreApproximatelyEqual(0.9f, 0.91f, 0.01f, "断言检测 约等于 不成立");

        Assert.AreNotApproximatelyEqual(0.9f, 0.9000001f, 0.01f, "断言检测 不约等于 不成立");
    }
img_476fbb8fe87a25c412b7067d2ac2cef0.png
检测数值为True或False

    public void TestIsTrue()
    {
        Assert.IsTrue(1 > 2, "违背结果为真的原则");

        Assert.IsTrue(Testbool());

        Assert.IsFalse(1 < 2, "违背结果为假的原则");
    }

    public bool Testbool()
    {
        return false;
    }
img_799188691fc49be8a0d4572ef71ec796.png
检测是否为Null
    public void TestIsNull()
    {
        Student student1 = new Student();

        Assert.IsNull(student1,"检测结果值不为null");

        student1 = null;

        Assert.IsNotNull<Student>(student1,"检测结果为null");
    }
img_d6fdd4308b47d9b5feca7f5750a46ac6.png

在断言中也可以自定义比较规则,代码如下

public class Student
{
    public string name;
    public int number;
    public int age;
}
    public void TestCustomEqualityComparer()
    {
        Student S1 = new Student() { age = 1 };
        Student S2 = new Student() { age = 2 };
        Assert.AreEqual(S1, S2, "自定义比较结果为不相等", new CustomEqualityComparer<Student>((s1, s2) => { return s1.age == s2.age; }));
    }
比较的部分在于继承 IEqualityComparer 重新实现我们要比较的规则
public class CustomEqualityComparer<T> : IEqualityComparer<T>
{
    public  Func<T,T, bool> EqualsComparer;

    public CustomEqualityComparer(Func<T, T, bool> equalsComparer)
    {
        EqualsComparer = equalsComparer;
    }
    public bool Equals(T x, T y)
    {
        if ( EqualsComparer!=null)
        {
            return EqualsComparer(x, y);
        }
        else
        {
            return false;
        }
    }

    public int GetHashCode(T obj)
    {
        return obj.ToString().GetHashCode();
    }
}

断言中还有一个特性就是否阻止触发条件以后的代码块执行,使用对用的API为Assert.raiseExceptions,默认值为false

效果如下:

Assert.raiseExceptions = false;

    public void TestRaiseExceptions()
    {
        Assert.raiseExceptions = false;

        Assert.IsTrue(1 > 2);

        Assert.IsTrue(1 > 2, "自定义输出");

        Debug.LogError(new string('*', 20));

        Debug.Log("执行");
    }
img_a08bf09bffcc399b8cd47e48ffae5aa0.png

Assert.raiseExceptions = true;

    public void TestRaiseExceptions()
    {
        Assert.raiseExceptions = true;

        Assert.IsTrue(1 > 2);

        Assert.IsTrue(1 > 2, "自定义输出");

        Debug.LogError(new string('*', 20));

        Debug.Log("执行");
    }
img_93e5d60e38c138b9688add7511b31b8f.png

打完收工~

img_569463b7983f645f063dd7b1bef93e0c.png
相关文章
|
18天前
|
API
debug常见错误,出现debug时出现跑进为条件为false的if语句
debug常见错误,出现debug时出现跑进为条件为false的if语句
15 0
|
18天前
|
API Android开发 开发者
debug常见错误,出现debug时出现跑进为条件为false的if语句,DELETE_FAILED_INTERNAL_ERROR Error while In
debug常见错误,出现debug时出现跑进为条件为false的if语句,DELETE_FAILED_INTERNAL_ERROR Error while In
19 5
|
4月前
|
C#
C#调试与测试 | Assert(断言)
什么是Assert呢? 断言是一种用于在程序运行时检查条件是否满足的工具。如果条件不满足,断言就会抛出一个异常,从而帮助我们快速定位问题并进行调试。 在C#中,可以使用Debug.Assert方法来实现断言,该方法接受一个布尔表达式作为参数,如果该表达式的值为false,就会抛出一个AssertionFailedException异常。
68 0
|
8月前
|
测试技术
Assertion检查
Assertion检查
77 0
|
算法
写代码一天,debug一年?
写代码一天,debug一年?
171 0
写代码一天,debug一年?
C# Debug Trace调试类用法
    Debug和Trace都是调试类。     Debug类的方法只有DEBUG版中生效,而Trace的方法可以在DEBUG/RELEASE版本中生效。 一、Debug类 Debug类的控制台输出及断言Assert用法。
1685 0
|
程序员
重构——41引入断言(Introduce Assertion)
引入断言(Introduce Assertion):某一段代码需要对程序状态作出某种假设;以断言明确表现这种假设
1498 0