흐르는 시간의 블로그...

Golang 프로젝트에 TDD 도입하기 - Golang Korean Community

위의 내용에 따라 간단한 테스트를 진행해 봤다.

이와 함께 assertion 패키지를 인스톨하고 사용했다.

if문으로 도배될 많은 테스트 코드들을 위해서 assertion은 필수라고 생각한다.


원래의 테스트 코드는 아래와 같이 if와 else로 구성할 수 밖에 없다.

func TestGetWords(t *testing.T) {
	if words, err := dbjobs.GetWords(); nil != err {
		t.Error("GetWords() return error. " + err.Error())
	} else {
		tWord := []string{"a", "c", "d", "xxdfs", "ワード"}

		isSame := true
		for i, _ := range words {
			if words[i] != tWord[i] {
				isSame = false
				break
			}
		}

		if isSame {
			t.Logf("result is %v", words)
		} else {
			t.Errorf("Value is Not valid. \n first %v \n second %v", tWord, words)
		}
	}
}


github.com/stretchr/testify 패키지를 인스톨하고 assert로 변경한 코드이다.

func TestGetWords(t *testing.T) {
	assert.Equal(t, 123, 125, "they should be equal")
	assert.NotEqual(t, 123, 456, "they should not be equal")

	words, err := dbjobs.GetWords()

	assert.Nil(t, err)

	t1Word := []string{"a", "c", "d", "xxdfs", "ワード"}
	assert.Equal(t, t1Word, words, "Words are not what I want.")

	t2Word := []string{"a", "b", "c", "d", "xxdfs", "ワード"}
	assert.Equal(t, t2Word, words, "Words are not what I want.")
}


assertion 사용에 따른 결과는 아래와 같다.

	Error Trace:	job4mariadb_test.go:32
	Error:      	Not equal: 
	            	expected: 123
	            	actual: 125
	Messages:   	they should be equal
	Error Trace:	job4mariadb_test.go:40
	Error:      	Not equal: 
	            	expected: []string{"a", "c", "d", "xxdfs", "ワード"}
	            	actual: []string{"a", "b", "c", "d", "xxdfs", "ワード"}
	            	
	            	Diff:
	            	--- Expected
	            	+++ Actual
	            	@@ -1,3 +1,4 @@
	            	-([]string) (len=5) {
	            	+([]string) (len=6) {
	            	  (string) (len=1) "a",
	            	+ (string) (len=1) "b",
	            	  (string) (len=1) "c",
	Messages:   	Words are not what I want.




-꼬리-

* 티스토리의 스타일 변경을 했다가 오랜 시간 기존 작성글들을 다시 수정해야 했다.

* 그 과정에서 오늘자로 작성한 내용이 날아갔다. ㅠ.ㅠ

* 그래서 간략히 다시 작성