.NET单元测试学习(三)

接上一篇:

    7、Collection Constraint
     集合结束为指定的集合执行测试,主要的语法有:
     Is.All...,Has.All...  为集合中的每一个元素申请一个约束,所有元素符合要求则测试成功。
     Has.Some...   为集合中的每一个元素申请约束,最少有一个符合要求则测试成功。
     Has.None...    所有的元素都不都不符合要求则测试成功。
     Is.Unique       测试集合拥有一个唯一值
     Has.Member(object)    测试集合拥有一个对象
     Is.EquivalentTo(IEnumerable)  测试两个集合相等,需要拥有相同的items.
     Is.SubsetOf(IEnumerable)       测试一个集合是否是另一个的子集
          int[] iarray = new int[] { 1, 2, 3 };
          string[] sarray = new string[] { "a", "b", "c" };
          Assert.That( iarray, Is.All.Not.Null );
          Assert.That( sarray, Is.All.InstanceOfType(typeof(string)) );
          Assert.That( iarray, Is.All.GreaterThan(0) );
          Assert.That( sarray, Is.Unique );
          Assert.That( iarray, List.Contains(3) );
          Assert.That( sarray, List.Contains("b") );
          Assert.That( sarray, List.Not.Contains("x") );
          Assert.That( new string[] { "c", "a", "b" }, Is.EquivalentTo( sarray ) );
          Assert.That( new int[] { 1, 2, 2 }, Is.Not.EquivalentTo( iarray ) );
          Assert.That( new int[] { 1, 3 }, Is.SubsetOf( iarray ) );
          // Using inheritance
          Expect( iarray, All.GreaterThan( 0 ) );
          Expect( sarray, Contains("b") );
          Expect( new int[] { 1, 3 }, SubsetOf( iarray ) );

     8、Property Constraint
     测试对象的属性。Has.Property(string)   //测试指定对象是否存在
               Has.Property(string,object)   //测试对象属性值是否与提供的值相等
               Has.Lenght(int)   // 测试对象的长度是否与指定的值相等
               Has.Count(int)     //测试对象的总数是否与指定的值相等
           ArrayList arrayList1 = new ArrayList();
            arrayList1.Add("ccnu");
            arrayList1.Add("whu");
            arrayList1.Add("hsut");
            int[] array = new int[8] {0,1,2,3,4,5,6,7 };
            NUnit.Framework.Assert.That(array,Has.Property ("Length"));
            NUnit.Framework.Assert.That(array,Has.Property ("Length").EqualTo(8));
            NUnit.Framework.Assert.That(array,Has.Length (8));
            //Array has not Property Count
            //NUnit.Framework.Assert.That(array,Has.Count (8)); // Fail
            NUnit.Framework.Assert.That(arrayList1,Has .Property ("Count"));
            NUnit.Framework.Assert.That(arrayList1,Has.Count(3));

转载于:https://www.cnblogs.com/ision/archive/2008/10/23/1318141.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值