Pragmatic Test-Driven Development in C# and .NET - Adam Tibi - E-Book

Pragmatic Test-Driven Development in C# and .NET E-Book

Adam Tibi

0,0
29,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

Test-driven development is a manifesto for incrementally adding features to a product but starting with the unit tests first. Today’s project templates come with unit tests by default and implementing them has become an expectation. It’s no surprise that TDD/unit tests feature in most job specifications and are important ingredients for most interviews and coding challenges. Adopting TDD will enforce good design practices and expedite your journey toward becoming a better coding architect.
This book goes beyond the theoretical debates and focuses on familiarizing you with TDD in a real-world setting by using popular frameworks such as ASP.NET Core and Entity Framework. The book starts with the foundational elements before showing you how to use Visual Studio 2022 to build an appointment booking web application. To mimic real-life, you’ll be using EF, SQL Server, and Cosmos, and utilize patterns including repository, service, and builder. This book will also familiarize you with domain-driven design (DDD) and other software best practices, including SOLID and FIRSTHAND.
By the end of this TDD book, you’ll have become confident enough to champion a TDD implementation. You’ll also be equipped with a business and technical case for rolling out TDD or unit testing to present to your management and colleagues.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 411

Veröffentlichungsjahr: 2022

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Pragmatic Test-Driven Development in C# and .NET

Write loosely coupled, documented, and high-quality code with DDD using familiar tools and libraries

Adam Tibi

BIRMINGHAM—MUMBAI

Pragmatic Test-Driven Development in C# and .NET

Copyright © 2022 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Gebin George

Publishing Product Manager: Sathyanarayanan Ellapulli

Senior Editor: Nithya Sadanandan

Content Development Editor: Yashi Gupta

Technical Editor: Maran Fernandes

Copy Editor: Safis Editing

Project Coordinator: Deeksha Thakkar

Proofreader: Safis Editing

Indexer: Rekha Nair

Production Designer: Vijay Kamble

Developer Relations Marketing Executive: Sonakshi Bubbar

Business Development Executive: Kriti Sharma

First published: September 2022

Production reference: 1230922

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80323-019-1

www.packt.com

I would like to thank my dear partner, Elvira, for keeping me on track, and for her support and encouragement throughout the book-writing journey. I wouldn’t have done it without you. Also, thank you to my lovely sons, Robin and Charlie, for understanding why daddy was busy.

Also, I would like to thank the person who believed in me and enthused me, my teacher Dr. Jaber M. Jaber, and my favorite technical author of all time, Charles Petzold, who inspired me with his writing style and simplicity.

I would like to thank the editors, Nithya Sadanandan and Yashi Gupta, for their patience with me and for their remarks.

Finally, I would like to thank the reviewer, Ahmed Ilyas, for his comments and for his good grades for each chapter, which kept me going with passion.

Contributors

About the author

Adam Tibi is a London-based software consultant with over 22 years of experience in .NET, Python, the Microsoft stack, and Azure. He is experienced in mentoring teams, designing architecture, promoting agile and good software practices, and, of course, writing code. Adam has consulted for blue-chip firms including Shell, Lloyds Bank, Lloyd’s of London, Willis Towers Watson, and for a mix of start-ups. As a consultant who has a heterogeneous portfolio of clients, he has gained a solid understanding of the TDD intricacies, which he has transferred into this book.

About the reviewers

Ahmed Ilyas has 18 years of professional experience in software development.

After leaving Microsoft, he ventured into setting up his consultancy company, offering the best possible solutions for a multitude of industries and providing real-world answers to those problems. He only uses the Microsoft stack to build these technologies and provide the best practices, patterns, and software to his client base to enable long-term stability and compliance in the ever-changing software industry, but also to help software developers around the globe to improve as well as push the limits of technology.

Three times awarded MVP in C# by Microsoft and with a great reputation earned, he has a large client base for his consultancy company, Sandler Software LLC, which includes clients from different industries. Clients have included him on their “approved contractors/consultants” list as a trusted vendor and partner and this further led to his joining Microsoft again.

