How to use Derived class of NUnit.Framework.Constraints package

Best Nunit code snippet using NUnit.Framework.Constraints.Derived

Run Nunit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

CustomConstraintExample.cs

Source: CustomConstraintExample.cs Github

copy
1// Copyright (c) 2021 Koji Hasegawa.
2// This software is released under the MIT License.
3
4using NUnit.Framework;
5using NUnit.Framework.Constraints;
6
7// ReSharper disable AccessToStaticMemberViaDerivedType
8
9namespace APIExamples.NUnit
10{
11    /// <summary>
12    /// カスタム<see cref="Constraint"/>の使用例
13    /// </summary>
14    [TestFixture]
15    public class CustomConstraintExample
16    {
17        [Test]
18        public void CustomEqConstraint_Constraintの実装だけで可能な書きかた()
19        {
20            var actual = "Foo bar";
21
22            Assert.That(actual, new CustomEqConstraint("Foo bar"));
23            // 失敗時メッセージ例:
24            //  Expected: "Foo bar"(カスタム制約)
25            //  But was:  "Foo bar baz"
26        }
27
28        [Test]
29        public void CustomEqConstraint_Extensionsの実装も行なうと可能な書きかた()
30        {
31            var actual = "Foo bar";
32
33            Assert.That(actual, Is.Not.Null.And.CustomEq("Foo bar"));
34            // 失敗時メッセージ例:
35            //  Expected: not null and "Foo bar"(カスタム制約)
36            //  But was:  "Foo bar baz"
37        }
38
39        [Test]
40        public void CustomEqConstraint_Isの実装も行なうと可能な書きかた()
41        {
42            var actual = "Foo bar";
43
44            Assert.That(actual, Is.CustomEq("Foo bar"));
45            // 失敗時メッセージ例:
46            //  Expected: "Foo bar"(カスタム制約)
47            //  But was:  "Foo bar baz"
48        }
49    }
50
51    /// <summary>
52    /// カスタム<see cref="Constraint"/>の実装例
53    /// </summary>
54    /// <remarks>
55    /// 内容は文字列が一致するか判定しているだけ
56    /// </remarks>
57    public class CustomEqConstraint : Constraint
58    {
59        public CustomEqConstraint(params object[] args) : base(args) { }
60
61        public override ConstraintResult ApplyTo(object actual)
62        {
63            return new ConstraintResult(this, actual, actual.ToString() == Arguments[0].ToString());
64        }
65
66        public override string Description { get { return $"\"{Arguments[0]}\"(カスタム制約)"; } }
67    }
68
69    /// <summary>
70    /// カスタム<see cref="Constraint"/>向けの<see cref="ConstraintExpression"/>拡張メソッド
71    /// </summary>
72    public static class CustomConstraintExtensions
73    {
74        public static CustomEqConstraint CustomEq(this ConstraintExpression expression, object expected)
75        {
76            var constraint = new CustomEqConstraint(expected);
77            expression.Append(constraint);
78            return constraint;
79        }
80    }
81
82    /// <summary>
83    /// カスタム<see cref="Constraint"/>向けの<see cref="Is"/>クラス
84    /// </summary>
85    /// <remarks>
86    /// Unity Test Framework には NUnit.Framework.Is を継承した UnityEngine.TestTools.Constraints.Is がすでにあるため、
87    /// これを継承して作ります。
88    /// UnityEngine.TestTools.Constraints.Is が必要なければ、直接 global::NUnit.Framework.Is を継承しても構いません。
89    /// </remarks>
90    // ReSharper disable once ClassNeverInstantiated.Global
91    public class Is : UnityEngine.TestTools.Constraints.Is
92    {
93        public static CustomEqConstraint CustomEq(object expected)
94        {
95            return new CustomEqConstraint(expected);
96        }
97    }
98}
99
Full Screen

AllocatingGCMemoryConstraint.cs

Source: AllocatingGCMemoryConstraint.cs Github

