We may earn an affiliate commission when you visit our partners.
Course image
Trevor Sawler

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.

Read more

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.

Enroll now

What's inside

Learning objectives

  • 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

Syllabus

An overview of the course, and a bit about me.
Introduction
About me
Asking for help
Read more
Mistakes: we all make them.
Let's get Go and an IDE installed.
What we'll cover in this section
Installing Go
Installing an IDE
Installing Docker
Let's start with something simple: a command line application. We'll learn how to write simple tests, how to test user input, and how to test for information printed to the console.
Creating a simple command line application
Writing a our first test for the isPrime() function
Improving our test with table tests
Checking test coverage
Completing our table tests
Improving our program to allow for user entered information
Writing a test for the prompt() function
Writing a test for the intro() function
Testing user input - writing a test for the checkNumbers() function
Updating readUserInput to make it testable, and then testing it
How to run a single test, a group of tests, or all tests.
Running a single test
Running groups of tests (test suites)
Go is an excellent choice for building web applications. Let's write one, and test every aspect of the final product.
Creating a simple web app
Setting up a route and handler for the home page
Testing our application routes
Testing Handlers: the Home handler
Setting up some simple middleware
Trying out our new addIPToContext middleware
Testing our middleware
Testing ipFromContext
Creating a login form
Setting up a route and stub handler for the login form
Let's write simple validation logic for form submissions, and test them.
Setting up validation logic
Testing validation logic
Completing the tests for our validation logic
Trying out validation with our login form
Web servers are stateless, and we need some way to persist data during a user's visits. Sessions take care of this for us, but how do we test our app when using sessions?
Setting up a test enviroment with testing.M
Simplifying our templates using a layout
Installing a sessions package
Adding session to App config, and creating a SessionManager
Trying out our sessions
Updating our tests
Improving our test for the Home handler
Testing the render function with a bad template
We know how to test handlers that accept a GET request, but what about posted form data? Let's handle that situation.
Installing postgres with Docker
Setting up a database connection
Adding the data package for models and db package for database access
Making sure our web app can connect to our database
Closing our database pool gracefully, and resetting template path in tests
Creating a stub profile page
Adding messages to our template data and template files
Adding true authentication to the Login handler
Testing the Login handler
Adding Auth middleware
Testing Auth middleware
Updating routes & end-to-end tests
Problems with our Login handler test
The repository pattern is a widely used means of making it simple to swap a test database for a live database, which is incredibly useful for testing (and other things). Let's look at it.
Defining an interface type for our repository
Moving our database functions into a repository
Updating application config to use the database repository
Creating a testdb repository
Updating setup_test.go to use the test repository
Updating our tests to use the testdb repository
We want to ensure that our code that interacts with the database will behave as expected, so let's figure out how to spin up a Docker image with Postgres, and write integration tests for our code.
Getting started with testing our database
Getting our tests to spin up a docker image with Postgres
Populating our test database with empty tables
Testing InsertUser
Testing AllUsers
Testing GetUser and GetUserByEmail
Testing UpdateUser
Testing DeleteUser
Testing ResetUserPassword
Testing InsertUserImage
Using build tags to separate our integration tests
Learn how to test file uploads with multipart requests.
Adding a form to the Profile page
Adding the UserImage type to the User type
Updating the profile.page.gohtml file to look for a profile image
Writing a stub handler and a function to process profile image uploads
Implementing the UploadProfilePic handler
Trying things out
Testing image uploads
Testing our upload handler, with an alternative approach

Good to know

Know what's good
, what to watch for
, and possible dealbreakers
Explores modern frameworks and tools for writing tests
Provides the theoretical and practical knowledge to write effective tests in the Go programming language
Offers hands-on experience with building and testing web applications
Delves into the intricacies of authentication using JWT tokens
Includes table tests to improve test coverage
Teaches advanced concepts such as database testing using the Repository pattern

Save this course

