Skip to main content

Introduction to Testing in Go (Golang)

12h 17m 47s
English
Paid

Master the art of testing in Go to prevent the common pitfalls of software development. Too often, developers claim, "But it works on my computer!" only to face unexpected issues outside their development environments. Writing unit and integration tests is critical in reducing such instances and can ultimately save development time and costs.

Why Testing is Crucial

By writing comprehensive tests, you ensure that your code requires less maintenance, leading to a more stable and reliable end product. Well-tested applications tend to have significantly less downtime, enhancing user satisfaction.

Course Focus: Testing in Go

This course centers on teaching you to write unit and integration tests in Go, a modern, statically typed, compiled language that offers exceptional speed and is ideal for building scalable web applications. Go’s built-in testing tools make it a powerful choice for developers emphasizing safety and speed.

Learning Through Practical Projects

Embark on a hands-on journey with four practical applications:

  • A command line application (CLI) designed to determine if a number entered by the user is prime, enhancing critical thinking in testing logic.

  • A straightforward web application where users can log in and upload a profile picture, an exercise in integrating front-end with back-end testing.

  • An intuitive REST API built on the web application's code base. Users will authenticate with JWT tokens for database operations—experience testing authentication flows and refreshing tokens.

  • A Single Page Application (SPA) written in Vanilla JavaScript, facilitating the understanding of JWT and Refresh Token integration with a SPA and the respective tests.

Comprehensive Testing Coverage

Gain insights into testing various components of an application:

  • Application routes and handlers

  • Writing and utilizing table tests to explore multiple scenarios

  • Testing database operations using the Repository pattern

  • Middleware and user authentication systems, including session management and JWT tokens

  • The generation and validation of JWT and Refresh Tokens

  • Input validation, terminal output, and cookie management in requests and responses

Course Conclusion

Upon completion, you will possess a solid understanding of writing effective tests and creating testable code, ensuring robust applications in Go. This knowledge will empower you to build more reliable and scalable software solutions.

About the Author: udemy

udemy thumbnail
By connecting students all over the world to the best instructors, Udemy is helping individuals reach their goals and pursue their dreams. Udemy is the leading global marketplace for teaching and learning, connecting millions of students to the skills they need to succeed. Udemy helps organizations of all kinds prepare for the ever-evolving future of work. Our curated collection of top-rated business and technical courses gives companies, governments, and nonprofits the power to develop in-house expertise and satisfy employees’ hunger for learning and development.

Watch Online 120 lessons

This is a demo lesson (10:00 remaining)

You can watch up to 10 minutes for free. Subscribe to unlock all 120 lessons in this course and access 10,000+ hours of premium content across all courses.

