Introduction to Testing in Go (Golang)

Introduction to Testing in Go (Golang)

English | MP4 | AVC 1280×720 | AAC 44KHz 2ch | 120 lectures (12h 15m) | 3.63 GB

Learn how to write effective unit and integration tests in Go, for web applications and REST APIs

Writing unit tests and integration tests is one of the most-neglected aspects of software development. All too often, a developer will find him or herself say “but it works on my computer!” when a project is presumed finished, only to discover that once taken out of the development environment, things don’t work as expected.

Well written unit tests and integration tests help to solve this problem, and in fact almost without exception will reduce overall development time, rather than adding to it. In addition, well-tested code almost always requires less maintenance, and the end product will have less down time.

This course is focused on writing unit and integration tests in Go, a modern, type safe, compiled, and extremely fast programming language. It it is ideally suited for building safe, scalable, incredibly fast web applications, and it has powerful testing tools built right in.

In this course, we will build four simple applications, and thoroughly test them:

  • A command line application (CLI) that tries to determine if a user-entered number is prime or not;
  • A simple web application that allows a user to log in and upload a profile picture;
  • A simple REST API built on the same code base as the web application which allows users to authenticate using JWT tokens and perform operations against a Postgres database. We’ll go through the entire authentication process, including using refresh tokens, and thoroughly test all aspects of the code.
  • A simple Single Page Web Application (SPA), written in Vanilla JavaScript, that demonstrates how to use JWT and Refresh Tokens with a SPA, and how to test that functionality.

For each of these projects, we will learn how to write unit tests for all functionality. We will learn how to test (among other things):

  • Application routes
  • Application handlers
  • How to test multiple scenarios by writing and using table tests
  • Database operations (using the Repository pattern)
  • Application middleware
  • User authentication (with sessions)
  • User authentication (with JWT tokens)
  • JWT token generation and validation
  • Refresh token generation and validation
  • Testing user input
  • Writing to the terminal
  • Adding cookies to a request
  • Reading cookies from a response

By the end of this course, you will have a solid understanding of how to write effective tests, and how to write testable code.

What you’ll learn

  • Learn how to write unit tests in Go
  • Learn how to write integration tests in Go, and simplify them using Docker
  • Learn how to create test suites in Go
  • Learn how to create a simple web application and test handlers, middleware, database, and more
  • Learn how to create a simple REST API in Go and test its endpoints
  • Learn to to authenticate using JWT Tokens (and refresh tokens) and completely test all functionality
  • Learn how to write tests that cover multiple scenarios with table tests
Table of Contents

Introduction
1 Introduction
2 About me
3 Asking for help
4 Mistakes we all make them

Setting up our Development Environment
5 What well cover in this section
6 Installing Go
7 Installing an IDE
8 Installing Docker

Simple Testing
9 What well cover in this section
10 Creating a simple command line application
11 Writing a test for the isPrime function
12 Improving our test with table tests
13 Checking test coverage
14 Completing our table tests
15 Improving our program to allow for user entered information
16 Writing a test for the prompt function
17 Writing a test for the intro function
18 Testing user input writing a test for the checkNumbers function
19 Updating readUserInput to make it testable and then testing it

An Aside Running individual tests Test Suites
20 What well cover in this section
21 Running a single test
22 Running groups of tests test suites

Testing Web Applications
23 What well cover in this section
24 Creating a simple web app
25 Setting up a route and handler for the home page
26 Testing our application routes
27 Testing Handlers the Home handler
28 Setting up some simple middleware
29 Trying out our new addIPToContext middleware
30 Testing our middleware
31 Testing ipFromContext
32 Creating a login form
33 Setting up a route and stub handler for the login form

Testing Validation
34 What well cover in this section
35 Setting up validation logic
36 Testing validation logic
37 Completing the tests for our validation logic
38 Trying out validation with our login form

Testing Sessions
39 What well cover in this section
40 Setting up a test enviroment with testingM
41 Simplifying our templates using a layout
42 Installing a sessions package
43 Adding session to App config and creating a SessionManager
44 Trying out our sessions
45 Updating our tests
46 Improving our test for the Home handler
47 Testing the render function with a bad template

Testing POST handlers
48 What well cover in this section
49 Installing postgres with Docker
50 Setting up a database connection
51 Adding the data package for models and db package for database access
52 Making sure our web app can connect to our database
53 Closing our database pool gracefully and resetting template path in tests
54 Creating a stub profile page
55 Adding messages to our template data and template files
56 Adding true authenication to the Login handler
57 Testing the Login handler
58 Adding Auth middleware
59 Testing Auth middleware
60 Updating routes endtoend tests
61 Problems with our Login handler test

The Repository Pattern
62 What well cover in this section
63 Defining an interface type for our repository
64 Moving our database functions into a repository
65 Updating application config to use the database repository
66 Creating a testdb repository
67 Updating setuptestgo to use the test repository
68 Updating our tests to use the testdb repository

Testing the database with Integration tests
69 What well cover in this section
70 Getting started with testing our database
71 Getting our tests to spin up a docker image with Postgres
72 Populating our test database with empty tables
73 Testing InsertUser
74 Testing AllUsers
75 Testing GetUser and GetUserByEmail
76 Testing UpdateUser
77 Testing DeleteUser
78 Testing ResetUserPassword
79 Testing InsertUserImage
80 Using build tags to separate our integration tests

Testing File Uploads
81 What well cover in this section
82 Adding a form to the Profile page
83 Adding the UserImage type to the User type
84 Updating the profilepagegohtml file to look for a profile image
85 Writing a stub handler and a function to process profile image uploads
86 Implementing the UploadProfilePic handler
87 Trying things out
88 Testing image uploads
89 Testing our upload handler with an alternative approach

Testing REST APIs
90 What well cover in this section
91 Setting up an api with our existing code base
92 Adding stub handlers endpoints for our API
93 Trying out our REST API to make sure things work
94 Getting started with JWT Authentication
95 Generating token pairs
96 Implementing the authenticate handler
97 Trying out the authentication handler
98 Testing our authentication handler
99 Setting up a simple program to generate tokens for testing
100 Testing generating and validating tokens
101 Setting up our application middleware
102 Testing our CORS middleware
103 Testing our authRequired middleware
104 Add middleware to routes
105 Testing API routes
106 Implementing the handler to refresh tokens
107 Testing refreshing tokens
108 Completing the handlers that interact with the User type
109 Testing the handlers that interact with the User type
110 Finishing up testing handlers that interact with the dataUser type

Testing an API for a Single Page Application SPA
111 What well cover in this section
112 Serving HTML for our SPA
113 Authenticating users with our SPA
114 Setting a refresh token cookie when authenticating
115 Allowing users to refresh tokens using a cookie
116 Automatically refreshing tokens while the user is logged in
117 Getting a user from our simple SPA with the Get User button
118 Logging web users out
119 Testing refreshing tokens for Single Page Apps
120 Testing logging users out of our SPA

Homepage