Visual Studio: How to create Unit Test

By FoxLearn 6/25/2017 8:07:53 PM   3.26K
Creating your first unit test in C# using Visual Studio 2015

Check that your code is working as expected by creating and running unit tests. It’s called unit testing because you break down the functionality of your program into discrete testable behaviors that you can test as individual units

Create a BankAccount class to play demo as below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTestDemo
{
    public class BankAccount
    {
        string _customerName;
        double _balance;

        public BankAccount(string customerName, double balance)
        {
            _customerName = customerName;
            _balance = balance;
        }

        public double Balance { get { return _balance; } }

        public void Debit(double amount)
        {
            if (_balance == 0)
                throw new Exception("balance = 0");
            if (amount <= 0 || amount > _balance)
                throw new ArgumentOutOfRangeException("amount <= 0 or amount > balance");
            _balance -= amount;
        }

        public void Credit(double amount)
        {
            if (amount <= 0)
                throw new ArgumentOutOfRangeException("amount <= 0");
            _balance += amount;
        }
    }
}

You can quickly generate test projects and test methods from your code, or manually create the tests as you need them. When you use IntelliTest to explore your .NET code, you can generate test data and a suite of unit tests

using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnitTestDemo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTestDemo.Tests
{
    [TestClass()]
    public class BankAccountTests
    {
        [TestMethod()]
        public void TestExceptionCase()
        {
            BankAccount bank = new BankAccount("Lucy", 0);
            try
            {
                bank.Debit(2);
            }
            catch(ArgumentOutOfRangeException e)
            {
                StringAssert.Contains(e.Message, "amount <= 0 or amount > balance");
                return;
            }
            catch(Exception e)
            {
                StringAssert.Contains(e.Message, "balance = 0");
                return;
            }
            Assert.Fail("No exception was thrown");
        }

        [TestMethod()]
        public void TestCreditCase()
        {
            BankAccount bank = new BankAccount("Lucy", 2);
            bank.Credit(2);
            Assert.AreEqual(4, bank.Balance);
        }

        [TestMethod()]
        public void TestDebitCase()
        {
            BankAccount bank = new BankAccount("Lucy", 2);
            bank.Debit(2);
            Assert.AreEqual(0, bank.Balance);
        }

        [TestMethod()]
        public void TestCreditDebitCase()
        {
            BankAccount bank = new BankAccount("Lucy", 2);
            bank.Debit(2);
            bank.Credit(2);
            Assert.AreEqual(2, bank.Balance);
        }
    }
}

VIDEO TUTORIALS

 

Related