"Go 서브테스트"의 두 판 사이의 차이

 
1번째 줄: 1번째 줄:
==개요==
==개요==
;Go subtest
;Go 서브테스트
;Go 서브테스트



2023년 4월 26일 (수) 11:04 기준 최신판

1 개요[ | ]

Go subtest
Go 서브테스트

2 테이블 드리븐 테스트 기초[ | ]

func TestTime(t *testing.T) {
    testCases := []struct {
        gmt  string
        loc  string
        want string
    }{
        {"12:31", "Europe/Zuri", "13:31"},     // incorrect location name
        {"12:31", "America/New_York", "7:31"}, // should be 07:31
        {"08:08", "Australia/Sydney", "18:08"},
    }
    for _, tc := range testCases {
        loc, err := time.LoadLocation(tc.loc)
        if err != nil {
            t.Fatalf("could not load location %q", tc.loc)
        }
        gmt, _ := time.Parse("15:04", tc.gmt)
        if got := gmt.In(loc).Format("15:04"); got != tc.want {
            t.Errorf("In(%s, %s) = %s; want %s", tc.gmt, tc.loc, got, tc.want)
        }
    }
}

3 서브테스트를 사용한 테이블 드리븐 테스트[ | ]

func TestTime(t *testing.T) {
    testCases := []struct {
        gmt  string
        loc  string
        want string
    }{
        {"12:31", "Europe/Zuri", "13:31"},
        {"12:31", "America/New_York", "7:31"},
        {"08:08", "Australia/Sydney", "18:08"},
    }
    for _, tc := range testCases {
        t.Run(fmt.Sprintf("%s in %s", tc.gmt, tc.loc), func(t *testing.T) {
            loc, err := time.LoadLocation(tc.loc)
            if err != nil {
                t.Fatal("could not load location")
            }
            gmt, _ := time.Parse("15:04", tc.gmt)
            if got := gmt.In(loc).Format("15:04"); got != tc.want {
                t.Errorf("got %s; want %s", got, tc.want)
            }
        })
    }
}
→ 위의 경우와 달리func(t *testing.T)에서 변수명 t 대신 다른 것(예: tt, subt)을 사용하는 경우가 종종 있는데, func() 내부에서는 TestTime(t *testing.T)t를 사용할 일이 없으므로 동일한 변수명을 사용하여 외부 변수 t 사용가능성을 완전히 차단하는 게 좋을 것 같다. (일관성, 단순성, 실수방지)

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}