zoukankan      html  css  js  c++  java
  • 使用xUnit.NET 对.NET Core 项目进行单元测试

    (1)三个A : Arrange  ------> Act   ----->Assert

    (2)1、新建项目,选择xUnit测试项目

        2、代码写好后、测试---》测试窗口

      (3) 1、布尔型:Assert.True(result)  ----结果预期为True 

      2、字符串:Assert.Equal("Nick Carter",result);  ----是否相等

             Assert.StartWith("Nick",result);  ----是否以**开头

             Assert.EndsWith("Caeter",result);----是否以**结尾

             Assert.Contains("ck",result);-----是否包含

                Assert.Matches("[A-Z]{1}{a-z}+[A-Z]{1}[a-z]+",result); -----正则表达式验证

             Assert.Null(result); ----是否为空

             Assert.NotNull(result); 

      3、浮点型:Assert.Equal(expected,actual,precision);-----是否相等,最后的参数是精度

             Assert.InRange(actual,low,high)----是否在某个范围

      3、集合:Assert.Contains(expected,list); ----集合是否包含某个项

            Assert.DoesNotContain(expected,list)

          Assert.Contains(p.History,x=>x.StartWith("水")); ----集合内至少一个元素以"水"开头

          Assert.All(p.History,x=>Assert.True(x.Length >=2 )); ----集合内所有元素大于等于2

          Assert.Equal(expected,actual);

      4、其它类型:

          Assert.IsType<Person>(p);-------p是否为Person类

          Assert.IsNotType<Person>(p);

          Assert.IsAssignableFrom<Person>(p); ------p是否继承与Person类

          Assert.Same(p,p2); ----是否是同一个实例

          Assert.NotSame(p,p2);

      5、异常

          Assert.Throws<Exception>(()=>p.NotAllowed());-----判断是否跑出Exception异常

          var ex = Assert.Throws<Exception>(()=>p.NotAllowed());

          Assert.Equal("Not able to create",ex.Message); ------判断异常信息是否为’Not able to create’

      6、事件

          Assert.Raises<EventArgs>(

            handler =>p.PatientSlept += handler,

            handler =>p.PatientSlept -= handler,

             =>p.Sleep()); ------是否触发事件

    (4)测试分组: [Trait("Name","Value")]

      可作用于方法名上,也可作用于类名上

    (5)忽略测试:[Fact(Skip = "不需要跑这个测试")]

    (6)自定义输出测试信息:

      private readonly ITestOutputHelper _output;

      public PatientShould(ItestOutputHelper output)

      {

        _output=output;

      }

      [Fact]

      public void BeNewWhenCreated()

      {

        _output.WriteLine("第一个测试");

        ……

      }

    (7)数据来源:

      1、[InlineData(x,y,……)]

        [Theory]

        [InlineData(1,2,3)]

        [InlineData(2,3,5)]

        public void ShouldAdd(int x,int y,int expected)

        {

          var sut = new Calcu();

          var result = syt.Add(x,y);

          Assert.Equal(expected,result);

        }

      2、[MemberData(nameof(xxx),MemberType = typeof(xxx))]

  • 相关阅读:
    std thread
    windows更新包发布地址
    How to set up logging level for Spark application in IntelliJ IDEA?
    spark 错误 How to set heap size in spark within the Eclipse environment?
    hadoop 常用命令
    windows 安装hadoop 3.2.1
    windows JAVA_HOME 路径有空格,执行软连接
    day01MyBatisPlus条件构造器(04)
    day01MyBatisPlus的CRUD 接口(03)
    day01MyBatisPlus入门(02)
  • 原文地址:https://www.cnblogs.com/zuiailiuruoying/p/14458648.html
Copyright © 2011-2022 走看看