最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#使用MSTest進(jìn)行單元測(cè)試的示例代碼

 更新時(shí)間:2023年12月22日 09:28:31   作者:rjcql  
MSTest是微軟官方提供的.NET平臺(tái)下的單元測(cè)試框架,這篇文章主要為大家詳細(xì)介紹了C#如何使用MSTest進(jìn)行單元測(cè)試,感興趣的小伙伴可以參考一下

寫在前面

MSTest是微軟官方提供的.NET平臺(tái)下的單元測(cè)試框架;可使用DataRow屬性來(lái)指定數(shù)據(jù),驅(qū)動(dòng)測(cè)試用例所用到的值,連續(xù)對(duì)每個(gè)數(shù)據(jù)化進(jìn)行運(yùn)行測(cè)試,也可以使用DynamicData 屬性來(lái)指定數(shù)據(jù),驅(qū)動(dòng)測(cè)試用例所用數(shù)據(jù)的成員的名稱、種類(屬性、默認(rèn)值或方法)和定義類型(默認(rèn)情況下使用當(dāng)前類型)

代碼實(shí)現(xiàn)

新建目標(biāo)類DataChecker,增加待測(cè)試的方法,內(nèi)容如下:

    public class DataChecker
    {
 
        public bool IsPrime(int candidate)
        {
            if (candidate == 1)
            {
                return true;
            }
            return false;
        }
 
        public int AddInt(int first, int second)
        {
            int sum = first;
            for (int i = 0; i < second; i++)
            {
                sum += 1;
            }
            return sum;
        }
    }

新建單元測(cè)試類UnitTest1

namespace MSTestTester.Tests;
 
[TestClass]
public class UnitTest1
{
    private readonly DataChecker _dataChecker;
     
    public UnitTest1()
    {
        _dataChecker = new DataChecker();
    }
 
    [TestMethod]
    [DataRow(-1)]
    [DataRow(0)]
    [DataRow(1)]
    public void IsPrime_ValuesLessThan2_ReturnFalse(int value)
    {
        var result = _dataChecker.IsPrime(value);
 
        Assert.IsFalse(result, $"{value} should not be prime");
    }
 
    [DataTestMethod]
    [DataRow(1, 1, 2)]
    [DataRow(2, 2, 4)]
    [DataRow(3, 3, 6)]
    [DataRow(0, 0, 1)] // The test run with this row fails
    public void AddInt_DataRowTest(int x, int y, int expected)
    {
        int actual = _dataChecker.AddInt(x, y);
        Assert.AreEqual(expected, actual,"x:<{0}> y:<{1}>",new object[] { x, y });
    }
 
    public static IEnumerable<object[]> AdditionData
    {
        get
        {
            return new[]
            {
            new object[] { 1, 1, 2 },
            new object[] { 2, 2, 4 },
            new object[] { 3, 3, 6 },
            new object[] { 0, 0, 1 },
        };
        }
    }
 
    [TestMethod]
    [DynamicData(nameof(AdditionData))]
    public void AddIntegers_FromDynamicDataTest(int x, int y, int expected)
    {
        int actual = _dataChecker.AddInt(x, y);
        Assert.AreEqual(expected, actual, "x:<{0}> y:<{1}>", new object[] { x, y });
    }
}

執(zhí)行結(jié)果

打開命令行窗口執(zhí)行以下命令:

dotnet test

 符合預(yù)期結(jié)果

到此這篇關(guān)于C#使用MSTest進(jìn)行單元測(cè)試的示例代碼的文章就介紹到這了,更多相關(guān)C# MSTest單元測(cè)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

封丘县| 盖州市| 浠水县| 保靖县| 江川县| 海伦市| 阳信县| 武定县| 商南县| 云浮市| 三亚市| 西昌市| 遂溪县| 襄樊市| 孟州市| 读书| 宁河县| 福泉市| 松滋市| 宁陵县| 鹤壁市| 昌平区| 莱州市| 林周县| 辽阳市| 建始县| 新津县| 邯郸县| 天峨县| 黑山县| 浏阳市| 神木县| 安达市| 泰和县| 兰考县| 二手房| 垣曲县| 开封市| 浠水县| 武汉市| 道孚县|