单元测试

本文介绍了单元测试的基础概念,包括良好的单元测试的特点、编写流程及测试驱动开发(TDD)的方法,并通过.NET示例展示了如何实现单元测试,同时探讨了依赖管理和交互测试的技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值