copy
1using System;
2using NUnit.Framework;
3using NUnit.Framework.Constraints;
4using UnityEngine.Profiling;
5
6namespace UnityEngine.TestTools.Constraints
7{
8    /// <summary>
9    /// An NUnit test constraint class to test whether a given block of code makes any GC allocations.
10    /// 
11    /// Use this class with NUnit's Assert.That() method to make assertions about the GC behaviour of your code. The constraint executes the delegate you provide, and checks if it has caused any GC memory to be allocated. If any GC memory was allocated, the constraint passes; otherwise, the constraint fails.
12    ///
13    /// Usually you negate this constraint to make sure that your delegate does not allocate any GC memory. This is easy to do using the Is class:
14    /// </summary>
15    /// <example>
16    /// <code>
17    /// using NUnit.Framework;
18    /// using UnityEngine.TestTools.Constraints;
19    /// using Is = UnityEngine.TestTools.Constraints.Is;
20    /// 
21    /// public class MyTestClass
22    /// {
23    ///     [Test]
24    ///     public void SettingAVariableDoesNotAllocate()
25    ///     {
26    ///         Assert.That(() => {
27    ///             int a = 0;
28    ///             a = 1;
29    ///         }, Is.Not.AllocatingGCMemory());
30    ///     }
31    /// }
32    /// </code>
33    /// </example>
34    public class AllocatingGCMemoryConstraint : Constraint
35    {
36        private class AllocatingGCMemoryResult : ConstraintResult
37        {
38            private readonly int diff;
39            public AllocatingGCMemoryResult(IConstraint constraint, object actualValue, int diff) : base(constraint, actualValue, diff > 0)
40            {
41                this.diff = diff;
42            }
43
44            public override void WriteMessageTo(MessageWriter writer)
45            {
46                if (diff == 0)
47                    writer.WriteMessageLine("The provided delegate did not make any GC allocations.");
48                else
49                    writer.WriteMessageLine("The provided delegate made {0} GC allocation(s).", diff);
50            }
51        }
52
53        private ConstraintResult ApplyTo(Action action, object original)
54        {
55            var recorder = Recorder.Get("GC.Alloc");
56
57            // The recorder was created enabled, which means it captured the creation of the Recorder object itself, etc.
58            // Disabling it flushes its data, so that we can retrieve the sample block count and have it correctly account
59            // for these initial allocations.
60            recorder.enabled = false;
61
62#if !UNITY_WEBGL
63            recorder.FilterToCurrentThread();
64#endif
65
66            recorder.enabled = true;
67
68            try
69            {
70                action();
71            }
72            finally
73            {
74                recorder.enabled = false;
75#if !UNITY_WEBGL
76                recorder.CollectFromAllThreads();
77#endif
78            }
79
80            return new AllocatingGCMemoryResult(this, original, recorder.sampleBlockCount);
81        }
82
83        /// <summary>
84        /// Applies GC memory constraint to the test.
85        /// </summary>
86        /// <param name="obj">An object to apply the GC constraint to. Should be a <see cref="TestDelegate"/>.</param>
87        /// <returns>A ConstraintResult</returns>
88        /// <exception cref="ArgumentNullException">Throws a <see cref="ArgumentNullException"/> if the provided object is null.</exception>
89        /// <exception cref="ArgumentException">Throws a <see cref="ArgumentException"/> if the provided object is not a <see cref="TestDelegate"/>.</exception>
90        public override ConstraintResult ApplyTo(object obj)
91        {
92            if (obj == null)
93                throw new ArgumentNullException();
94
95            TestDelegate d = obj as TestDelegate;
96            if (d == null)
97                throw new ArgumentException(string.Format("The actual value must be a TestDelegate but was {0}",
98                    obj.GetType()));
99
100            return ApplyTo(() => d.Invoke(), obj);
101        }
102
103        /// <summary>
104        /// Test whether the constraint is satisfied by a given reference.
105        /// The default implementation simply dereferences the value but
106        /// derived classes may override it to provide for delayed processing.
107        /// </summary>
108        /// <typeparam name="TActual">The type of the actual value delegate to be tested.</typeparam>
109        /// <param name="del">A reference to the value delegate to be tested</param>
110        /// <returns>A ConstraintResult</returns>
111        /// <exception cref="ArgumentNullException">Throws a <see cref="ArgumentNullException"/> if the provided delegate is null.</exception>
112        public override ConstraintResult ApplyTo<TActual>(ActualValueDelegate<TActual> del)
113        {
114            if (del == null)
115                throw new ArgumentNullException();
116
117            return ApplyTo(() => del.Invoke(), del);
118        }
119
120        /// <summary>
121        /// The Description of what this constraint tests, for to use in messages and in the ConstraintResult.
122        /// </summary>
123        public override string Description
124        {
125            get { return "allocates GC memory"; }
126        }
127    }
128}
129
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)