程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Create a simple unit test project

Create a simple unit test project

編輯:C#入門知識

/*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 !

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved