Unit Testing with NSubstitute

简介:

These are the contents of my training session about unit testing, and also have some introductions about how could we write better unit tests with NSubstitute framework.

The related sessions:

Agenda

Unit Testing Introduction

Unit Testing Classic Definition

A unit test is a piece of a code (usually a method) that invokes another piece of codeand checks the correctness of some assumptions afterward. If the assumptions turn out to be wrong, the unit test has failed. A “unit” is a method or function. In OOP, a “unit” is often an entire interface, such as class, but could be an individual method.
-- from Wikipedia
Kent Beck introduced the concept of unit testing in Smalltalk.

Why Unit Testing?

  • Find Problems Early
    • Find problems in development cycle.
  • Facilitate Changes
    • Allow programmer to refactor code at a later date.
  • Simplify Integration
    • Reduce uncertainty, reduce efforts from integration test.
  • Documentation
    • Sort of living documentation of system.
  • Design by Test Driven
    • Support TDD approach.

Properties of a good unit test

A unit test  should have the following properties:
  • 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.

Unit Testing Good Definition

A unit test is an automated piece of code that invokes the method or class being tested and thenchecks some assumptions about the logical behavior of that method or class. A unit test is almost always written using a unit-testing framework. It can be written easily and runs quickly. It’s fully automated, trustworthy, readable, and maintainable.

What's Logical Behavior?

Logical code is any piece of code that  has some sort of logic in it, small as it may be. It’s logical code if it has one or more of the following: an IF statement, a loop, switch or case statements, calculations, or any other type of  decision-making code.

Verification Patterns -- State

  • State Verification, also known as State-based Testing.
    • We inspect the state of the system under test (SUT) after it has been exercised and compare it to the expected state.
 

Verification Patterns -- Behavior

  • Behavior Verification, also known as Interaction Testing.
    • We capture the indirect outputs of the SUT as they occur and compare them to the expected behavior.
 

Simple Example

Code:

 
Test:
 
 

TDD: Test-Driven Development

When to write the tests?

  1. Write a failing test to prove code or functionality is missing from the end product. (RED)
  2. Make the test pass by writing production code that meets the expectations of your test.(GREEN)
  3. Refactor your code. (REFACTOR)

Define TDD Paradigm

TAD: Test After Development

  • Are we TDD-style?
    • No, we are TAD now.
  • Must we do TDD-style coding?
    • No, TDD is a style choice. You can make your own choice.

Unit Testing Frameworks

Framework for Unit Testing

Unit testing frameworks are code libraries and modules that help developers unit-test their code. And also help running the tests as part of an automated build.

What Unit Testing Framework Offer

  • Write tests easily and in a structured manner.
    • Base classes or interfaces to inherit.
    • Attributes to place in code to note the tests to run.
    • Assert classes.
  • Execute one or all of the unit tests.
    • Identify tests in your code.
    • Run tests automatically.
    • Indicate status while running.
  • Review the result of the test runs.
    • Red/Green

xUnit architecture

  • Test Fixtures
  • Test Cases
  • Test Suites
  • Test Execution
  • Assertions

List of Unit Testing Frameworks

Integrated with Automated Build

  • With MSBuild.
    • <RunTest>true</RunTest>
    • <TestContainer Include="UT*.dll"/>

Unit Testing Patterns

Record – Replay – Verify Model

A model that allows for recording actions on a mock object and then replaying and verifying them.

Arrange – Act – Assert Pattern

  • "Arrange-Act-Assert" is a pattern for arranging and formatting code in UnitTest methods.
    • Each method should group these functional sections, separated by blank lines:
      1. Arrange all necessary preconditions and inputs.
      2. Act on the object or method under test.
      3. Assert that the expected results have occurred.

Unit Testing Mocking Libraries

Mocking Library

A mocking library is a set of programmable APIs that make creating mock and stub objects much easier. Mocking libraries save the developer from the need to write repetitive code to test or simulate object interactions.

Other names:

  • Mocking framework
  • Isolation framework

Most Features of Mocking Library

  • No Record/Replay idioms to learn.
  • Very low learning curve. Don't even need to read the documentation.
  • No need to learn what's the theoretical difference between a mock, a stub, a fake, a dynamic mock, etc.
  • Strong-typed, no object-typed return values or constraints.
  • Mock both interfaces and classes.
  • Override expectations as needed on tests.
  • Pass constructor arguments for mocked classes.
  • Intercept and raise events on mocks.
  • Intuitive support for out/ref arguments.

Famous Mocking Libraries

  • NSubstitute
  • Moq
  • Rhino Mocks
  • Microsoft Fakes
  • NMock
  • EasyMock.NET
  • TypeMock Isolator
  • JustMock
  • FakeItEasy

The Values of a Good Mocking Library

  • Simple to use. For example, a single point of entry in the API, easy to remember API, or Intelligence guidance.
  • Test Robustness. Making sure the test can live as long as possible while the system changes, and it only breaks when something important to it changes. Things like recursive fakes help a lot there. So does being non strict by default.
  • Helpful. If something is wrong, the user should not feel stupid. Helpful error messages go a long way. Being specific and not surprising the user as much as possible.

Mocking Libraries Comparison

-- Roy Osherove, the author of “The Art of Unit Testing”

Why we choose NSubstitute?

Why We Choose NSubstitute?

  • Simple, succinct, pleasant to use.
  • Helpful exceptions.
  • Don't sweat the small stuff.
    • Mock, stub, fake, spy, test double? Nah, just substitute for the type you need!

NSubstitute Features & Demos

Creating a substitute

Setting a return value

Return for any args

Return from a function

Argument matchers

Replacing return values

Check received calls

Callbacks, When..Do

Throwing exceptions

Raising events

References







本文转自匠心十年博客园博客,原文链接:http://www.cnblogs.com/gaochundong/p/unit_testing_with_nsubstitute.html,如需转载请自行联系原作者
目录
相关文章
|
12月前
|
NoSQL 关系型数据库 OLAP
如何选择最合适的数据库,帮助企业及个人业务更好的开展
如何选择最合适的数据库,帮助企业及个人业务更好的开展
|
存储 监控 安全
JVM内存管理机制&线上问题排查
本文主要基于“深入java虚拟机”这本书总结JVM的内存管理机制,并总结了常见的线上问题分析思路。文章最后面是我对线上故障思考的ppt总结。 Java内存区域 虚拟机运行时数据区如下图所示: 15291199000153.jpg 方法区:方法区又称为永生代(Permanent Generation)是线程共享的内存区域。
3190 0
|
存储 Java 程序员
Spring更简单的存储和读取Bean
在上一篇文章【Spring的创建与使用】中,我们已经了解了Spring中bean对象的基本的创建和使用方法,这篇文章通过注解的方法,使得存储和读取对象更加简单。
298 0
Spring更简单的存储和读取Bean
|
开发工具 git
UART子系统(七)字符设备驱动程序的另一种注册方法
UART子系统(七)字符设备驱动程序的另一种注册方法
219 0
UART子系统(七)字符设备驱动程序的另一种注册方法
|
JavaScript 前端开发 索引
JavaScript数组(Array)常用方法汇总
JavaScript数组(Array)常用方法汇总
275 0
|
8天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
7天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
346 130
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话