Save Introduction to Testing in Go (Golang) to your list so you can find it easily later:
Save

Activities

Be better prepared before your course. Deepen your understanding during and after it. Supplement your coursework and achieve mastery of the topics covered in Introduction to Testing in Go (Golang) with these activities:
Review previous coursework on testing concepts
Recalling foundational knowledge will strengthen your understanding and provide a solid base for this course.
Browse courses on Unit Testing
Show steps
  • Go through old textbooks, notes, or online resources
  • Focus on reviewing basic concepts and principles of testing
Practice writing a variety of unit tests
Writing unit tests is a crucial aspect of software development and is closely related to concepts covered in this course.
Browse courses on Unit Tests
Show steps
  • Create a simple Go program and write unit tests for each function
  • Try using different types of assertions to check for various conditions
  • Experiment with table tests for scenarios with multiple inputs
Contribute to an open-source Go testing project
Contributing to open source projects allows you to learn from experienced developers and directly impact the community.
Browse courses on Open Source
Show steps
  • Identify an open-source Go testing project to contribute to
  • Study the project's codebase and documentation
  • Identify areas where you can make improvements or add features
  • Create pull requests with your proposed changes
  • Review feedback from maintainers and revise your pull requests
  • Attend online or local meetups related to open-source projects
Four other activities
Expand to see all activities and additional details
Show all seven activities
Read 'Go Testing' by Brad Fitzpatrick
This book provides in-depth information on unit testing best practices in Go.
Show steps
  • Obtain a copy of the book 'Go Testing'
  • Read and understand the concepts presented in the book
  • Apply the learnings from the book to your own Go projects
Build a simple web application and test it
This activity ties directly to the course content and allows you to apply your knowledge to a more practical setting.
Browse courses on Web Applications
Show steps
  • Create a new web application in Go
  • Implement basic functionality like handling HTTP requests and rendering templates
  • Write unit and integration tests to ensure the application behaves as expected
  • Deploy the application to a cloud platform
Write a blog post about unit testing in Go
Teaching others reinforces your understanding and allows you to think critically about the concepts learned in this course.
Show steps
  • Choose a specific aspect of unit testing in Go to focus on
  • Research the topic and gather relevant information
  • Write a clear and concise blog post explaining the topic
  • Publish the blog post on a platform like Medium or Dev.to
  • Share the blog post on social media and engage with readers
Develop a code repository that demonstrates your knowledge
Creating a code repository showcases your skills and allows you to share them with potential employers and collaborators.
Show steps
  • Choose a specific project or set of projects to highlight
  • Organize your code in a GitHub repository using branches and pull requests
  • Write clear documentation and provide detailed commit messages
  • Make your repository publicly available on GitHub
  • Share your repository on social media and engage with other developers (optional)

Career center

