第1ユニット試験例


1.被測定類
被験関数IsValidLogFileNameは、ファイル名が.logの末尾.
public class LogAnalyzer
{
    public bool IsValidLogFileName(string fileName)
    {
        if (string.IsNullOrEmpty(fileName))
            throw new ArgumentException("fileName is null");

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

2.ユニットテスト
[TestFixture]
public class LogAnalyzerTests
{
    private LogAnalyzer analyzer = null;

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

    [Test]
    public void IsValidLogFileName_validFileLower_ReturnTrue()
    {
        bool flag = analyzer.IsValidLogFileName("20190103.log");
        Assert.IsTrue(flag);
    }

    [Test]
    public void IsValidLogFileName_validFileUpper_ReturnTrue()
    {
        bool flag = analyzer.IsValidLogFileName("20190103.LOG");
        Assert.IsTrue(flag);
    }

    [Test]
    [ExpectedException(typeof(ArgumentException),
        ExpectedMessage = "fileName is null")]
    public void IsValidLogFileName_emptyFileName_ThrowException()
    {
        analyzer.IsValidLogFileName(string.Empty);
    }


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