Skip to main content

Testing with Go

33h 16m 48s
English
Free

Course description

Learn how to test real, complex software written in Go. Large or small, perfect abstractions or global state galore; it doesn't matter what your code looks like, you CAN learn to test it. Tell me if this sounds familiar - you are learning how to test in Go, and things seem to be going great. The tutorials are all clicking, and you can't wait to start applying what you are learning in your real projects.

Read more about the course

You fire up your editor, grab your latest project, create your first *_test.go source file, and suddenly it feels like you don't have a clue what you are doing.

*You hear the sound of glass shattering*

What happened?!?!

Things were going so great. All those examples made sense, but now you don't even know where to start.

It seemed so easy to test that "Hello, world" HTTP handler, but how do you test complex handlers? You know, HTTP handlers that do something realistic like insert a record into a database, or use an API to verify someone's address.

For that matter, how do we verify that our database interactions are working as we expected? Or maybe your app has a global DB variable - does that mean testing simply isn't possible?

What about those APIs we are interacting with? Do we stub them? Do we hit the test API? What happens if we hit API rate limits or there isn't even a test API?

Alright, alright! Take a second to breathe and let me fill you in on a little secret...

Testing isn't hard, but simple tutorials don't do it justice

Testing isn't any harder than writing any other Go code. In fact, if we wanted we could test our code by just writing a main package and interacting with our application code. We could panic when something doesn't act the way we expected, and viola - we have a test!

But why does it feel so hard? Probably because we simplify the examples to the point that they lose all of their value.

Think about it, when is the last time you wrote a Palindrome function?

Never? So why are all these tutorials showing us how to test one?

Why aren't they showing us how to test realistic software? What happened to the example where we test a real HTTP handler that needs access to a data store? Or the tutorial where we build an API client and learn how to test it WITHOUT always hitting the real API. Wouldn't it be great if we could learn how to test a real web app with a real DB and a real integration to a payments API like Stripe?

Test with Go is different.

In this course you will learn how to test REAL software, not palindrome functions.

We will have to look at a few isolated examples in order to learn specific testing techniques, but that isn't enough to solidify a concept so we won't stop there. We will build real projects that teach you how to apply all of these testing skills in real software.

In one project we build a web application which allows us to address the complexities that come up while testing an application that uses a database, third party APIs, and more. In another project we look at how internal testing helps us verify our intermediate steps are correct, while also discussing the downside to testing unexported functions.

You will learn about common pitfalls to avoid in order to write more testable code. You will learn how to incrementally fix code that has already succumbed to many of these pitfalls, allowing you to avoid a massive PR that makes your reviewer cry inside.

When you run into an application with a global DB variable you won't need to give up on testing. You will learn exactly how to make small, manageable changes to the code that allow you to start testing it almost immediately.

The next time you are asked whether the data store should be mocked or if a real SQL database should be used you will be able to discuss the pros and cons of both approaches with your teammates and decide on a proper plan of action.

After completing this course you will have the knowledge and the skills necessary to start testing your own projects. You will still have to put in the work, but the mystery, the confusion, and the frustration will be gone.

Watch Online

