一些简单的Assert
Posted 大木水可
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一些简单的Assert相关的知识,希望对你有一定的参考价值。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using NUnit.Framework.Constraints; 7 using NUnit.Framework; 8 9 namespace TestAssertLearning 10 { 11 [TestFixture] 12 class AssertModuleTest 13 { 14 private FileInfo fi; 15 private FileStream fs; 16 private DefaultDefine dd; 17 public class dad : DefaultDefine 18 { 19 public int realID; 20 public dad() 21 { 22 realID = 100; 23 } 24 } 25 public dad ad; 26 [SetUp] 27 public void AssertTestSetup() 28 { 29 dd = new DefaultDefine(); 30 ad = new dad(); 31 fi = new FileInfo(@"D:\a.txt"); 32 if (fs != null) 33 fs.Close(); 34 fs = new FileStream(@"D:\b.txt", FileMode.Create); 35 } 36 37 [Test]//同等断言 38 public void EqualTest() 39 { 40 Assert.That(5093, Is.EqualTo(5000 + 93)); 41 Assert.That(2 + 3, Is.Not.EqualTo(2)); 42 Assert.That(ad.ID, Is.EqualTo(dd.ID)); 43 } 44 45 [Test]//条件测试 46 public void ConditionTest() 47 { 48 Assert.That(double.NaN, Is.NaN); 49 Assert.That(3 > 2, Is.True); 50 Assert.That(3 == 3); 51 Assert.That(ad.NAME == dd.NAME); 52 Assert.That("", Is.Empty); 53 } 54 55 [Test]//一致性断言 56 public void SameAsTest() 57 { 58 Assert.That(dd, Is.Not.SameAs(ad)); 59 Assert.That(dd, Is.SameAs(dd)); 60 } 61 62 [Test]//比较断言 63 public void ComparisonTest() 64 { 65 Assert.That(100, Is.GreaterThan(3)); 66 Assert.That(3, Is.LessThanOrEqualTo(3)); 67 Assert.That(4, Is.AtLeast(3)); 68 } 69 70 [Test]//类型断言 71 public void TypeTest() 72 { 73 Assert.That("5", Is.TypeOf(typeof(string))); 74 Assert.That(dd, Is.InstanceOf(typeof(DefaultDefine))); 75 Assert.That(ad, Is.AssignableFrom(typeof(dad))); 76 } 77 78 [Test]//字符串断言 79 public void StringTest() 80 { 81 var bb = "Hello World"; 82 Assert.That(bb, Text.StartsWith("h").IgnoreCase); 83 Assert.That(bb, Text.DoesNotStartWith("h")); 84 Assert.That(bb, Text.DoesNotContain("a")); 85 Assert.That(bb, Text.Matches("Hello*")); 86 } 87 88 [Test]//文件断言 89 public void FileTest() 90 { 91 Assert.That(fi, Is.Not.EqualTo(fs)); 92 } 93 94 [Test]//集合断言 95 public void CollectionsTest() 96 { 97 object[] ints = new object[] { 1, 2, 3, 4 }; 98 object[] doubles = new object[] { 1.44, 2.3, 4.4, 2.05 }; 99 object[] strings = new object[] { "absc", "asdaf", " ssdd", "fess" }; 100 101 Assert.That(ints, Has.None.Null); 102 Assert.That(ints, Is.All.Not.Null); 103 Assert.That(ints, Is.All.AssignableFrom(typeof(int))); 104 Assert.That(strings, Is.All.AssignableFrom(typeof(string))); 105 //元素保持唯一性,互不相同 106 Assert.That(ints, Is.Unique); 107 Assert.That(ints, Is.Not.All.GreaterThan(3)); 108 Assert.That(strings, Has.Some.StartsWith("a")); 109 Assert.That(strings, Has.Some.StartsWith(" ")); 110 Assert.That(doubles, Is.All.InstanceOf(typeof(double))); 111 Assert.That(strings, Has.Some.EndsWith("s")); 112 Assert.That(strings, Is.All.Contains("s")); 113 } 114 115 [Test]//复合断言 116 public void CompoundTest() 117 { 118 Assert.That(2 + 2, Is.EqualTo(4) & Is.GreaterThan(2)); 119 Assert.That(2, Is.GreaterThan(6) | Is.LessThanOrEqualTo(3)); 120 } 121 122 [Test]//属性断言 123 public void PropertyTest() 124 { 125 dad[] ss = new dad[4] { new dad(), new dad(), new dad(), new dad() }; 126 object[] oo = new object[3] { 3, "2", ss }; 127 Assert.That(ss, Has.Some.Property("NAME")); 128 Assert.That(oo, Is.EqualTo(oo)); 129 Assert.That(oo, Has.Some.Property("Length")); 130 Assert.That(ss, Is.All.Property("ID")); 131 Assert.That(oo, Has.Some.Equals(3)); 132 } 133 134 [TearDown] 135 public void TearDown() 136 { 137 dd = null; 138 fs.Close(); 139 ad = null; 140 } 141 } 142 }
以上是关于一些简单的Assert的主要内容,如果未能解决你的问题,请参考以下文章