How to use ComplexNumber class of NUnit.TestData.AssertMultipleData package

Best Nunit code snippet using NUnit.TestData.AssertMultipleData.ComplexNumber

Run Nunit automation tests on LambdaTest cloud grid

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

AssertMultipleTests.cs

Source: AssertMultipleTests.cs Github

copy
1// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2
3using System;
4using NUnit.Framework.Interfaces;
5using NUnit.TestData.AssertMultipleData;
6using NUnit.TestUtilities;
7using AM = NUnit.TestData.AssertMultipleData.AssertMultipleFixture;
8
9namespace NUnit.Framework.Assertions
10{
11    public class AssertMultipleTests
12    {
13        private static readonly ComplexNumber _complex = new ComplexNumber(5.2, 3.9);
14
15        [TestCase(nameof(AM.EmptyBlock), 0)]
16        [TestCase(nameof(AM.SingleAssertSucceeds), 1)]
17        [TestCase(nameof(AM.TwoAssertsSucceed), 2)]
18        [TestCase(nameof(AM.ThreeAssertsSucceed), 3)]
19        [TestCase(nameof(AM.NestedBlock_ThreeAssertsSucceed), 3)]
20        [TestCase(nameof(AM.TwoNestedBlocks_ThreeAssertsSucceed), 3)]
21        [TestCase(nameof(AM.NestedBlocksInMethodCalls), 3)]
22        [TestCase(nameof(AM.ThreeWarnIf_AllPass), 3)]
23        [TestCase(nameof(AM.ThreeWarnUnless_AllPass), 3)]
24        [TestCase(nameof(AM.ThreeAssertsSucceed_Async), 3)]
25        [TestCase(nameof(AM.NestedBlock_ThreeAssertsSucceed_Async), 3)]
26        [TestCase(nameof(AM.TwoNestedBlocks_ThreeAssertsSucceed_Async), 3)]
27        public void AssertMultipleSucceeds(string methodName, int asserts)
28        {
29            CheckResult(methodName, ResultState.Success, asserts);
30        }
31
32        [TestCase(nameof(AM.TwoAsserts_FirstAssertFails), 2, "RealPart")]
33        [TestCase(nameof(AM.TwoAsserts_SecondAssertFails), 2, "ImaginaryPart")]
34        [TestCase(nameof(AM.TwoAsserts_BothAssertsFail), 2, "RealPart", "ImaginaryPart")]
35        [TestCase(nameof(AM.NestedBlock_FirstAssertFails), 3, "Expected: 5")]
36        [TestCase(nameof(AM.NestedBlock_TwoAssertsFail), 3, "Expected: 5", "ImaginaryPart")]
37        [TestCase(nameof(AM.TwoNestedBlocks_FirstAssertFails), 3, "Expected: 5")]
38        [TestCase(nameof(AM.TwoNestedBlocks_TwoAssertsFail), 3, "Expected: 5", "ImaginaryPart")]
39        [TestCase(nameof(AM.MethodCallsFail), 0, "Message from Assert.Fail")]
40        [TestCase(nameof(AM.MethodCallsFailAfterTwoAssertsFail), 2, "Expected: 5", "ImaginaryPart", "Message from Assert.Fail")]
41        [TestCase(nameof(AM.TwoAssertsFailAfterWarning), 2, "WARNING", "Expected: 5", "ImaginaryPart")]
42        [TestCase(nameof(AM.WarningAfterTwoAssertsFail), 2, "Expected: 5", "ImaginaryPart", "WARNING")]
43        [TestCase(nameof(AM.TwoAsserts_BothAssertsFail_Async), 2, "RealPart", "ImaginaryPart")]
44        [TestCase(nameof(AM.TwoNestedBlocks_TwoAssertsFail_Async), 3, "Expected: 5", "ImaginaryPart")]
45        public void AssertMultipleFails(string methodName, int asserts, params string[] assertionMessages)
46        {
47            CheckResult(methodName, ResultState.Failure, asserts, assertionMessages);
48        }
49
50        [TestCase(nameof(AM.ThreeAssertWarns), 0, "WARNING1", "WARNING2", "WARNING3")]
51        [TestCase(nameof(AM.ThreeWarnIf_TwoFail), 3, "WARNING1", "WARNING3")]
52        [TestCase(nameof(AM.ThreeWarnUnless_TwoFail), 3, "WARNING1", "WARNING3")]
53        public void AssertMultipleWarns(string methodName, int asserts, params string[] assertionMessages)
54        {
55            CheckResult(methodName, ResultState.Warning, asserts, assertionMessages);
56        }
57
58        [TestCase(nameof(AM.ExceptionThrown), 0)]
59        [TestCase(nameof(AM.ExceptionThrownAfterTwoFailures), 2, "Failure 1", "Failure 2", "Simulated Error")]
60        [TestCase(nameof(AM.ExceptionThrownAfterWarning), 0, "WARNING", "Simulated Error")]
61        public void AssertMultipleErrorTests(string methodName, int asserts, params string[] assertionMessages)
62        {
63            ITestResult result = CheckResult(methodName, ResultState.Error, asserts, assertionMessages);
64            Assert.That(result.Message, Does.StartWith("System.Exception : Simulated Error"));//
65        }
66
67        [TestCase(nameof(AM.AssertPassInBlock), "Assert.Pass")]
68        [TestCase(nameof(AM.AssertIgnoreInBlock), "Assert.Ignore")]
69        [TestCase(nameof(AM.AssertInconclusiveInBlock), "Assert.Inconclusive")]
70        [TestCase(nameof(AM.AssumptionInBlock), "Assume.That")]
71        public void AssertMultiple_InvalidAssertThrowsException(string methodName, string invalidAssert)
72        {
73            ITestResult result = CheckResult(methodName, ResultState.Error, 0);
74            Assert.That(result.Message, Contains.Substring($"{invalidAssert} may not be used in a multiple assertion block."));
75        }
76
77        private ITestResult CheckResult(string methodName, ResultState expectedResultState, int expectedAsserts, params string[] assertionMessageRegex)
78        {
79            ITestResult result = TestBuilder.RunTestCase(typeof(AssertMultipleFixture), methodName);
80
81            Assert.That(result.ResultState, Is.EqualTo(expectedResultState), "ResultState");
82            Assert.That(result.AssertCount, Is.EqualTo(expectedAsserts), "AssertCount");
83            Assert.That(result.AssertionResults.Count, Is.EqualTo(assertionMessageRegex.Length), "Number of AssertionResults");
84
85            PlatformInconsistency.MonoMethodInfoInvokeLosesStackTrace.SkipOnAffectedPlatform(() =>
86            {
87                if (result.ResultState.Status == TestStatus.Failed)
88                    Assert.That(result.StackTrace, Is.Not.Null.And.Contains(methodName), "StackTrace");
89            });
90
91            if (result.AssertionResults.Count > 0)
92            {
93                int numFailures = result.AssertionResults.Count;
94                if (expectedResultState == ResultState.Error)
95                    --numFailures;
96
97                if (numFailures > 1)
98                    Assert.That(result.Message, Contains.Substring("Multiple failures or warnings in test:"));
99
100                int i = 0;
101                foreach (var assertion in result.AssertionResults)
102                {
103                    // Since the order of argument evaluation is not guaranteed, we don't
104                    // want 'i' to appear more than once in the Assert statement.
105                    string errmsg = string.Format("AssertionResult {0}", i + 1);
106                    Assert.That(assertion.Message, Does.Match(assertionMessageRegex[i++]), errmsg);
107                    Assert.That(result.Message, Contains.Substring(assertion.Message), errmsg);
108
109                    // NOTE: This test expects the stack trace to contain the name of the method 
110                    // that actually caused the failure. To ensure it is not optimized away, we
111                    // compile the testdata assembly with optimizations disabled.
112
113                    PlatformInconsistency.MonoMethodInfoInvokeLosesStackTrace.SkipOnAffectedPlatform(() =>
114                    {
115                        Assert.That(assertion.StackTrace, Is.Not.Null.And.Contains(methodName), errmsg);
116                    });
117                }
118            }
119
120            return result;
121        }
122    }
123
124    [Explicit("Used to display error messages for visual confirmation")]
125    public class MultipleAssertDemo
126    {
127        private static readonly ComplexNumber _complex = new ComplexNumber(5.2, 3.9);
128
129        [Test]
130        // Shows multiple failures including one from Assert.Fail
131        public void MultipleAssertFailureDemo()
132        {
133            Assert.Multiple(() =>
134            {
135                Assert.That(_complex.RealPart, Is.EqualTo(5.0), "RealPart");
136                Assert.That(_complex.ImaginaryPart, Is.EqualTo(4.2), "ImaginaryPart");
137                Assert.Fail("Assert.Fail Called");
138            });
139        }
140
141        [Test]
142        // Shows two failures followed by an exception
143        public void MultipleAssertErrorDemo()
144        {
145            Assert.Multiple(() =>
146            {
147                Assert.That(_complex.RealPart, Is.EqualTo(5.0), "RealPart");
148                Assert.That(_complex.ImaginaryPart, Is.EqualTo(4.2), "ImaginaryPart");
149                throw new Exception("Simulated Error");
150            });
151        }
152    }
153
154    internal class ComplexNumber
155    {
156        public ComplexNumber(double realPart, double imaginaryPart)
157        {
158            RealPart = realPart;
159            ImaginaryPart = imaginaryPart;
160        }
161
162        public double RealPart;
163        public double ImaginaryPart;
164    }
165}
166
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

Most used methods in ComplexNumber

    No methods found for this class 😞

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)