单元测试

来源:互联网 发布:福特嘉年华轮毂15数据 编辑:程序博客网 时间:2024/05/15 19:01

The Art of Unit Testing with Examples in .NET

第一章:单元测试的基本知识

0、一个好的单元测试应当有如下特性:

❂ It should be automated and repeatable.
❂ It should be easy to implement.
❂ Once it’s written, it should remain for future use.
❂ Anyone should be able to run it.
❂ It should run at the push of a button.
❂ It should run quickly.

1、写单元测试的传统方式

2、TDD中的单元测试开发

3 TDD开发

1 Write a failing test to prove code or functionality is missing from the end product.

2 Make the test pass by writing production code that meets the expectations of your test.

3 Refactor your code.

第二章:单元测试举例

1、基本的命名规范:

Project:[项目名].Tests,如:AOUT.Logan.Tests

Class:[类名]Tests,如: LogAnalyzerTests

Method:[方法名]_[测试条件]_[在该测试条件下返回的结果],如:IsValidFileName_validFile_ReturnsTrue()

2 示例:

using System;

using System.IO;

namespace AOUT.CH2.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;

}

}

}

测试代码

using System;

using NUnit.Framework;

namespace AOUT.CH2.LogAn.Tests

{

[TestFixture]

public class LogAnalyzerTests

{

private LogAnalyzer m_analyzer=null;

[SetUp]

public void Setup()

{

m_analyzer = new LogAnalyzer();

}

[Test]

[Ignore("This test is broken")]

public void IsValidFileName_validFileLowerCased_ReturnsTrue()

{

bool result = m_analyzer.IsValidLogFileName("whatever.slf");

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

}

[Test]

public void IsValidFileName_validFileUpperCased_ReturnsTrue()

{

bool result = m_analyzer.IsValidLogFileName("whatever.SLF");

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

}

[Test]

[ExpectedException(typeof(Exception),"No log file with that name exists")]

public void IsValidFileName_nunintvalidFileUpperCased_ReturnsTrue()

{

bool result = m_analyzer.IsValidLogFileName("whatever.SLF");

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

}

[TearDown]

public void TearDown()

{

m_analyzer = null;

}

}

}

第三章:用测试桩消除依赖

1、Refactoring our design to be more testable

Refactoring is the act of changing the code’s design without breaking
existing functionality.
Seams are places in your code where you can plug in different function-
ality, such as stub classes.

2、消除依赖的具体技术

❂ Extract an interface to allow replacing underlying implementation.
❂ Inject stub implementation into a class under test.
❂ Receive an interface at the constructor level.
❂ Receive an interface as a property get or set.
❂ Get a stub just before a method call.

第四章:用Mock对象进行交互测试

1 Interaction testing is testing how an object sends input to or receives
input from other objects—how that object interacts with other objects.

2 A mock object is a fake object in the system that decides whether the
unit test has passed or failed. It does so by verifying whether the object
under test interacted as expected with the fake object. There’s usually
no more than one mock per test.

3 A fake is a generic term that can be used to describe either a stub or a mock
object (handwritten or otherwise), because they both look like the real object.
Whether a fake is a stub or a mock depends on how it’s used in the current
test. If it’s used to check an interaction (asserted against), it’s a mock object.
Otherwise, it’s a stub.

原创粉丝点击