当前位置:编程问答 > C#/ASP.NET >

Create a simple unit test project

/*by garcon1986*/

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.



Hope this helps. Enjoy coding !

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,