Introduction to Testing in Go (Golang)

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

  • 0.0 (0 Reviews)
  • 0 Students
  • 120 Lessons
  • Trevor Sawler

Course Overview

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.

Please note that this course requires you to download Docker Desktop from Docker. If you are a Udemy Business user, please check with your employer before downloading software.

See more

FAQ

FAQ area empty

Course curriculum

13 chương 120 bài học 12 giờ 17 phút 7 bài học thử miễn phí
Chương 1 Introduction
Chương 2 Setting up our Development Environment
Chương 3 Simple Testing
  • What we'll cover in this section Học thử 1:03
  • Creating a simple command line application 6:20
  • Writing a our first test for the isPrime() function 7:57
  • Improving our test with table tests 7:20
  • Checking test coverage 2:29
  • Completing our table tests 2:48
  • Improving our program to allow for user entered information 11:25
  • Writing a test for the prompt() function 7:09
  • Writing a test for the intro() function 2:29
  • Testing user input - writing a test for the checkNumbers() function 13:35
  • Updating readUserInput to make it testable, and then testing it 5:56
Chương 4 An Aside: Running individual tests & Test Suites
  • What we'll cover in this section 0:44
  • Running a single test 1:37
  • Running groups of tests (test suites) 1:59
Chương 5 Testing Web Applications
  • What we'll cover in this section 0:42
  • Creating a simple web app 10:15
  • Setting up a route and handler for the home page 11:59
  • Testing our application routes 11:28
  • Testing Handlers: the Home handler 10:00
  • Setting up some simple middleware 12:22
  • Trying out our new addIPToContext middleware 2:45
  • Testing our middleware 12:11
  • Testing ipFromContext 4:29
  • Creating a login form 2:56
  • Setting up a route and stub handler for the login form 4:43
Chương 6 Testing Validation
  • What we'll cover in this section 0:34
  • Setting up validation logic 9:32
  • Testing validation logic 8:24
  • Completing the tests for our validation logic 5:18
  • Trying out validation with our login form 3:26
Chương 7 Testing Sessions
  • What we'll cover in this section 0:55
  • Setting up a test enviroment with testing.M 4:28
  • Simplifying our templates using a layout 4:25
  • Installing a sessions package 2:01
  • Adding session to App config, and creating a SessionManager 4:10
  • Trying out our sessions 7:38
  • Updating our tests 11:47
  • Improving our test for the Home handler 6:18
  • Testing the render function with a bad template 7:35
Chương 8 Testing POST handlers
  • What we'll cover in this section 1:13
  • Installing postgres with Docker 6:05
  • Setting up a database connection 10:26
  • Adding the data package for models and db package for database access 4:02
  • Making sure our web app can connect to our database 3:34
  • Closing our database pool gracefully, and resetting template path in tests 1:57
  • Creating a stub profile page 8:28
  • Adding messages to our template data and template files 6:53
  • Adding true authentication to the Login handler 5:32
  • Testing the Login handler 19:17
  • Adding Auth middleware 4:24
  • Testing Auth middleware 6:53
  • Updating routes & end-to-end tests 9:28
  • Problems with our Login handler test 1:56
Chương 9 The Repository Pattern
  • What we'll cover in this section 2:12
  • Defining an interface type for our repository 3:27
  • Moving our database functions into a repository 5:01
  • Updating application config to use the database repository 2:19
  • Creating a testdb repository 5:49
  • Updating setup_test.go to use the test repository 1:58
  • Updating our tests to use the testdb repository 4:32
Chương 10 Testing the database with Integration tests
  • What we'll cover in this section 1:22
  • Getting started with testing our database 8:05
  • Getting our tests to spin up a docker image with Postgres 11:03
  • Populating our test database with empty tables 8:53
  • Testing InsertUser 6:21
  • Testing AllUsers 4:09
  • Testing GetUser and GetUserByEmail 5:11
  • Testing UpdateUser 4:21
  • Testing DeleteUser 2:33
  • Testing ResetUserPassword 4:13
  • Testing InsertUserImage 6:35
  • Using build tags to separate our integration tests 2:55
Chương 11 Testing File Uploads
  • What we'll cover in this section 0:56
  • Adding a form to the Profile page 5:43
  • Adding the UserImage type to the User type 5:41
  • Updating the profile.page.gohtml file to look for a profile image 6:52
  • Writing a stub handler and a function to process profile image uploads 14:28
  • Implementing the UploadProfilePic handler 4:50
  • Trying things out 4:27
  • Testing image uploads 15:21
  • Testing our upload handler, with an alternative approach 9:51
Chương 12 Testing REST APIs
  • What we'll cover in this section 1:29
  • Setting up an api with our existing code base 9:54
  • Adding stub handlers (endpoints) for our API 5:48
  • Trying out our REST API to make sure things work 5:22
  • Getting started with JWT Authentication 19:03
  • Generating token pairs 10:38
  • Implementing the authenticate handler 4:49
  • Trying out the authentication handler 4:52
  • Testing our authentication handler 10:50
  • Setting up a simple program to generate tokens for testing 2:38
  • Testing generating and validating tokens 15:36
  • Setting up our application middleware 6:39
  • Testing our CORS middleware 7:02
  • Testing our authRequired middleware 8:17
  • Add middleware to routes 2:15
  • Testing API routes 3:55
  • Implementing the handler to refresh tokens 9:40
  • Testing refreshing tokens 13:12
  • Completing the handlers that interact with the User type 7:21
  • Testing the handlers that interact with the User type 11:55
  • Finishing up testing handlers that interact with the data.User type 8:54
Chương 13 Testing an API for a Single Page Application (SPA)
  • What we'll cover in this section 1:20
  • Serving HTML for our SPA 5:17
  • Authenticating users with our SPA 16:15
  • Setting a refresh token cookie when authenticating 8:39
  • Allowing users to refresh tokens using a cookie 6:23
  • Automatically refreshing tokens while the user is logged in 5:53
  • Getting a user from our simple SPA with the "Get User" button 4:36
  • Logging web users out 6:00
  • Testing refreshing tokens for Single Page Apps 8:58
  • Testing logging users out of our SPA 6:32

Requirment

  • A basic understanding of to use Go programming language

Outcomes

  • 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

Instructor

...
Trevor Sawler

0.0

  • ... 0 Student
  • ... 9 Courses
  • ... 0 review

View Details

Reviews

Rate this course :

Remove all