Skip to content
Constantin Konstantinidis edited this page Jan 16, 2023 · 1 revision

Testing requires often to compare the output of a method to some reference. A stable reference is necessary to be practical and safe over time.

Comparing a log is awkward as it often contains varying data like date and time. Code like pfile := iotest.NewWriteLogger(t.Name(),os.Stdout) is complex to compare to some reference.

Using an example and its // Output: instruction is cumbersome when many lines are expected.

To handle output, the func with a io.Writer parameter like below, but it requires to update or to write code with test in mind.

func modulo37(f io.Writer) {
	for i := 1; i < 100; i++ {
		if i%3 == 0 {
			fmt.Fprint(f,"Open")
		}
		if i%7 == 0 {
			fmt.Fprint(f,"Source")
		}
		if (i%3 != 0) && (i%7 != 0) {
			fmt.Fprintf(f,"%d\n",i)
		} else {
			fmt.Fprintln(f)
		}
	}
}

Without access to the source code, the output can be piped to a file. A reference file is used for comparison purposes. This is the purpose of this module.

Clone this wiki locally