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

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

Run Nunit automation tests on LambdaTest cloud grid

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

TypeConstraintTests.cs

Source: TypeConstraintTests.cs Github

copy
1// ****************************************************************
2// Copyright 2007, Charlie Poole
3// This is free software licensed under the NUnit license. You may
4// obtain a copy of the license at http://nunit.org.
5// ****************************************************************
6
7namespace NUnit.Framework.Constraints.Tests
8{
9    [TestFixture]
10    public class ExactTypeTest : ConstraintTestBase
11    {
12        [SetUp]
13        public void SetUp()
14        {
15            theConstraint = new ExactTypeConstraint(typeof(D1));
16            expectedDescription = string.Format("<{0}>", typeof(D1));
17            stringRepresentation = string.Format("<typeof {0}>", typeof(D1));
18        }
19
20        object[] SuccessData = new object[] { new D1() };
21        
22        object[] FailureData = new object[] { new B(), new D2() };
23
24        string[] ActualValues = new string[]
25            {
26                "<NUnit.Framework.Constraints.Tests.B>",
27                "<NUnit.Framework.Constraints.Tests.D2>"
28            };
29    }
30
31    [TestFixture]
32    public class InstanceOfTypeTest : ConstraintTestBase
33    {
34        [SetUp]
35        public void SetUp()
36        {
37            theConstraint = new InstanceOfTypeConstraint(typeof(D1));
38            expectedDescription = string.Format("instance of <{0}>", typeof(D1));
39            stringRepresentation = string.Format("<instanceof {0}>", typeof(D1));
40        }
41
42        object[] SuccessData = new object[] { new D1(), new D2() };
43
44        object[] FailureData = new object[] { new B() };
45
46        string[] ActualValues = new string[]
47            {
48                "<NUnit.Framework.Constraints.Tests.B>"
49            };
50    }
51
52    [TestFixture]
53    public class AssignableFromTest : ConstraintTestBase
54    {
55        [SetUp]
56        public void SetUp()
57        {
58            theConstraint = new AssignableFromConstraint(typeof(D1));
59            expectedDescription = string.Format("assignable from <{0}>", typeof(D1));
60            stringRepresentation = string.Format("<assignablefrom {0}>", typeof(D1));
61        }
62
63        object[] SuccessData = new object[] { new D1(), new B() };
64            
65        object[] FailureData = new object[] { new D2() };
66
67        string[] ActualValues = new string[]
68            {
69                "<NUnit.Framework.Constraints.Tests.D2>"
70            };
71    }
72
73    [TestFixture]
74    public class AssignableToTest : ConstraintTestBase
75    {
76        [SetUp]
77        public void SetUp()
78        {
79            theConstraint = new AssignableToConstraint(typeof(D1));
80            expectedDescription = string.Format("assignable to <{0}>", typeof(D1));
81            stringRepresentation = string.Format("<assignableto {0}>", typeof(D1));
82        }
83        
84        object[] SuccessData = new object[] { new D1(), new D2() };
85            
86        object[] FailureData = new object[] { new B() };
87
88        string[] ActualValues = new string[]
89            {
90                "<NUnit.Framework.Constraints.Tests.B>"
91            };
92    }
93
94    class B { }
95
96    class D1 : B { }
97
98    class D2 : D1 { }
99
100    [TestFixture]
101    public class AttributeExistsConstraintTest : ConstraintTestBase
102    {
103        [SetUp]
104        public void SetUp()
105        {
106            theConstraint = new AttributeExistsConstraint(typeof(TestFixtureAttribute));
107            expectedDescription = "type with attribute <NUnit.Framework.TestFixtureAttribute>";
108            stringRepresentation = "<attributeexists NUnit.Framework.TestFixtureAttribute>";
109        }
110
111        object[] SuccessData = new object[] { typeof(AttributeExistsConstraintTest) };
112            
113        object[] FailureData = new object[] { typeof(D2) };
114
115        string[] ActualValues = new string[]
116            {
117                "<NUnit.Framework.Constraints.Tests.D2>"
118            };
119
120        [Test, ExpectedException(typeof(System.ArgumentException))]
121        public void NonAttributeThrowsException()
122        {
123            new AttributeExistsConstraint(typeof(string));
124        }
125
126        [Test]
127        public void AttributeExistsOnMethodInfo()
128        {
129            Assert.That(
130                System.Reflection.MethodInfo.GetCurrentMethod(),
131                new AttributeExistsConstraint(typeof(TestAttribute)));
132        }
133
134        [Test, Description("my description")]
135        public void AttributeTestPropertyValueOnMethodInfo()
136        {
137            Assert.That(
138                System.Reflection.MethodInfo.GetCurrentMethod(),
139                Has.Attribute(typeof(DescriptionAttribute)).Property("Description").EqualTo("my description"));
140        }
141    }
142}
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 D2

    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)