The Power of Go: Tests

The Power of Go: Tests

The Power of Go: Tests
Автор: Arundel John
Дата выхода: 2026
Издательство: Bitfield Consulting
Количество страниц: 376
Размер файла: 2,3 МБ
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы

Praise for The Power of Go: Tests....8

Introduction....9

1. Programming with confidence....11

Self-testing code....12

The adventure begins....13

Verifying the test....16

Running tests with go test....18

Using cmp.Diff to compare results....19

New behaviour? New test.....22

Test cases....23

Adding cases one at a time....27

Quelling a panic....29

Refactoring....31

Well, that was easy....33

Sounds good, now what?....35

2. Tools for testing....37

Gos built-in testing facilities....38

Writing and running tests....39

Interpreting test output....42

The magic package: testing functions that dont exist....44

Validating our mental models....46

Concurrent tests with t.Parallel....47

Failures: t.Error and t.Errorf....48

Abandoning the test with t.Fatal....50

Writing debug output with t.Log....52

Test flags: -v and -run....53

Assistants: t.Helper....56

t.TempDir and t.Cleanup....57

Tests are for failing....58

Detecting useless implementations....60

Feeble tests....62

Comparisons: cmp.Equal and cmp.Diff....63

3. Communicating with tests....66

Tests capture intent....66

Test names should be sentences....68

Failures are a message to the future....69

Are we testing all important behaviours?....71

The power of combining tests....73

Reading tests as docs, with gotestdox....76

Definitions: does, not should....79

A sentence is about the right size for a unit....80

Keeping behaviour simple and focused....82

Shallow abstractions: Is this even worth it?....83

Dont worry about long test names....83

Crafting informative failure messages....84

Exercising failure messages....88

Executable examples....89

4. Errors expected....98

Ignoring errors is a mistake....98

Unexpected errors should stop the test....100

Error behaviour is part of your API....103

Simulating errors....107

Testing that an error is not nil....109

String matching on errors is fragile....113

Sentinel errors lose useful information....114

Detecting sentinel errors with errors.Is....117

Wrapping sentinel errors with dynamic information....120

Custom error types and errors.As....123

Conclusions....126

5. Users shouldnt do that....127

Exhaustive testing....127

Constructing effective test inputs....129

User testing....132

Crafting bespoke bug detectors....135

Table tests and subtests....136

Table tests group together similar cases....140

Using dummy names to mark irrelevant test inputs....141

Outsourcing test data to variables or functions....144

Loading test data from files....148

Readers and writers versus files....149

The filesystem abstraction....150

Using t.TempDir for test output with cleanup....151

Managing golden files....151

Dealing with cross-platform line endings....154

What about the inputs you didnt think of?....155

6. Fuzzy thinking....156

Generating random test inputs....157

Randomly permuting a set of known inputs....158

Property-based testing....159

Fuzz testing....161

The fuzz target....164

Running tests in fuzzing mode....165

Failing inputs become static test cases....166

Fuzzing a risky function....168

Adding training data with f.Add....170

A more sophisticated fuzz target....171

Using the fuzzer to detect a panic....174

Detecting more subtle bugs....179

What to do with fuzz-generated test cases....180

Fixing the implementation....182

7. Wandering mutants....185

What is test coverage?....185

Coverage profiling with the go tool....186

Coverage is a signal, not a target....189

Using bebugging to discover feeble tests....192

Detecting unnecessary or unreachable code....194

Automated mutation testing....196

Finding a bug with mutation testing....197

Running go-mutesting....199

Introducing a deliberate bug....201

Interpreting go-mutesting results....203

Revealing a subtle test feebleness....205

Fixing up the function....207

Mutation testing is worth your while....209

8. Testing the untestable....211

Building a walking skeleton....211

The first test....214

Solving big problems....215

Designing with tests....215

Unexported functions....218

Concurrency....220

Concurrency safety....230

Long-running tasks....235

User interaction....238

Command-line interfaces....246

9. Flipping the script....252

Introducing testscript....252

Running programs with exec....255

Interpreting testscript output....257

The testscript language....259

Negating assertions with the ! prefix....260

Passing arguments to programs....263

Testing CLI tools with testscript....264

Checking the test coverage of scripts....270

Comparing output with files using cmp....271

More matching: exists, grep, and -count....273

The txtar format: constructing test data files....275

Supplying input to programs using stdin....278

File operations....281

Differences from shell scripts....283

Comments and phases....284

Conditions....285

Setting environment variables with env....287

Passing values to scripts via environment variables....288