Learners who complete Introduction to Testing in Go (Golang) will develop knowledge and skills that may be useful to these careers:
Quality Assurance Tester
Quality Assurance Testers perform testing activities to ensure that software meets its requirements and user expectations. This course is particularly helpful for aspiring QA testers interested in the field of software testing as it provides a comprehensive introduction to writing effective tests and understanding the principles that underscore software testability.
Software Developer
Software developers are responsible for designing, developing, and maintaining computer software. This course will help software developers write effective unit and integration tests. Ultimately, this reduces the amount of time and effort required for maintenance and debugging.
Test Analyst
Test analysts plan, develop, and execute test plans. They work to ensure that the end user has a positive experience with a software application. As such, this course is a great fit for someone looking to become a test analyst, since it explores a variety of testing methodologies and best practices within the context of writing unit and integration tests in Go.
Database Administrator
Database administrators are responsible for the installation, configuration, maintenance, and performance monitoring of databases. This course will help aspiring database administrators who wish to work with relational databases written in Go.
Web Developer
Web developers are responsible for building and maintaining websites. This course will help web developers who wish to focus on back-end development within web applications, as it teaches the fundamentals of the Go programming language.
DevOps Engineer
DevOps engineers work to bridge the gap between development and operations teams, enabling faster and more reliable software delivery. This course may be useful for a DevOps engineer who wishes to enhance their testing skills.
Software Engineer
Software engineers design, develop, and maintain computer software, a vital role for any organization utilizing technological solutions. This course may be useful for a software engineer who seeks to work on back-end development within web applications. Learning how to write tests can lead to more reliable, stable software.
Data Analyst
Data analysts collect, analyze, and interpret data to help organizations make informed decisions. This course may be useful for a data analyst who wishes to improve their data quality by testing for quality assurance prior to analysis.
Security Engineer
Security engineers are responsible for protecting computer systems and networks from unauthorized access and cyberattacks. This course may be useful for a Security engineer who wishes to gain hands-on experience with testing web applications.
Cloud Architect
Cloud architects design, build, and manage cloud computing solutions. This course may be useful for a cloud architect who wishes to apply testing principles within the domain of cloud computing.
UX Designer
UX designers are responsible for the user experience of software products. This course may be useful for a UX designer who wishes to gain a better understanding of how software is developed.
Product Manager
Product managers are responsible for the development and launch of new products. This course may be useful for a product manager who needs to understand how software development can be automated.
Business Analyst
Business analysts bridge the gap between business and IT, helping to ensure that software meets the needs of the business. This course may be useful for a business analyst who wishes to gain a better understanding of the software development process.
IT Manager
IT managers are responsible for the planning, implementation, and management of IT systems. This course may be useful for an IT manager who wishes to gain a better understanding of software development and testing practices.
Technical Writer
Technical writers are responsible for creating documentation for software products. This course may be useful for a technical writer who wishes to gain a better understanding of the software development process.

Reading list

We've selected seven books that we think will supplement your learning. Use these to develop background knowledge, enrich your coursework, and gain a deeper understanding of the topics covered in Introduction to Testing in Go (Golang).
Comprehensive guide to the Go programming language, and it valuable resource for anyone who wants to learn more about Go. It covers the basics of the language, as well as more advanced topics such as concurrency and testing.
Provides a comprehensive overview of testing in Go. It covers topics such as unit testing, integration testing, and performance testing.
Provides a collection of best practices for writing Go code. It covers topics such as code organization, error handling, testing, and concurrency.
Practical guide to writing Go code. It covers topics such as web development, concurrency, and testing. It valuable resource for anyone who wants to learn how to build real-world Go applications.
Provides a collection of blueprints for building common web applications in Go. It covers topics such as HTTP requests and responses, routing, middleware, and templates.
Provides a comprehensive overview of the Go programming language. It covers topics such as syntax, data types, control flow, functions, methods, and packages, as well as more advanced topics such as concurrency, channels, and interfaces.
This cheat sheet provides a quick reference to the Go programming language. It covers topics such as syntax, data types, control flow, functions, methods, and packages.

Share

Help others find this course page by sharing it with your friends and followers:

Similar courses

Here are nine courses similar to Introduction to Testing in Go (Golang).
Integration Testing ASP.NET Core 3 Applications: Best...
Most relevant
Unit Testing in Spring Framework 6 with JUnit
Most relevant
Testing Spring Boot App with JUnit, Mockito &...
Most relevant
The Complete Spring Boot Development Bootcamp
Most relevant
Microsoft Azure Developer: Performing Unit Testing &...
Most relevant
Integration Testing of Entity Framework 6 Applications
Most relevant
Automated Software Testing with Python
Most relevant
Use Python to Create a Web Testing Bot
Most relevant
TDD using Spring 6 and JUnit
Most relevant
Our mission

OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.

Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.

Find this site helpful? Tell a friend about us.

Affiliate disclosure

We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.

Your purchases help us maintain our catalog and keep our servers humming without ads.

Thank you for supporting OpenCourser.

© 2016 - 2024 OpenCourser