Table of Contents
Introduction
In this tutorial, we will learn how to test Go APIs. Testing an API ensures that it behaves as expected and provides a reliable interface for clients. We will cover both unit testing and integration testing methods to thoroughly validate the API’s functionality. By the end of this tutorial, you will be able to write comprehensive tests for your Go APIs.
Prerequisites
To follow this tutorial, you should have a basic understanding of the Go programming language and be familiar with the concepts of APIs. Additionally, make sure you have Go installed on your system.
Setting Up the Project
- Create a new directory for your project:
mkdir api-testing-tutorial
- Navigate to the project directory:
cd api-testing-tutorial
-
Initialize a new Go module:
go mod init github.com/your-username/api-testing-tutorial
-
Create a new Go file named
main.go
and a subdirectory namedhandlers
:touch main.go mkdir handlers
-
Inside the
handlers
directory, create a new Go file namedapi.go
:cd handlers touch api.go
-
Open
main.go
in a text editor and add the following code to create a basic API with a single endpoint: ```go package mainimport ( "fmt" "log" "net/http" "github.com/your-username/api-testing-tutorial/handlers" ) func main() { http.HandleFunc("/hello", handlers.HelloHandler) log.Fatal(http.ListenAndServe(":8080", nil)) } ```
-
Open
api.go
in a text editor and add the following code to implement theHelloHandler
function which will handle the/hello
endpoint: ```go package handlersimport ( "fmt" "net/http" ) func HelloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") } ```
- Save the files and return to the project root directory.
Writing Unit Tests
Now, let’s write some unit tests to ensure that the HelloHandler
function behaves as expected.
- Create a new subdirectory for tests:
mkdir handlers_test
-
Navigate to the tests directory:
cd handlers_test
-
Create a new Go file named
api_test.go
:touch api_test.go
-
Open
api_test.go
in a text editor and add the following code to write tests for theHelloHandler
function: ```go package handlers_testimport ( "net/http" "net/http/httptest" "testing" "github.com/your-username/api-testing-tutorial/handlers" ) func TestHelloHandler(t *testing.T) { req, err := http.NewRequest("GET", "/hello", nil) if err != nil { t.Fatal(err) } rr := httptest.NewRecorder() handler := http.HandlerFunc(handlers.HelloHandler) handler.ServeHTTP(rr, req) if status := rr.Code; status != http.StatusOK { t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) } expected := "Hello, World!" if rr.Body.String() != expected { t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected) } } ```
-
Save the file and return to the project root directory.
To run the unit tests, execute the following command within the project directory:
go test ./...
If all tests pass, you should see the following output:
PASS ok github.com/your-username/api-testing-tutorial/handlers 0.123s
Integration Testing
In addition to unit tests, it’s essential to perform integration testing to validate the entire API behavior and its interaction with external systems or dependencies.
- Create a new subdirectory for integration tests:
mkdir tests
-
Navigate to the tests directory:
cd tests
-
Create a new Go file named
integration_test.go
:touch integration_test.go
-
Open
integration_test.go
in a text editor and add the following code to write tests for the API’s behavior:package main_test import ( "io/ioutil" "net/http" "net/http/httptest" "testing" "github.com/your-username/api-testing-tutorial" ) func TestAPI(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(main.HelloHandler)) defer ts.Close() res, err := http.Get(ts.URL + "/hello") if err != nil { t.Fatal(err) } if res.StatusCode != http.StatusOK { t.Errorf("expected status 200, got %v", res.StatusCode) } body, err := ioutil.ReadAll(res.Body) res.Body.Close() if err != nil { t.Fatal(err) } expected := "Hello, World!" if string(body) != expected { t.Errorf("expected body '%s', got '%s'", expected, body) } }
-
Save the file and return to the project root directory.
To run the integration tests, execute the following command within the project directory:
go test ./tests
If all tests pass, you should see the following output:
PASS ok github.com/your-username/api-testing-tutorial/tests 0.123s
Conclusion
In this tutorial, we have learned how to test Go APIs effectively. We covered both unit testing and integration testing methods to ensure the functionality and reliability of our APIs. By writing comprehensive tests, you can confidently deploy your Go APIs, knowing they perform as expected in a variety of scenarios. Remember to regularly run your tests as part of your development process to catch any regressions or breaking changes. Happy testing!