Create a simple unit test project with Visual Studio Unit Testing Framework

/*by Jiangong SUN*/


Unit test is used widely used in projects, it facilitates a lot the work of developers. Test Driven Development is a very popular development method based on unit testing.

There are some popular unit testing tools like xUnit (based on NUnit) etc. Visual studio has its own unit test "Microsoft.VisualStudio.TestTools.UnitTesting.Web"


Here I will create a simple unit test project.


Firstly, create a class library "BankSystem" with a class "IncomeMoney". Then, create a test project "UnitTest" with a test class "BankSystemTest".
Add a reference of BankSystem in project UnitTest.


Here is the structure of solution:



Create a property and a method in class "IncomeMoney" 


namespace BankSystem
{
    public class IncomeMoney
    {
        public decimal currentVolume
        {
            get;
            set;
        }

        public bool IsAcceptable(decimal m)
        {
            if (m < 1000)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

Create test method for testing if it's acceptable in the bank system

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace BankSystem.UnitTest
{
    [TestClass]
    public class BankSystemTest
    {
        [TestMethod]
        public void TestInputMoney()
        {
            IncomeMoney im = new IncomeMoney();
            im.currentVolume = 1001;
            im.IsAcceptable(im.currentVolume);
            Assert.IsTrue(im.IsAcceptable(im.currentVolume));
        }
    }
}

Because current volume is "1001", it's larger than "1000". The test is failed.



You can download the code here: http://download.csdn.net/detail/garcon1986/4869222



Hope this helps. Enjoy coding ! 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值