.NET单元测试艺术(2) - 第一个单元测试

List 2.1 使用[SetUp]和[TearDown]特性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace AOUT.LogAn.Tests
{
    [TestFixture]
    public class LogAnalyzerTests
    {
        private LogAnalyzer _analyzer = null;

        [SetUp]
        public void Setup()
        {
            _analyzer = new LogAnalyzer();
        }

        [Test]
        public void IsValidLogFileName_ValidFileLowerCased_ReturnsTrue()
        {
            bool result = _analyzer.IsValidLogFileName(@"C:\Work\Microsoft\ArtOfUnitTesting\whatever.slf");

            Assert.IsTrue(result, "filename should be valid!");
        }

        [Test]
        public void IsValidLogFileName_ValidFileUpperCased_ReturnsTrue()
        {
            bool result = _analyzer.IsValidLogFileName(@"C:\Work\Microsoft\ArtOfUnitTesting\whatever.SLF");

            Assert.IsTrue(result, "filename should be valid!");
        }

        [TearDown]
        public void TearDown()
        {
            _analyzer = null;
        }
    }
}

List 2.2 我们希望测试的LogAnalyzer文件名验证逻辑

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AOUT.LogAn
{
    public class LogAnalyzer
    {
        public bool IsValidLogFileName(string fileName)
        {
            if (!File.Exists(fileName))
            {
                throw new Exception("No log file with that name exists");
            }

            if (!fileName.ToLower().EndsWith(".slf"))
            {
                return false;
            }

            return true;
        }
    }
}

List 2.3 通过调用IsValidLogFileName来测试属性的值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AOUT.LogAn
{
    public class LogAnalyzer
    {
        private bool _wasLastFileNameValid;

        public bool WasLastFileNameValid
        {
            get { return _wasLastFileNameValid; }
            set { _wasLastFileNameValid = value; }
        }

        public bool IsValidLogFileName(string fileName)
        {

            if (!fileName.ToLower().EndsWith(".slf"))
            {
                _wasLastFileNameValid = false;
                return false;
            }

            // Save status for later valid
            _wasLastFileNameValid = true;
            return true;
        }
    }
}

List 2.4 通过调用调用方法并检查其属性值来测试一个类

[Test]
        public void IsValidLogFileName_ValidName_RemembersTrue()
        {
            LogAnalyzer log = new LogAnalyzer();
            log.IsValidLogFileName("somefile.slf");

            Assert.IsTrue(log.WasLastFileNameValid);
        }

List 2.5 Add()方法和Sum()方法

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

namespace AOUT.LogAn
{
    public class Calculator
    {
        ;

        public void Add(int number)
        {
            _sum += number;
        }

        public int Sum()
        {
            int temp = _sum;
            _sum = ;

            return temp;
        }
    }
}

List 2.6 针对Calculator的Sum()方法的最简单测试

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace AOUT.LogAn.Tests
{
    public class CalculatorTests
    {
        [Test]
        public void Sum_NoAddCalls_DefaultsToZero()
        {
            Calculator calc = new Calculator();
            int lastSum = calc.Sum();

            Assert.AreEqual(, lastSum);
        }
    }
}

List 2.7 两个测试,其中第二个调用了Add()方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace AOUT.LogAn.Tests
{
    public class CalculatorTests
    {
        private Calculator _calc = null;

        [SetUp]
        public void Setup()
        {
            _calc = new Calculator();
        }

        [Test]
        public void Sum_NoAddCalls_DefaultsToZero()
        {
            int lastSum = _calc.Sum();

            Assert.AreEqual(, lastSum);
        }

        [Test]
        public void Add_CalledOnce_SavesNumberForSum()
        {
            _calc.Add();
            int lastSum = _calc.Sum();

            Assert.AreEqual(, lastSum);
        }
    }
}
上一篇:Mvc controller单元测试 Mock Url对象


下一篇:谈谈php对象的依赖