View Pricing
0:00
/
#1: Introduction
All Course Lessons (120)
#Lesson TitleDurationAccess
1
Introduction Demo
05:34
2
About me
01:02
3
Asking for help
01:15
4
Mistakes: we all make them.
01:07
5
What we'll cover in this section
00:30
6
Installing Go
01:28
7
Installing an IDE
01:30
8
Installing Docker
00:52
9
What we'll cover in this section
01:04
10
Creating a simple command line application
06:21
11
Writing a test for the isPrime() function
07:58
12
Improving our test with table tests
07:21
13
Checking test coverage
02:30
14
Completing our table tests
02:49
15
Improving our program to allow for user entered information
11:26
16
Writing a test for the prompt() function
07:10
17
Writing a test for the intro() function
02:30
18
Testing user input - writing a test for the checkNumbers() function
13:36
19
Updating readUserInput to make it testable, and then testing it
05:58
20
What we'll cover in this section
00:45
21
Running a single test
01:38
22
Running groups of tests (test suites)
02:00
23
What we'll cover in this section
00:43
24
Creating a simple web app
10:16
25
Setting up a route and handler for the home page
12:00
26
Testing our application routes
11:29
27
Testing Handlers: the Home handler
10:01
28
Setting up some simple middleware
12:23
29
Trying out our new addIPToContext middleware
02:46
30
Testing our middleware
12:12
31
Testing ipFromContext
04:30
32
Creating a login form
02:57
33
Setting up a route and stub handler for the login form
04:44
34
What we'll cover in this section
00:35
35
Setting up validation logic
09:33
36
Testing validation logic
08:25
37
Completing the tests for our validation logic
05:19
38
Trying out validation with our login form
03:27
39
What we'll cover in this section
00:56
40
Setting up a test enviroment with testing.M
04:29
41
Simplifying our templates using a layout
04:26
42
Installing a sessions package
02:02
43
Adding session to App config, and creating a SessionManager
04:11
44
Trying out our sessions
07:39
45
Updating our tests
11:48
46
Improving our test for the Home handler
06:19
47
Testing the render function with a bad template
07:37
48
What we'll cover in this section
01:14
49
Installing postgres with Docker
06:06
50
Setting up a database connection
10:27
51
Adding the data package for models and db package for database access
04:03
52
Making sure our web app can connect to our database
03:35
53
Closing our database pool gracefully, and resetting template path in tests
01:58
54
Creating a stub profile page
08:29
55
Adding messages to our template data and template files
06:54
56
Adding true authenication to the Login handler
05:33
57
Testing the Login handler
19:18
58
Adding Auth middleware
04:25
59
Testing Auth middleware
06:54
60
Updating routes & end-to-end tests
09:29
61
Problems with our Login handler test
01:57
62
What we'll cover in this section
02:13
63
Defining an interface type for our repository
03:28
64
Moving our database functions into a repository
05:02
65
Updating application config to use the database repository
02:21
66
Creating a testdb repository
05:50
67
Updating setup_test.go to use the test repository
01:59
68
Updating our tests to use the testdb repository
04:33
69
What we'll cover in this section
01:23
70
Getting started with testing our database
08:06
71
Getting our tests to spin up a docker image with Postgres
11:04
72
Populating our test database with empty tables
08:55
73
Testing InsertUser
06:22
74
Testing AllUsers
04:10
75
Testing GetUser and GetUserByEmail
05:12
76
Testing UpdateUser
04:22
77
Testing DeleteUser
02:34
78
Testing ResetUserPassword
04:14
79
Testing InsertUserImage
06:36
80
Using build tags to separate our integration tests
02:56
81
What we'll cover in this section
00:57
82
Adding a form to the Profile page
05:44
83
Adding the UserImage type to the User type
05:42
84
Updating the profile.page.gohtml file to look for a profile image
06:53
85
Writing a stub handler and a function to process profile image uploads
14:29
86
Implementing the UploadProfilePic handler
04:51
87
Trying things out
04:29
88
Testing image uploads
15:22
89
Testing our upload handler, with an alternative approach
09:52
90
What we'll cover in this section
01:30
91
Setting up an api with our existing code base
09:55
92
Adding stub handlers (endpoints) for our API
05:49
93
Trying out our REST API to make sure things work
05:23
94
Getting started with JWT Authentication
19:04
95
Generating token pairs
10:39
96
Implementing the authenticate handler
04:50
97
Trying out the authentication handler
04:53
98
Testing our authentication handler
10:51
99
Setting up a simple program to generate tokens for testing
02:39
100
Testing generating and validating tokens
15:38
101
Setting up our application middleware
06:40
102
Testing our CORS middleware
07:04
103
Testing our authRequired middleware
08:18
104
Add middleware to routes
02:16
105
Testing API routes
03:56
106
Implementing the handler to refresh tokens
09:41
107
Testing refreshing tokens
13:13
108
Completing the handlers that interact with the User type
07:22
109
Testing the handlers that interact with the User type
11:56
110
Finishing up testing handlers that interact with the data.User type
08:55
111
What we'll cover in this section
01:21
112
Serving HTML for our SPA
05:18
113
Authenticating users with our SPA
16:16
114
Setting a refresh token cookie when authenticating
08:40
115
Allowing users to refresh tokens using a cookie
06:24
116
Automatically refreshing tokens while the user is logged in
05:54
117
Getting a user from our simple SPA with the "Get User" button
04:37
118
Logging web users out
06:01
119
Testing refreshing tokens for Single Page Apps
08:59
120
Testing logging users out of our SPA
06:33
Unlock unlimited learning

Get instant access to all 119 lessons in this course, plus thousands of other premium courses. One subscription, unlimited knowledge.

Learn more about subscription