0:00 0:00
#Lesson TitleDuration
11 - What is a test?03:13
22 - Why do tests matter?09:37
33 - Writing great tests25:30
44 - Testing with a main package06:57
55 - Testing with Go's testing package08:22
66 - What happens when we run go test07:03
77 - File naming conventions05:46
88 - Function naming conventions04:48
99 - Variable naming conventions05:53
1010 - Ways to signal test failure08:19
1111 - When to use Error vs Fatal17:38
1212 - Writing useful failure messages17:41
1313 - A basic example as a test case06:50
1414 - Viewing examples in the docs05:49
1515 - Unordered example output04:28
1616 - Complex examples15:29
1717 - Examples in the standard library04:20
1818 - Table driven tests10:47
1919 - Generating table driven test code05:01
2020 - Subtests10:47
2121 - Shared setup and teardown13:40
2222 - TestMain10:09
2323 - Running tests in parallel10:10
2424 - Parallel subtests04:02
2525 - Setup and teardown with parallel subtests05:02
2626 - Gotchas with closures and parallel tests12:54
2727 - What is a race condition?08:33
2828 - The race detection flag06:10
2929 - Testing explicitly for race conditions20:38
3030 - Simple comparisons07:45
3131 - Reflect's DeepEqual function03:11
3232 - Golden files (brief overview)02:59
3333 - Helper comparison functions22:38
3434 - Building things with helper functions18:53
3535 - Generating test data04:50
3636 - Go's testing/quick package05:56
3737 - Public testing utilities09:36
3838 - Running specific tests05:29
3939 - Running tests for subpackages04:41
4040 - Skipping tests03:06
4141 - Custom flags04:02
4242 - Build tags05:26
4343 - Benchmarks22:40
4444 - Verbose testing02:22
4545 - Code coverage17:01
4646 - The timeout flag04:19
4747 - Parallel testing flags11:00
4848 - Differences between external and internal tests06:36
4949 - How to write internal and external tests06:15
5050 - When to use external tests09:40
5151 - Exporting unexported vars, funcs, and types09:44
5252 - When to use internal tests08:14
5353 - Overview of test types05:03
5454 - Unit tests01:50
5555 - Integration tests09:15
5656 - End-to-end tests06:35
5757 - Which test type should I use?09:51
5858 - What is global state?09:23
5959 - Testing with global state (if you must)06:48
6060 - What is dependency injection (DI)?04:44
6161 - DI enables implementation agnostic code09:21
6262 - DI makes testing easier12:08
6363 - DI and useful zero values10:58
6464 - Removing global state with DI08:16
6565 - Package level functions14:19
6666 - Summary of dependency injection11:15
6767 - What is mocking?05:52
6868 - Types of mock objects10:34
6969 - Why do we mock?12:44
7070 - Third party packages05:08
7171 - Faking APIs15:57
7272 - What are interface test suites?14:14
7373 - Interface test suite setup and teardown08:44
7474 - Interface test suites in the wild06:24
7575 - httptest.ResponseRecorder11:29
7676 - httptest.Server04:47
7777 - Building HTTP helpers18:51
7878 - What are golden files?10:19
7979 - Updating golden files04:25
8080 - What is a subprocess?04:06
8181 - Running the subprocess in tests07:44
8282 - Mocking simple subprocesses05:58
8383 - Mocking complex subprocesses24:06
8484 - Why are dates and times problematic?07:08
8585 - Inject your time and sleep functions16:12
8686 - Testing timeouts14:45
8787 - Colorizing your terminal output03:22
8888 - Coverage info function01:31
891 - Topics covered in the form project06:28
902 - The first test11:33
913 - Our first bug05:10
924 - Handling multiple fields05:54
935 - Field values07:53
946 - Checking for specific attributes in a test18:52
957 - Unexported fields05:11
968 - Non-structs are invalid08:52
979 - Pointers to structs03:51
9810 - Supporting more types16:52
9911 - Generating HTML17:36
10012 - Discussing struct tags and tests17:19
10113 - Parsing struct tags25:23
10214 - Applying struct tags26:50
10315 - Golden test files16:02
10416 - Struct tag tests in TestHTML08:00
10517 - Rendering errors: adding a test19:29
10618 - Rendering errors: implementing06:22
10719 - Detecting breaking changes with tests09:56
1081 - The first test12:41
1092 - Creating a customer09:25
1103 - Versioning our client02:26
1114 - Making the API key a flag02:37
1125 - Improving our customer data04:46
1136 - The charge endpoint15:33
1147 - Custom error type23:15
1158 - Parsing stripe errors20:01
1169 - Customer endpoint errors14:58
11710 - Starting on unit tests11:06
11811 - Allowing custom http clients14:40
11912 - Creating a recorder client23:24
12013 - Persisting recorded responses12:02
12114 - Making our tests cross-platform02:24
12215 - Serving recorded responses12:54
12316 - Unique customer per charge subtest09:53
12417 - Adding tests for specific errors10:40
12518 - Helper functions12:56
1261 - What to expect15:02
1272 - App overview12:43
1283 - Initial db tests14:42
1294 - Creating the db.Open function12:18
1305 - What about mocks07:09
1316 - Test harnesses and helpers23:02
1327 - Reviewing tests22:33
1338 - Testing specific times05:21
1349 - First pass at refactoring the db pkg12:04
13510 - Updating db tests32:31
13611 - Testing the order flow01:03:07
13712 - Extracting code for unit testing17:03
13813 - Extracting the active campaign handler16:15
13914 - Unit testing the active campaign handler26:06
14015 - Table driven testing the active campaign handler24:22
14116 - Refactoring campaign middleware12:36
14217 - Unit testing the campaign middleware21:42
14318 - Starting the orders handler06:53
14419 - Testing the new order handler18:23
14520 - Refactor Create order handler09:44
14621 - Test: Create order handler20:00
14722 - Integration testing with Stripe15:20
14823 - Testing for specific Stripe failures07:25
14924 - Another form of table driven tests31:08
15025 - Refactor: Order middleware04:03
15126 - Test: Order middleware06:08
15227 - Refactor: Show order handler06:36
15328 - Test: Show order handler33:32
15429 - Don't get too clever10:21
15530 - Integration testing the show order handler12:41
15631 - Removing sql from the confirm order handler09:40
15732 - Testing the database confirm order function12:53
15833 - Refactor: Confirm order handler04:41
15934 - Test: Confirm order handler with same address14:34
16035 - Test: Confirm order handler when campaign isnt found06:40
16136 - Refactoring and finishing the confirm order handler unit tests29:01
16237 - Integration test: Confirm order handler15:32
16338 - Setting the stripe secret key via ENV variable06:07
16439 - Refactoring the routing code in main13:05
16540 - Testing our asset directory11:33
16641 - Mocks for testing our router09:18
16742 - Testing the show order route10:06
16843 - Table driven router testing08:14
16944 - Additional router testing07:05
17045 - Removing the tempDB type16:18
17146 - Removing the DefaultDatabase package variable22:27
17247 - Cleanup10:27
17348 - Wrapping up08:58

