C#创建单元测试

1 新建C#库项目,修改代码

using System;

namespace MyLib
{
    public class Caculator
    {
        public double Add(double a,double b)
        {
            return a * b;
        }
    }
}

2 新建一个测试项目,选择xUnit测试项目

C#创建单元测试

 3 修改测试代码

using MyLib;
using System;
using Xunit;

namespace XUnitTestProject1
{
    public class UnitTest1
    {
        [Fact]
        public void TestAdding2And2()
        {
            double a = 2;
            double b = 2;
            double expected = 4;
            var calc = new Caculator();
            double actual = calc.Add(a, b);
            Assert.Equal(expected, actual);
        }

        [Fact]
        public void TestAdding2And3()
        {
            double a = 2;
            double b = 3;
            double expected = 5;
            var calc = new Caculator();
            double actual = calc.Add(a, b);
            Assert.Equal(expected, actual);
        }
    }
}

4 选择测试,运行所有测试 

C#创建单元测试

5 既可以看到所有的测试结果

C#创建单元测试 

 

上一篇:error: expected ‘;‘ at end of member declaration 和 expected ‘)‘ before ‘&‘ toke等报错问题


下一篇:编译报错 error: expected ‘;’, ‘,’ or ‘)’ before ‘switch’