He has been involved in reviewing books for Packt Publishing in the past and wishes to thank them for the great opportunity once again.

Table of Contents

Preface

Part 1: Getting Started and the Basics of TDD

1

Writing Your First TDD Implementation

Technical requirements

Choosing your IDE

Microsoft VS

JetBrains Rider

VS Code

.NET and C# versions

Building a solution skeleton with unit testing

Requirements

Creating a project skeleton

Familiarizing yourself with built-in test tools

Implementing requirements with TDD

SUT

Testing class

Conditions and expectations

Red/green

The AAA pattern

More tests

Summary

Further reading

2

Understanding Dependency Injection by Example

Technical requirements

The WFA

Creating a sample weather forecaster

Adding a real weather forecaster

Understanding dependency

Abstractions and concrete types

What is a dependency?

Dependency relevance

Depend on abstraction, not concrete

Introducing DI

First example of DI

Testing an API

What are seams?

Inversion of control

Using DI containers

Container role

Third-party containers

Service lifetime

Refactoring for DI

Lifelike DI scenario

Method injection

Property injection

Service locator

Summary

Further reading

3

Getting Started with Unit Testing

Technical requirements

Introducing unit testing

What is unit testing?

Unit testing frameworks

Understanding test projects

Adding xUnit via the CLI

Test project naming convention

Running the sample unit test

Test Explorer

Unit test class anatomy

Class naming convention

Test methods

The Arrange-Act-Assert pattern

System under test

The basics of xUnit

Fact and theory attributes

Running the tests

Assert class

Applying SOLID principles to unit testing

Single-responsibility principle

Open-closed principle

Liskov substitution principle

Interface segregation principle

Dependency inversion principle

Summary

Further reading

4

Real Unit Testing with Test Doubles

Technical requirements

Understanding and using test doubles

Types of test doubles

What should I use for TDD?

More testing categories

Integration testing

Sintegration testing

Acceptance tests

Choosing test categories

Summary

Further reading

5

Test-Driven Development Explained

Technical requirements

TDD pillars

Test first

Red, green, refactor

TDD by example

Creating the solution shell

Adding coding tasks

Recap

FAQs and criticism

Why do I need to do TDD? Can’t I just do unit testing?

TDD feels unnatural to the software process!

Doing TDD is going to slow us down!

Is TDD relevant for a start-up?

I don’t like to do TDD, I like to do my code design first!

Unit testing doesn’t test the real thing!

I’ve heard about London-school TDD and classic-school TDD. What are they?

Why do some developers dislike unit testing and TDD?

What is the relationship between TDD and Agile XP?

Can a system survive without TDD?

TDD with Sintegration tests

Sintegration testing as an alternative to unit testing with TDD

Challenges of Sintegration testing

Summary

Further reading

6

The FIRSTHAND Guidelines of TDD

Technical requirements

The First guideline

Later means never

Being dependency injection ready

Designing from the client’s perspective

Promoting behavior testing

Eliminating false positives

Eradication of speculative code

The Intention guideline

Method_Condition_Expectation

Method_Should_When

Unit test structure

The Readability guideline

SUT constructor initialization

The builder pattern

The Single-Behavior guideline

What is behavior?

Example of behavior

Testing external behavior only

Why not test internals?

A single behavior per test

The Thoroughness guideline

Unit tests for dependency testing

What is code coverage?

Being thorough

The High-Performance guideline

Integration as disguised units

CPU- and memory-intensive unit tests

Having too many tests

The Automation guideline

CI automation from day 1

Platform-independent

High performance on CI

The No Interdependency guideline

Unit testing framework responsibility

Developer’s responsibility

The Deterministic guideline

Non-deterministic cases

Example of freezing time

Summary

Part 2: Building an Application with TDD

7

A Pragmatic View of Domain-Driven Design

Technical requirements

Working with a sample application

The application project

The contract objects project

The domain layer project

Exploring domains

Domain objects

Entities and value objects

Aggregates

Anemic models

Ubiquitous language

Exploring services

