Unit Testing Principles, Practices, and Patterns
"This book is an indispensable resource." - Greg Wright, Kainos Software Ltd.

Radically improve your testing practice and software quality with new testing styles, good patterns, and reliable automation.

Key Features
A practical and results-driven approach to unit testing
Refine your existing unit tests by implementing modern best practices
Learn the four pillars of a good unit test
Safely automate your testing process to save time and money
Spot which tests need refactoring, and which need to be deleted entirely

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About The Book
Great testing practices maximize your project quality and delivery speed by identifying bad code early in the development process. Wrong tests will break your code, multiply bugs, and increase time and costs. You owe it to yourself—and your projects—to learn how to do excellent unit testing.

Unit Testing Principles, Patterns and Practices teaches you to design and write tests that target key areas of your code including the domain model. In this clearly written guide, you learn to develop professional-quality tests and test suites and integrate testing throughout the application life cycle. As you adopt a testing mindset, you’ll be amazed at how better tests cause you to write better code.

What You Will Learn

Universal guidelines to assess any unit test
Testing to identify and avoid anti-patterns
Refactoring tests along with the production code
Using integration tests to verify the whole system

This Book Is Written For
For readers who know the basics of unit testing. Examples are written in C# and can easily be applied to any language.

About the Author

Vladimir Khorikov is an author, blogger, and Microsoft MVP. He has mentored numerous teams on the ins and outs of unit testing.

Table of Contents:

PART 1 THE BIGGER PICTURE

1 ¦ The goal of unit testing
2 ¦ What is a unit test?
3 ¦ The anatomy of a unit test

PART 2 MAKING YOUR TESTS WORK FOR YOU

4 ¦ The four pillars of a good unit test
5 ¦ Mocks and test fragility
6 ¦ Styles of unit testing
7 ¦ Refactoring toward valuable unit tests

PART 3 INTEGRATION TESTING

8 ¦ Why integration testing?
9 ¦ Mocking best practices
10 ¦ Testing the database

PART 4 UNIT TESTING ANTI-PATTERNS

11 ¦ Unit testing anti-patterns
"1137832071"
Unit Testing Principles, Practices, and Patterns
"This book is an indispensable resource." - Greg Wright, Kainos Software Ltd.

Radically improve your testing practice and software quality with new testing styles, good patterns, and reliable automation.

Key Features
A practical and results-driven approach to unit testing
Refine your existing unit tests by implementing modern best practices
Learn the four pillars of a good unit test
Safely automate your testing process to save time and money
Spot which tests need refactoring, and which need to be deleted entirely

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About The Book
Great testing practices maximize your project quality and delivery speed by identifying bad code early in the development process. Wrong tests will break your code, multiply bugs, and increase time and costs. You owe it to yourself—and your projects—to learn how to do excellent unit testing.

Unit Testing Principles, Patterns and Practices teaches you to design and write tests that target key areas of your code including the domain model. In this clearly written guide, you learn to develop professional-quality tests and test suites and integrate testing throughout the application life cycle. As you adopt a testing mindset, you’ll be amazed at how better tests cause you to write better code.

What You Will Learn

Universal guidelines to assess any unit test
Testing to identify and avoid anti-patterns
Refactoring tests along with the production code
Using integration tests to verify the whole system

This Book Is Written For
For readers who know the basics of unit testing. Examples are written in C# and can easily be applied to any language.

About the Author

Vladimir Khorikov is an author, blogger, and Microsoft MVP. He has mentored numerous teams on the ins and outs of unit testing.

Table of Contents:

PART 1 THE BIGGER PICTURE

1 ¦ The goal of unit testing
2 ¦ What is a unit test?
3 ¦ The anatomy of a unit test

PART 2 MAKING YOUR TESTS WORK FOR YOU

4 ¦ The four pillars of a good unit test
5 ¦ Mocks and test fragility
6 ¦ Styles of unit testing
7 ¦ Refactoring toward valuable unit tests

PART 3 INTEGRATION TESTING

8 ¦ Why integration testing?
9 ¦ Mocking best practices
10 ¦ Testing the database

PART 4 UNIT TESTING ANTI-PATTERNS

11 ¦ Unit testing anti-patterns
36.99 In Stock
Unit Testing Principles, Practices, and Patterns

Unit Testing Principles, Practices, and Patterns

by Vladimir Khorikov
Unit Testing Principles, Practices, and Patterns

Unit Testing Principles, Practices, and Patterns

by Vladimir Khorikov

eBook

$36.99 

Available on Compatible NOOK devices, the free NOOK App and in My Digital Library.
WANT A NOOK?  Explore Now

Related collections and offers


Overview

"This book is an indispensable resource." - Greg Wright, Kainos Software Ltd.

Radically improve your testing practice and software quality with new testing styles, good patterns, and reliable automation.

Key Features
A practical and results-driven approach to unit testing
Refine your existing unit tests by implementing modern best practices
Learn the four pillars of a good unit test
Safely automate your testing process to save time and money
Spot which tests need refactoring, and which need to be deleted entirely

Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.

