This is a creation in Article, where the information may have evolved or changed. Table-driven testing is a test method for writing easy-to-extend test cases. Table-driven testing is common (not unique) in the Go language, and is used by many standard library <sup>[1] (#reference) </sup>. Table-driven testing uses an anonymous struct. In this article I'll show you how to write a table-driven test. To continue using the [Errline repo] (https://github.com/virup/errline) project, now let's add a test for the Wrap () function. The ' Wrap () ' function is used to give an ' error ' modifier to add a file name and number of rows at the call location. In particular, we need to test the logic in which the short name of the file is computed (in bold). The initial ' wrap () ' function is as follows: <pre>func wrap (Err Error) error {If Err = nil {return nil}//If error already has a file line D o not add it again. If _, OK: = Err. (*withfileline); OK {return Err} _, file, line, OK: = runtime. Caller (calldepth) if!ok {file = "???" line = 0} <b>short: = file for I: = Len (file)-1; i > 0; i--{if file[i] = = '/' {short = file[i+1:] Break}} file = short</b> return &withfileline{err, file, LINE}}&L T;/pre> to test the logic of short file name calculations, we extract this part of the logic as the function ' Getshortfilename () '. The code now becomes this: <pre>func Wrap (err Error) error {If Err = nil {return nil}//If error already has a file line does not add It again. If _, OK: = Err. (*withfileline);OK {return Err} _, file, line, OK: = runtime. Caller (calldepth) if!ok {file = "???" line = 0} file = getshortfilename (file) return &withfileline{err, file, line} }func <b>getshortfilename (file string) </b> string {short: = file for I: = Len (file)-1; i > 0; i--{if f Ile[i] = = '/' {short = file[i+1:] "break}}" file = Simple return file}</pre> It is common practice to refactor the code to make it easy to test. We now Test ' getshortfilename () ' By passing multiple file name parameters to verify that the output is as expected. Let's start with an empty test function: "' Gofunc testshortfilename (t *testing. T) {} "and then we introduce an anonymous struct (struct) containing the field ' in ' and ' expected '. ' In ' means the arguments passed to ' getshortfilename () ', ' expected ' represents our expected return result. ' Tests ' is an array that contains more than one such struct. "' Gofunc testshortfilename (t *testing. T) {Tests: = []struct {in string//input expected string//expected result} {"???", "???"}, {"Filename.go", "Filenam E.go "}, {" Hello/filename.go "," Filename.go "}, {" Main/hello/filename.go "," Filename.go "},}" with this, We can use loops to implement our testing methods. "' Gofunc testshortfilename (t *testing. T) {Tests: = []struct {in string expectEd string} {"???", "???"}, {"Filename.go", "Filename.go"}, {"Hello/filename.go", "Filename.go"}, {"main/hello/ Filename.go "," Filename.go "},} for _, TT: = Range Tests {actual: = Getshortfilename (tt.in) if strings. Compare (Actual, tt.expected)! = 0 {t.fail ()}}} "can be noted that adding test cases is extremely simple, just add items in ' tests '. This scheme can be extended to test methods that accept and return multiple parameters. That's it. The code can be obtained from [my GitHub] (https://github.com/virup/errline/tree/master). # # <p id= "Reference" > Reference </p>1. Some examples of table-driven tests for the Go language standard library * [Https://github.com/golang/go/blob/master/src/strconv/ftoa_test.go] (https://github.com/ GOLANG/GO/BLOB/MASTER/SRC/STRCONV/FTOA_TEST.GO) * [Https://github.com/golang/go/blob/master/src/path/match_ Test.go] (https://github.com/golang/go/blob/master/src/path/match_test.go) * [https://github.com/golang/go/blob/ Master/src/archive/tar/strconv_test.go] (https://github.com/golang/go/blob/master/src/archive/tar/strconv_ TEST.GO)
via:https://medium.com/@virup/how-to-write-concise-tests-table-driven-tests-ed672c502ae4
Author: Viru Translator: Alfred-zhong proofreading: polaris1119
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
407 Reads