웹서버 테스트를 위한 Test코드 작성하기

  1. Test 코드 작성
// 위치 : main

package main

import (
	"net/http"

	"./myapp"
)

func main() {

	http.ListenAndServe(":5000", myapp.NewHttpHandler())
}
// ./myapp 폴더를 생성하여 작성
// app.go 생성

package myapp

import (
	"encoding/json"
	"fmt"
	"net/http"
	"time"
)

type User struct {
	FirstName string    `json:"first_name"`
	LastName  string    `json:"last_name"`
	Email     string    `json:"email"`
	CreateAt  time.Time `json:"timestamp"`
}

type fooHandler struct{}

func (f *fooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	//user구조체를 담을 user 선언
	user := new(User)
	// json형 데이터는 Request의 Body에 저장
	err := json.NewDecoder(r.Body).Decode(user)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprint(w, "Bad Request : ", err)
		return
	}
	user.CreateAt = time.Now()

	data, _ := json.Marshal(user)
	w.Header().Add("content-type", "application/json")
	w.WriteHeader(http.StatusOK)
	fmt.Fprint(w, string(data))
}

func barHandler(w http.ResponseWriter, r *http.Request) {
	name := r.URL.Query().Get("name")
	if name == "" {
		name = "World"
	}
	fmt.Fprintf(w, "Hello  %s!", name)
}

func NewHttpHandler() http.Handler {
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello World")
	})

	mux.HandleFunc("/bar", barHandler)
	mux.Handle("/foo", &fooHandler{})
	return mux
}

go 언어의 특성으로 _test.go 로 파일명 작성시(컨베이션) 테스트 코드

// ./myapp
// app_test.go 작성
package myapp

import "testing"

func TestIndexPathHandler(t *testing.T) {

}
  1. 테스트 방법

go convey 설치

  1. 설치 방법 : go get https://github.com/smartystreets/goconvey
  2. 테스트 해당 폴더에서 "goconvey" 실행
  3. 실행 결과
D:\\DID Project\\test\\Holder 디렉터리

2021-09-17  오전 08:06    <DIR>          .
2021-09-17  오전 08:06    <DIR>          ..
2021-09-17  오전 10:35               133 main.go
2021-09-17  오전 10:11    <DIR>          myapp
2021-09-17  오전 10:33         6,372,864 Holder.exe
               2개 파일           6,372,997 바이트
               3개 디렉터리  164,000,169,984 바이트 남음

D:\\DID Project\\test\\Holder>goconvey
2021/09/17 10:38:39 goconvey.go:61: Initial configuration: [host: 127.0.0.1] [port: 8080] [poll: 250ms] [cover: true]
2021/09/17 10:38:40 tester.go:19: Now configured to test 10 packages concurrently.
2021/09/17 10:38:40 goconvey.go:203: Serving HTTP at: <http://127.0.0.1:8080>
2021/09/17 10:38:40 goconvey.go:105: Launching browser on 127.0.0.1:8080
2021/09/17 10:38:40 integration.go:122: File system state modified, publishing current folders... 0 4895528108
2021/09/17 10:38:40 goconvey.go:118: Received request from watcher to execute tests...
2021/09/17 10:38:40 goconvey.go:111: exec: "start": executable file not found in %PATH%
2021/09/17 10:38:40 goconvey.go:113:
2021/09/17 10:38:40 executor.go:69: Executor status: 'executing'
2021/09/17 10:38:40 coordinator.go:46: Executing concurrent tests: D:/DID
2021/09/17 10:38:40 coordinator.go:46: Executing concurrent tests: D:/DID
2021/09/17 10:38:47 shell.go:90: Please run goconvey from within $GOPATH/src (also, symlinks might be problematic). Output and Error:  cannot find package "D:\\\\DID" in any of:
        C:\\Program Files\\Go\\src\\D:\\DID (from $GOROOT)
        C:\\Users\\K4\\go\\src\\D:\\DID (from $GOPATH)
 exit status 1
2021/09/17 10:38:47 shell.go:90: Please run goconvey from within $GOPATH/src (also, symlinks might be problematic). Output and Error:  cannot find package "D:\\\\DID" in any of:
        C:\\Program Files\\Go\\src\\D:\\DID (from $GOROOT)
        C:\\Users\\K4\\go\\src\\D:\\DID (from $GOPATH)
 exit status 1
2021/09/17 10:38:47 parser.go:24: [build failure]: D:\\DID
2021/09/17 10:38:47 parser.go:24: [build failure]: D:\\DID
2021/09/17 10:38:47 executor.go:69: Executor status: 'idle'
2021/09/17 10:39:53 integration.go:122: File system state modified, publishing current folders... 4895528108 4895528376
2021/09/17 10:39:53 goconvey.go:118: Received request from watcher to execute tests...
2021/09/17 10:39:54 executor.go:69: Executor status: 'executing'
2021/09/17 10:39:54 coordinator.go:46: Executing concurrent tests: D:/DID
2021/09/17 10:39:54 coordinator.go:46: Executing concurrent tests: D:/DID
2021/09/17 10:39:55 shell.go:90: Please run goconvey from within $GOPATH/src (also, symlinks might be problematic). Output and Error:  cannot find package "D:\\\\DID" in any of:
        C:\\Program Files\\Go\\src\\D:\\DID (from $GOROOT)
        C:\\Users\\K4\\go\\src\\D:\\DID (from $GOPATH)
 exit status 1
2021/09/17 10:39:55 shell.go:90: Please run goconvey from within $GOPATH/src (also, symlinks might be problematic). Output and Error:  cannot find package "D:\\\\DID" in any of:
        C:\\Program Files\\Go\\src\\D:\\DID (from $GOROOT)
        C:\\Users\\K4\\go\\src\\D:\\DID (from $GOPATH)
 exit status 1
2021/09/17 10:39:55 parser.go:24: [build failure]: D:\\DID
2021/09/17 10:39:55 parser.go:24: [build failure]: D:\\DID
2021/09/17 10:39:55 executor.go:69: Executor status: 'idle'