About The Book
Great testing practices maximize your project quality and delivery speed by identifying bad code early in the development process. Wrong tests will break your code, multiply bugs, and increase time and costs. You owe it to yourself—and your projects—to learn how to do excellent unit testing.

Unit Testing Principles, Patterns and Practices teaches you to design and write tests that target key areas of your code including the domain model. In this clearly written guide, you learn to develop professional-quality tests and test suites and integrate testing throughout the application life cycle. As you adopt a testing mindset, you’ll be amazed at how better tests cause you to write better code.

What You Will Learn

Universal guidelines to assess any unit test
Testing to identify and avoid anti-patterns
Refactoring tests along with the production code
Using integration tests to verify the whole system

This Book Is Written For
For readers who know the basics of unit testing. Examples are written in C# and can easily be applied to any language.

About the Author

Vladimir Khorikov is an author, blogger, and Microsoft MVP. He has mentored numerous teams on the ins and outs of unit testing.

Table of Contents:

PART 1 THE BIGGER PICTURE

1 ¦ The goal of unit testing
2 ¦ What is a unit test?
3 ¦ The anatomy of a unit test

PART 2 MAKING YOUR TESTS WORK FOR YOU

4 ¦ The four pillars of a good unit test
5 ¦ Mocks and test fragility
6 ¦ Styles of unit testing
7 ¦ Refactoring toward valuable unit tests

PART 3 INTEGRATION TESTING

8 ¦ Why integration testing?
9 ¦ Mocking best practices
10 ¦ Testing the database

PART 4 UNIT TESTING ANTI-PATTERNS

11 ¦ Unit testing anti-patterns

Product Details

ISBN-13: 9781638350293
Publisher: Manning
Publication date: 01/06/2020
Sold by: SIMON & SCHUSTER
Format: eBook
Pages: 304
File size: 3 MB

About the Author

Vladimir Khorikov is the author of Unit Testing Principles, Practices, and Patterns, Pluralsight author, and Microsoft MVP. He has mentored numerous teams on the ins and outs of unit testing.

Table of Contents

Preface xiv

Acknowledgments xv

About this book xvi

About the author xix

About the cover illustration xx

Part 1 The Bigger Picture 1

1 The goal of unit testing 3

1.1 The current state of unit testing 4

1.2 The goal of unit testing 5

What makes a good or bad test? 7

1.3 Using coverage metrics to measure test suite quality 8

Understanding the code coverage metric 9

Understanding the branch coverage metric 10

Problems with coverage metrics 12

Aiming at a particular coverage number 15

1.4 What makes a successful test suite? 15

It's integrated into the development cycle 16

It targets only the most important parts of your code base 16

It provides maximum value with minimum maintenance costs 17

1.5 What you will learn in this book 17

2 What is a unit test? 20

2.1 The definition of "unit test" 21

The isolation issue: The London take 21

The isolation issue: The classical take 27

2.2 The classical and London schools of unit testing 30

How the classical and London schools handle dependencies 30

2.3 Contrasting the classical and London schools of unit testing 34

Unit testing one class at a time 34

Unit testing a large graph of interconnected classes 35

Revealing the precise bug location 36

Other differences between the classical and London schools 36

2.4 Integration tests in the two schools 37

End-to-end tests are a subset of integration tests 38

3 The anatomy of a unit test 41

3.1 How to structure a unit test 42

Using the AAA pattern 42

Avoid multiple arrange, act, and assert sections 43

Avoid if statements in tests 44

How large should each section be"? 45

How many assertions should the assert section hold? 47

What about the teardown phase? 47

Differentiating the system under test 47

Dropping the arrange, act, and assert comments from tests 48

3.2 Exploring the xUnit testing framework 49

3.3 Reusing test fixtures between tests 50

High coupling between tests is an anti-pattern 52

The use of constructors in tests diminishes test readability 52

A better way to reuse test fixtures 52

3.4 Naming a unit test 54

Unit test naming guidelines 56

Example: Renaming a test toward the guidelines 56

3.5 Refactoring to parameterized tests 58

Generating data for parameterized tests 60

3.6 Using an assertion library to further improve test readability 62

Part 2 Making Your Tests Work for You 65

4 The four pillars of a good unit test 67

4.1 Diving into the four pillars of a good unit test 68

The first pillar: Protection against regressions 68

The second pillar: Resistance to refactoring 69

What causes false positives? 71

Aim at the end result instead of implementation details 74

4.2 The intrinsic connection between the first two attributes 76

Maximizing test accuracy 76

The importance of false positives and false negatives: The dynamics 78

4.3 The third and fourth pillars: Fast feedback and maintainability 79

4.4 In search of an ideal test 80

Is it possible to create an ideal test? 81

Extreme case #1 End-to-end tests 81

Extreme case #2 Trivial tests 82

Extreme case #3 Brittle tests 83

In search of an ideal test: The results 84

4.5 Exploring well-known test automation concepts 87

Breaking down the Test Pyramid 87

Choosing between black-box and white-box testing 89

5 Mocks and test fragility 92

5.1 Differentiating mocks from stubs 93

The types of test doubles 93

Mock (the tool) vs. mock (the test double) 94