Post management

Application services

Infrastructure services

Service characteristics

Exploring repositories

An example of a repository

EF and repositories

Putting everything together

Solution Explorer view

Architectural view

Summary

Further reading

8

Designing an Appointment Booking App

Technical requirements

Collecting business requirements

Business goals

Stories

Designing with the DDD spirit

Domain objects

Domain services

System architecture

Implementation routes

Frontend

Relational database backend

Document DB backend

Using the Mediator pattern

Summary

Further reading

9

Building an Appointment Booking App with Entity Framework and Relational DB

Technical requirements

Planning your code and project structure

Analyzing the project’s structure

Creating projects and configuring dependencies

Setting up the domain project

Wiring up Entity Framework

Setting up the website project

Implementing the WebApis with TDD

Using the EF in-memory provider

Implementing the first story

Implementing the fifth story (time management)

Answering frequently asked questions

Are these unit tests enough?

Why didn’t we unit test the controllers?

Did we test the system enough?

We omitted testing some areas, how can we achieve high coverage?

Summary

10

Building an App with Repositories and Document DB

Technical requirements

Planning your code and project structure

Analyzing projects’ structure

Creating projects and configuring dependencies

Setting up the domain project

Exploring the repository pattern

Setting up the website project

Implementing the WebApis with TDD

Implementing the first story

Implementing the fifth story (time management)

Answering frequently asked questions

Are these unit tests enough?

Why didn’t we unit test the controllers?

Why didn’t we unit test the repositories implementation?

Did we test the system enough?

We omitted testing some areas, how can we achieve high coverage?

Summary

Part 3: Applying TDD to Your Projects

11

Implementing Continuous Integration with GitHub Actions

Technical requirements

Introduction to continuous integration

CI workflow

The benefits of CI systems

Implementing a CI process with GitHub Actions

Creating a sample project in a GitHub repo

Creating a workflow

CI and testing

Simulating failed tests

Understanding workflows

Summary

Further reading

12

Dealing with Brownfield Projects

Technical requirements

Analyzing the challenges

Dependency injection support

Code modification challenges

Time and effort challenges

The strategy of enabling TDD

Consider rewriting

Changing code

Native support for DI

Test coverage prior to unit test

Refactoring for unit testing

Variables instantiated in the code

Static members replacement

Changing code structure

Summary

Further reading 

13

The Intricacies of Rolling Out TDD

Technical challenges

Greenfield or brownfield?

Tools and infrastructure

Team challenges

Team experience

Willingness

Timing

Business challenges

Business benefits of TDD

Disadvantages from the business perspective

TDD arguments and misconceptions

Unit testing, not TDD

Unit testing is not implemented by testers

The way for writing and maintaining documentation

We have incompetent developers

Summary

Appendix 1: Commonly Used Libraries with Unit Tests

Technical requirements

Unit testing frameworks

MSTest

NUnit

Mocking libraries

Moq

Unit testing helper libraries

Fluent Assertions

AutoFixture

Further reading 

Appendix 2: Advanced Mocking Scenarios

Technical requirements

Writing an OpenWeather client library

The One Call API

Creating the solution skeleton

Starting the implementation with TDD

Fail then pass

Recap

Investigating complex mocking scenarios

Further reading

Index

Other Books You May Enjoy

Part 1: Getting Started and the Basics of TDD

In this part, we will gradually introduce all the concepts that make up test-driven development – starting with dependency injection, going through test doubles, and ending with the TDD guidelines and best practices.

By the end of this part, you will have the necessary knowledge to contribute to an application using TDD. The following chapters are included in this part:

Chapter 1, Writing Your First TDD ImplementationChapter 2, Understanding Dependency Injection by ExampleChapter 3, Getting Started with Unit TestingChapter 4, Real Unit Testing with Test DoublesChapter 5, Test-Driven Development ExplainedChapter 6, The FIRSTHAND Guidelines of TDD


Tausende von E-Books und Hörbücher

Ihre Zahl wächst ständig und Sie haben eine Fixpreisgarantie.