Fiber: ๐Ÿค” How do I run a set of unit test?

Created on 10 Sep 2020  ยท  2Comments  ยท  Source: gofiber/fiber

I want to set up a bunch of test

  • What command do i use to run them or link them to a fiber project?

  • How do i link a bunch of files with test functions on it?

Again, i am really sorry if this seems easy or obvious, i am quite new to go and fiber.
i would really appreciate some light on here.

I have already applied the examples below, i just dont get how to link them or run them

โ™ป Wait for Response ๐Ÿค” Question

Most helpful comment

Hi @slalbertojesus, don't apologize for asking questions ( we all started somewhere ๐Ÿ˜‰ )

I recommend watching this video https://www.youtube.com/watch?v=sOeUf1YICSA or https://www.youtube.com/watch?v=GlA57dHa5Rg that explains how to write unit tests.

You could also browse Fiber's root directory and check out the _test.go files https://github.com/gofiber/fiber, for example:
```go
// myfile_test.go
package main

import (
"net/http/httptest"
"testing"

fiber "github.com/gofiber/fiber"
utils "github.com/gofiber/utils"

)

// go test -run -v Test_Handler
func Test_Handler(t *testing.T) {
app := New()

app.Get("/test", func(c *Ctx) {
    c.SendStatus(400)
})

resp, err := app.Test(httptest.NewRequest("GET", "/test", nil))

utils.AssertEqual(t, nil, err, "app.Test")
utils.AssertEqual(t, 400, resp.StatusCode, "Status code")

}

All 2 comments

Hi @slalbertojesus, don't apologize for asking questions ( we all started somewhere ๐Ÿ˜‰ )

I recommend watching this video https://www.youtube.com/watch?v=sOeUf1YICSA or https://www.youtube.com/watch?v=GlA57dHa5Rg that explains how to write unit tests.

You could also browse Fiber's root directory and check out the _test.go files https://github.com/gofiber/fiber, for example:
```go
// myfile_test.go
package main

import (
"net/http/httptest"
"testing"

fiber "github.com/gofiber/fiber"
utils "github.com/gofiber/utils"

)

// go test -run -v Test_Handler
func Test_Handler(t *testing.T) {
app := New()

app.Get("/test", func(c *Ctx) {
    c.SendStatus(400)
})

resp, err := app.Test(httptest.NewRequest("GET", "/test", nil))

utils.AssertEqual(t, nil, err, "app.Test")
utils.AssertEqual(t, 400, resp.StatusCode, "Status code")

}

@slalbertojesus did my advice help you creating unit tests? We assume you don't need further help, feel free to re-open this issue if you have any more questions ๐Ÿ‘

Was this page helpful?
0 / 5 - 0 ratings