Don't assert interactions with stubs 96

Using mocks and stubs together 97

How mocks and stubs relate to commands and queries 97

5.2 Observable behavior vs. implementation details 99

Observable behavior is not the same as a public API 99

Leaking implementation details: An example with an operation 100

Well-designed API and encapsulation 103

Leaking implementation details: An example with state 104

5.3 The relationship between mocks and test fragility 106

Defining hexagonal architecture 106

Intra-system vs. inter-system communications 110

Intra-system vs. inter-system communications: An example 111

5.4 The classical vs. London schools of unit testing, revisited 114

Not all out-of-process dependencies should be mocked out 115

Using mocks to verify behavior 116

6 Styles of unit testing 119

6.1 The three styles of unit testing 120

Defining the output-based style 120

Defining the state-based style 121

Defining the communication-based style 122

6.2 Comparing the three styles of unit testing 123

Comparing the styles using the metrics of protection against regressions and feedback speed 124

Comparing the styles using the metric of resistance to refactoring 124

Comparing the styles using the metric of maintainability 125

Comparing the styles: The results 127

6.3 Understanding functional architecture 128

What is functional programming? 128

What is functional architecture? 132

Comparing functional and hexagonal architectures 133

6.4 Transitioning to functional architecture and output-based testing 135

Introducing an audit system 135

Using mocks to decouple tests from the filesystem 137

Refactoring toward functional architecture 140

Looking forward to further developments 146

6.5 Understanding the drawbacks of functional architecture 146

Applicability of functional architecture 147

Performance drawbacks 148

Increase in the code base size 149

7 Refactoring toward valuable unit tests 151

7.1 Identifying the code to refactor 152

The four types of code 152

Using the Humble Object pattern to split overcomplicated code 155

7.2 Refactoring toward valuable unit tests 158

Introducing a customer management system 158

Take 1 Making implicit dependencies explicit 160

Take 2 Introducing an application services layer 160

Take 3 Removing complexity from the application service 163

Take 4 Introducing a new Company class 164

7.3 Analysis of optimal unit test coverage 167

Testing the domain layer and utility code 167

Testing the code from the other three quadrants 168

Should you test preconditions? 169

7.4 Handling conditional logic in controllers 169

Using the CanExecute/Execute pattern 172

Using domain events to track changes in the domain model 175

7.5 Conclusion 178

Part 3 Integration Testing 183

8 Why integration testing? 185

8.1 What is an integration test? 186

The role of integration tests 186

The Test Pyramid revisited 187

Integration testing vs. failing fast 188

8.2 Which out-of-process dependencies to test directly 190

The two types of out-of-process dependencies 190

Working with both managed and unmanaged dependencies 191

What if you can't use a real database in integration tests? 192

8.3 Integration testing: An example 193

What scenarios to test? 194

Categorizing the database and the message bus 195

What about end-to-end testing? 195

Integration testing: The first try 196

8.4 Using interfaces to abstract dependencies 197

Interfaces and loose coupling 198

Why use interfaces for out-of-process dependencies? 199

Using interfaces for in-process dependencies 199

8.5 Integration testing best practices 200

Making domain model boundaries explicit 200

Reducing the number of layers 200

Eliminating circular dependencies 202

Using multiple act sections in a test 204

8.6 How to test logging functionality 205

Should you test logging? 205

How should you test logging? 207

How much logging is enough? 212

How do you pass around logger instances? 212

8.7 Conclusion 213

9 Mocking best practices 216

9.1 Maximizing mocks' value 217

Verifying interactions at the system edges 219

Replacing mocks with spies 222

What about IDomainLogger? 224

9.2 Mocking best practices 225

Mocks are for integration tests only 225

Not just one mock per test 225

Verifying the number of calls 226

Only mock types that you own 227

10 Testing the database 229

10.1 Prerequisites for testing the database 230

Keeping the database in the source control system 230

Reference data is part of the database schema 231

Separate instance for every developer 232

State-based vs. migration-based database delivery 232

10.2 Database transaction management 234

Managing database transactions in production code 235

Managing database transactions in integration tests 242

10.3 Test data life cycle 243

Parallel vs. sequential test execution 243

Clearing data between test runs 244

Avoid in-memory databases 246

10.4 Reusing code in test sections 246

Reusing code in arrange sections 246

Reusing code in act sections 249

Reusing code in assert sections 250

Does the test create too many database transactions? 251

10.5 Common database testing questions 252

Should you test reads? 252

Should you test repositories? 253

10.6 Conclusion 254

Part 3 Unit Testing Anti-Patterns 257

11 Unit testing anti-patterns 259

11.1 Unit testing private methods 260

Private methods and test fragility 260

Private methods and insufficient coverage 260

When testing private methods is acceptable 261

11.2 Exposing private state 263

11.3 Leaking domain knowledge to tests 264

11.4 Code pollution 266

11.5 Mocking concrete classes 268

11.6 Working with time 271

Time as an ambient context 271

Time as an explicit dependency 272

11.7 Conclusion 273

Index 275

From the B&N Reads Blog

Customer Reviews