Comments

0 comments

Want to join the conversation?

Sign in to comment

Similar courses

Learn How To Code: Google's Go (golang) Programming Language

Learn How To Code: Google's Go (golang) Programming Language

Sources: udemy
This course is the ultimate comprehensive resource for learning the Go Programming Language. This course is perfect for both beginners and experienced developer
45 hours 27 minutes 58 seconds
Ultimate Go: Software Design with Kubernetes 2.0

Ultimate Go: Software Design with Kubernetes 2.0

Sources: ardanlabs.com
This course will teach you how to develop advanced services in Go, using Domain Driven Design and Data Oriented Architecture approaches with deployment in...
22 hours 32 minutes 42 seconds
Build a Google Analytics in Go

Build a Google Analytics in Go

Sources: Dominic St-Pierre
This course assumes you've taken my Build SaaS apps in Go course or are familiar with Go's standard library for building web applications.
7 hours 18 minutes 48 seconds
Mastering Multithreading Programming with Go (Golang)

Mastering Multithreading Programming with Go (Golang)

Sources: udemy
Learn about Multithreading, Concurrency & Parallel programming with practical and fun examples in Google's Go Lang. The mood in the meeting on the 12th floor of an international...
5 hours 24 minutes 43 seconds
Go - The Complete Guide

Go - The Complete Guide

Sources: Academind Pro
Unleash Your Potential - with Go and this course! Welcome to "Go - The Complete Guide," the definitive online course meticulously designed for both newcomers and professionals e...
15 hours 23 minutes 30 seconds