• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
目标驱动者
目标........努力........生活.........
博客园    首页    新随笔    联系   管理    订阅  订阅

.NET单元测试学习(三)

NUnit学习

接上一篇:

    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));

posted @ 2008-10-23 17:43  IsionWu  阅读(827)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3