软件工程第二次作业

开发工具

这里使用C#编写待测试功能,并在Microsoft Visual Studio 2017 Community(以下简称VS2017)环境下开发。

项目的建立

在VS2017中,创建一个C#类库项目(基于.NET Framework)如下:

之后再该类库项目下编写类库的功能实现。

测试样例

我编写了一个银行测试样例,包括名称 存款 利率
对 存钱 取钱以及计算利息操作进行测试

代码如下:

using System;

namespace ClassLibrary1
{
    public class Class1
    {
    }
}
namespace DebitAccount

{

    public class Account

    {

        public Account(string name,float save = 0.0f,float profitpct = 0.0f)

        {

            this.name = name;
            if(save < 0.0f)
            {
                throw new Exception();
            }
            if(profitpct < 0.0f)
            {
                throw new Exception();
            }
            this.save = save;
            this.profitpct = profitpct;
        }

        public void Deposit(float cash)
        {
            if(cash < 0.0f)
            {
                throw new Exception();
            }
            else
            {
                save += cash;
            }
        }

        public void Withdraw(float cash)
        {
            if(cash < 0.0f)
            {
                throw new Exception();
            }
            else if(save < cash)
            {
                throw new Exception();
            }
            else
            {
                save -= cash;
            }
        }

        public float CalcInterest(uint years,float intrTax)
        {
            if(intrTax > 1 || intrTax < 0)
            {
                throw new Exception();
            }
            return save * years * profitpct * (1 - intrTax);
        }

        public string name;
        public float save;
        public float profitpct;

    }   

}

单元测试

进行了对数据设置的测试,对存储功能测试以及对计算去税利息计算结果的测试

全部代码如下:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DebitAccount;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestConstruct()
        {
            Account a = new Account("David", 200.00f, 0.003f);
            Assert.IsNotNull(a);
            Assert.AreEqual(a.name, "David");
            Assert.AreEqual(a.save, 200.00f);
            Assert.AreEqual(a.profitpct, 0.003f);
            try
            {
                Account b = new Account("Sam", -500.00f, -4.00f);
            }
            catch(Exception ex)
            {
                Assert.IsNotNull(ex);
            }
        }
        [TestMethod]
        public void TestDeposit()
        {
            Account a = new Account("David", 200.00f, 0.003f);
            a.Deposit(300.50f);
            Assert.AreEqual(a.save, 500.50f);
            try
            {
                a.Deposit(-50.0f);
            }
            catch (Exception ex)
            {
                Assert.IsNotNull(ex);
            }

        }
        [TestMethod]
        public void TestWithdraw()
        {
            Account a = new Account("David", 200.00f, 0.003f);
            a.Withdraw(100.50f);
            Assert.AreEqual(a.save, 99.50f);
            try
            {
                a.Withdraw(-50.0f);
            }
            catch(Exception ex)
            {
                Assert.IsNotNull(ex);
            }
            try
            {
                a.Withdraw(200.0f);
            }
            catch (Exception ex)
            {
                Assert.IsNotNull(ex);
            }
        }
        [TestMethod]
        public void TestCalcInterest()
        {
            Account a = new Account("David", 200.00f, 0.003f);
            float interest = a.CalcInterest(2, 0.2f);
            Assert.AreEqual(interest, 0.96f);
            try
            {
                interest = a.CalcInterest(2, 1.5f);
            }
            catch (Exception ex)
            {
                Assert.IsNotNull(ex);
            }
        }
    }
}

测试结果如下

总结

通过这次作业我基本对单元测试有了基本的了解与掌握。对于VS的使用还是有许多不熟练以及不掌握的地方,仍需努力

posted @ 2019-04-13 15:50  cs937171599  阅读(120)  评论(0编辑  收藏  举报