1.被测类
被测类LogAnalyzer,其中的被测函数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;
}
}