Run webapi tests as sub-tests.

Running as sub-tests has the benefit of automatically logging the test name, no need to include it in failure messages manually. It even works if the test panics.
This commit is contained in:
jholdstock 2022-03-30 12:00:56 +01:00 committed by Jamie Holdstock
parent b32bb56032
commit 6057c2b273

View File

@ -189,6 +189,7 @@ func TestSetAltSignAddress(t *testing.T) {
}} }}
for _, test := range tests { for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ticketHash := randString(64, hexCharset) ticketHash := randString(64, hexCharset)
req := &setAltSignAddrRequest{ req := &setAltSignAddrRequest{
Timestamp: time.Now().Unix(), Timestamp: time.Now().Unix(),
@ -212,7 +213,7 @@ func TestSetAltSignAddress(t *testing.T) {
RespSig: randString(96, sigCharset), RespSig: randString(96, sigCharset),
} }
if err := api.db.InsertAltSignAddr(ticketHash, data); err != nil { if err := api.db.InsertAltSignAddr(ticketHash, data); err != nil {
t.Fatalf("%q: unable to insert alt sign addr: %v", test.name, err) t.Fatalf("unable to insert alt sign addr: %v", err)
} }
} }
@ -251,23 +252,24 @@ func TestSetAltSignAddress(t *testing.T) {
r.ServeHTTP(w, c.Request) r.ServeHTTP(w, c.Request)
if test.wantCode != w.Code { if test.wantCode != w.Code {
t.Errorf("%q: expected status %d, got %d", test.name, test.wantCode, w.Code) t.Errorf("expected status %d, got %d", test.wantCode, w.Code)
} }
altsig, err := api.db.AltSignAddrData(ticketHash) altsig, err := api.db.AltSignAddrData(ticketHash)
if err != nil { if err != nil {
t.Fatalf("%q: unable to get alt sign addr data: %v", test.name, err) t.Fatalf("unable to get alt sign addr data: %v", err)
} }
if test.wantCode != http.StatusOK && !test.isExistingAltSignAddr { if test.wantCode != http.StatusOK && !test.isExistingAltSignAddr {
if altsig != nil { if altsig != nil {
t.Fatalf("%q: expected no alt sign addr saved for errored state", test.name) t.Fatalf("expected no alt sign addr saved for errored state")
} }
continue return
} }
if !bytes.Equal(b, []byte(altsig.Req)) || altsig.ReqSig != reqSig { if !bytes.Equal(b, []byte(altsig.Req)) || altsig.ReqSig != reqSig {
t.Fatalf("%q: expected alt sign addr data different than actual", test.name) t.Fatalf("expected alt sign addr data different than actual")
} }
})
} }
} }