Running programs in background with &....290

The standalone testscript runner....293

Test scripts as issue repros....295

Test scripts as tests....297

Conclusion....299

10. Dependence day....301

Just dont write untestable functions....302

Reduce the scope of the dependency....303

Be suspicious of dependency injection....305

Avoid test-induced damage....307

Chunk behaviour into subcomponents....309

Reassemble the tested chunks....311

Extract and isolate the key logic....313

Isolate by using an adapter....314

Example: a database adapter....316

Fakes, mocks, stubs, doubles, and spies....324

Dont be tempted to write mocks....325

Turn time into data....327

11. Suite smells....336

No tests....336

Legacy code....338

Insufficient tests....341

Ineffective code review....343

Optimistic tests....345

Persnickety tests....351

Over-precise comparisons....355

Too many tests....357

Test frameworks....358

Flaky tests....362

Shared worlds....365

Failing tests....366

Slow tests....367

A fragrant future....370

About this book....371

Cover photo....371

Who wrote this?....371

Feedback....372

Get my free newsletter....372

Free updates to future editions....372

The Deeper Love of Go....373

The Power of Go: Tools....373

Know Go....374

Further reading....374

Credits....375

Acknowledgements....376

This book introduces you to all Go’s testing facilities, shows you how to use them to write tests for the trickiest things, and distils the collected wisdom of the Go community on best practices for testing Go programs.

About the book

What does it mean to program with confidence? How do you build self-testing software? What even is a test, anyway?

Go’s built-in support for testing puts tests front and centre of any software project, from command-line tools to sophisticated backend servers and APIs. “If you get fired as a result of applying the advice in this book, then that’s probably for the best, all things considered. But if it happens, I’ll make it my personal mission to get you a job with a better company: one where people are rewarded, not punished, for producing software that actually works.”

You’ll learn how to use tests to design programs that solve user problems, how to build reliable codebases on solid foundations, and how tests can help you tackle horrible, bug-riddled legacy codebases and make them a nicer place to live. From choosing informative, behaviour-focused names for your tests to clever, powerful techniques for managing test dependencies like databases and concurrent servers, The Power of Go: Tests has everything you need to master the art of testing in Go. Crammed with hundreds of code examples, the book uses real tests and real problems to show you exactly what to do, step by step.

Welcome to the thrilling world of fuzzy mutants and spies, guerilla testing, mocks and crocks, design smells, mirage tests, deep abstractions, exploding pointers, sentinels and six-year-old astronauts, coverage ratchets and golden files, singletons and walking skeletons, canaries and smelly suites, flaky tests and concurrent callbacks, fakes, CRUD methods, infinite defects, brittle tests, wibbly-wobby timey-wimey stuff, adapters and ambassadors, tests that fail only at midnight, and gremlins that steal from the player during the hours of darkness.

What you’ll learn

By reading through this book and completing the exercises, you'll learn:

  • How to build practical, reliable, and delightful Go programs, guided by tests
  • All the latest facilities available in Go’s top-class testing libraries and tooling
  • Standard techniques like table tests, parallel tests, deep comparisons, and golden files
  • Randomised input generation and property-based testing
  • Usability testing and exploratory testing
  • Advanced techniques such as fuzzing and mutation testing, with detailed examples
  • How to use tests to support and streamline bug fixing, refactoring, and maintaining legacy code
  • Testing error handling, validation, sentinels, wrapped errors, and other sad-path behaviours
  • Testing the untestable: user interaction, databases, CLIs, and HTTP servers
  • How to test and refactor legacy systems with awkward dependencies
  • Testing concurrent and asychronous APIs
  • Why concurrency safety matters, and how to test for it
  • Writing scripted tests for command-line tools using the testscript language
  • How and when to use advanced techniques such as mocks, doubles, stubs, spies, fakes, and adapters
  • Brittle, feeble, and flaky tests, how to avoid them, and how to fix them
  • Time, and how to fake it for testing
  • How to design useful, informative, and challenging test cases
  • How tests can tell a story, and how to write that story in a clear, engaging, and readable way
  • Building a quality-focused culture in your organisation and giving productive code reviews

And with the help of this book, you’ll learn how to build the most complex projects from scratch, using a simple, reliable, and stress-free workflow that delivers robust software fast. Tests are not the goal, I like to remind my students: the goal is programming with confidence.

What you get

Your digital download is a ZIP file containing the book in PDF and ePub format. These should be suitable for any ebook reader, Kindle, computer, phone, or tablet.


Похожее:

Список отзывов:

Нет отзывов к книге.