Reader small image

You're reading from  Pragmatic Test-Driven Development in C# and .NET

Product typeBook
Published inSep 2022
PublisherPackt
ISBN-139781803230191
Edition1st Edition
Right arrow
Author (1)
Adam Tibi
Adam Tibi
author image
Adam Tibi

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.
Read more about Adam Tibi

Right arrow

Preface

As a consultant, I worked with many teams in multiple organizations. I’ve seen teams doing TDD and I’ve seen teams doing unit testing without TDD. I’ve also seen teams that thought they were doing unit testing but were doing integration testing and I’ve seen teams that are doing none! As an ordinary human being, I started forming a belief based on empirical evidence that TDD teams are the most successful, but this is not because they are using TDD! TDD results from having passion.

TDD is unit testing plus passion. Unit testing in some teams is imposed, therefore, the developers have to do it, but TDD is rarely imposed and it is up to the developers to enforce it on themselves. Needless to say, passionate developers produce quality projects and quality projects have more potential to succeed.

TDD is usually combined with some or all aspects of domain-driven design (DDD) architecture. So, I made sure I covered both TDD and DDD combined to be able to give realistic examples. I also wanted to reflect today’s market that is divided between two database categories, relational and document DBs, so I took the liberty to include an example chapter for each and show the differences in unit-testing implementations with the objective of keeping the book pragmatic.

Don’t be tricked by the book size, as the diagrams and the code snippets do inflate the book. I strived to stay away from old and impractical theories to shorten the book and stick to the point.

TDD and unit testing are in most modern job specifications, a requirement for interview test projects, and the subject of hot interview questions. If you wanted to know more about these topics and become a TDD developer, then you came to the right place.

There are many other good books on TDD and they are aimed at .NET developers as well, so why this book? In this book, I show real practical implementations by going to the DDD world, relational DBs, and document DBs. I demonstrate the decision tree of the mindset that practitioners use when doing TDD. I show the relationship between SOLID and TDD and I introduce a set of memorable best practices known as the FIRSTHAND guidelines of TDD.

My intention for writing this book is to have you as a confident TDD practitioner, or at least a unit-testing practitioner, and I hope I was able to deliver my intention.

Who this book is for

Test-driven development is the mainstream way of designing, documenting, and testing your application from day one. As a developer looking to climb the technical ladder to a more senior position, TDD and its related topics of unit testing, test doubles, and dependency injection are a must-learn.

This book is for mid- to senior-level .NET developers who are looking to utilize the potential of TDD to develop high-quality software. Basic knowledge of OOP and C# programming concepts is assumed, but no knowledge of TDD or unit testing is expected. As the book provides in-depth coverage of all the concepts of TDD and unit testing, and acts as an excellent guide for developers who want to build a TDD-based application from scratch or developers planning to introduce unit testing into their organization.

What this book covers

The book covers TDD and its .NET ecosystem of IDEs and libraries and goes through setting up the environment. The book starts by covering the topics that form the prerequisites for TDD, which are dependency injection, unit testing, and test doubles. Then, after covering TDD and its best practices, the book dives into building an application from scratch using domain-driven design as an architecture.

The book also covers the basics of building a continuous integration pipeline, dealing with legacy code that wasn’t written with testability in mind, and finishing with ideas for rolling out TDD into your organization.

Chapter 1, Writing Your First TDD Implementation, doesn’t have a long introduction or theory but rather dives directly into IDE selection and writing your first TDD implementation to get a taste of flavor of the content of the book.

Chapter 2, Understanding Dependency Injection by Example, revises advanced OOP principles needed to understand the concept of dependency injection and provides multiple examples.

Chapter 3, Getting Started with Unit Testing, offers a simple introduction to xUnit and unit-testing basics.

Chapter 4, Real Unit Testing with Test Doubles, goes through stubbing, mocking, and NSubstitute, and then discusses more testing categories.

Chapter 5, Test-Driven Development Explained, demonstrates how to write unit testing but in TDD style, and discusses the pros and cons.

Chapter 6, The FIRSTHAND Guidelines of TDD, details the best practices of unit testing and TDD.

Chapter 7, A Pragmatic View of Domain-Driven Design, introduces DDD, services, and repositories.

Chapter 8, Designing an Appointment Booking App, outlines the specification for a real-life app to be implemented later with a DDD architecture and the TDD style.

Chapter 9, Building an Appointment Booking App with Entity Framework and Relational DB, demonstrates an example of a TDD application using a relational DB backend.

Chapter 10, Building an App with Repositories and Document DB, demonstrates an example of a TDD application using a document DB and the repository pattern.

Chapter 11, Implementing Continuous Integration with GitHub Actions, shows how to use GitHub Actions to build a CI pipeline for the application in Chapter 10.

Chapter 12, Dealing with Brownfield Projects, outlines the thinking process when considering TDD and unit testing for legacy projects.

Chapter 13, The Intricacies of Rolling Out TDD, explains the train of thought when getting your organization to adopt TDD.

Appendix 1, Commonly Used Libraries with Unit Tests, shows some quick examples of MSTest, NUnit, Moq, Fluent Assertions, and Auto Fixture.

Appendix 2, Advanced Mocking Scenarios, demonstrates a more complex mocking scenario with NSubstitute.

To get the most out of this book

This book assumes you are familiar with C# syntax with at least one year’s experience working with Visual Studio or a similar IDE environment. While the advanced concepts of OOP principles will be revised in this book, the book assumes you are familiar with the basics.

Software covered in the book

Operating system requirements

Visual Studio 2022

Windows or macOS

Fine Code Coverage

Windows

SQL Server

Windows, macOS (Docker), or Linux

Cosmos DB

Windows, macOS (Docker), or Linux (Docker)

Libraries and frameworks

Operating system requirements

.NET Core 6, C# 10

Windows, macOS, or Linux

xUnit

Windows, macOS, or Linux

NSubstitute

Windows, macOS, or Linux

Entity Framework

Windows, macOS, or Linux

To get the most out of the book, you need to have a C# IDE. This book uses Visual Studio 2022 Community Edition and presents alternatives at the beginning of Chapter 1.

If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book’s GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.

Download the example code files

You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Pragmatic-Test-Driven-Development-in-C-Sharp-and-.NET. If there’s an update to the code, it will be updated in the GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: https://packt.link/OzRlM.

Conventions used

There are a number of text conventions used throughout this book.

Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: “The previous code fails this rule, as running UnitTest2 before UnitTest1 will fail the test.”

A block of code is set as follows:

public class SampleTests 
{
    private static int _staticField = 0;
    [Fact]
    public void UnitTest1()
    {
        _staticField += 1;
        Assert.Equal(1, _staticField);
    }
    [Fact]
    public void UnitTest2()
    {
        _staticField += 5;
        Assert.Equal(6, _staticField);
    }
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public class SampleTests 
{
    private static int _staticField = 0;
    [Fact]
    public void UnitTest1()
    {
        _staticField += 1;
        Assert.Equal(1, _staticField);
    }
    [Fact]
    public void UnitTest2()
    {
        _staticField += 5;
        Assert.Equal(6, _staticField);
    }
}

Any command-line input or output is written as follows:

GET https://webapidomain/services

Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: “After installing the local emulator, you need to grab the connection string, which you can do by browsing to https://localhost:8081/_explorer/index.html and copying the connection string from the Primary Connection String field.”

Tips or important notes

Appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, email us at customercare@packtpub.com and mention the book title in the subject of your message.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Share Your Thoughts

Once you’ve read Pragmatic Test-Driven Development in C# and .NET, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Pragmatic Test-Driven Development in C# and .NET
Published in: Sep 2022Publisher: PacktISBN-13: 9781803230191
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Adam Tibi

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.
Read more about Adam Tibi