Skip to content

Refactor tests to use vanilla testing package plus github.com/stretchr/testify #21

@WGH-

Description

@WGH-

Some problems with https://gopkg.in/check.v1:

  • It is unmaintained. Last commit 5 years ago.
  • It is not particularly popular. Most projects use vanilla testing, sometimes aided by third-party helpers such as https://github.com/stretchr/testify
  • It doesn't support subtests. For example, should any of the table-driven unmarshalTests test cases fail, the entire TestUnmarshal test is immediately aborted, and you can't estimate how broken your patch is :).
  • It doesn't distinguish Error (mark failed) vs. Fatal (mark failed and abort). (although in most cases you'd want to structure different checks as subtests instead, occasionally it doesn't worth it)
  • It doesn't support go test -run to run tests/subtests selectively.
  • go test -v doesn't show captured output.

I've had good experience with testing +github.com/stretchr/testify combo in several closed source projects.

Some examples how existing tests might change:

func TestUnmarshal(t *testing.T) {
	for _, item := range unmarshalTests {
		t.Run("", func(t *testing.T) {
			t.Logf("%q", item.data)
			value := reflect.New(reflect.ValueOf(item.value).Type())
			err := yaml.Unmarshal([]byte(item.data), value.Interface())
			if _, ok := err.(*yaml.TypeError); !ok {
				require.NoError(t, err) // fail and abort
			}
			assert.Equalf(t, item.value, value.Elem().Interface(), "error: %v", err)
		})
	}
}

/*
func (s *S) TestUnmarshal(c *C) {
	for i, item := range unmarshalTests {
		c.Logf("test %d: %q", i, item.data)
		t := reflect.ValueOf(item.value).Type()
		value := reflect.New(t)
		err := yaml.Unmarshal([]byte(item.data), value.Interface())
		if _, ok := err.(*yaml.TypeError); !ok {
			c.Assert(err, IsNil)
		}
		c.Assert(value.Elem().Interface(), DeepEquals, item.value, Commentf("error: %v", err))
	}
}
*/

Failure output example, showcasing multiple failing subtests: (yeah, diff output is somewhat excessive, but it can be useful in more complicated tests cases)

--- FAIL: TestUnmarshal (0.01s)
    --- FAIL: TestUnmarshal/#149 (0.00s)
        decode_test.go:830: "a: 2015-01-01\n"
        decode_test.go:836:
            	Error Trace:	/home/wgh/projects/_forks/go-yaml/decode_test.go:836
            	Error:      	Not equal:
            	            	expected: map[string]time.Time{"a":time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC)}
            	            	actual  : map[string]time.Time{"a":time.Date(2015, time.January, 1, 0, 0, 0, 0, time.UTC)}
            	            	
            	            	Diff:
            	            	--- Expected
            	            	+++ Actual
            	            	@@ -3,3 +3,3 @@
            	            	   wall: (uint64) 0,
            	            	-  ext: (int64) 63587203200,
            	            	+  ext: (int64) 63555667200,
            	            	   loc: (*time.Location)(<nil>)
            	Test:       	TestUnmarshal/#149
            	Messages:   	error: <nil>
    --- FAIL: TestUnmarshal/#171 (0.00s)
        decode_test.go:830: "a: b\r\nc:\r\n- d\r\n- e\r\n"
        decode_test.go:836:
            	Error Trace:	/home/wgh/projects/_forks/go-yaml/decode_test.go:836
            	Error:      	Not equal:
            	            	expected: map[string]interface {}{"a":"b", "c":[]interface {}{"d", "e", "f"}}
            	            	actual  : map[string]interface {}{"a":"b", "c":[]interface {}{"d", "e"}}
            	            	
            	            	Diff:
            	            	--- Expected
            	            	+++ Actual
            	            	@@ -2,6 +2,5 @@
            	            	  (string) (len=1) "a": (string) (len=1) "b",
            	            	- (string) (len=1) "c": ([]interface {}) (len=3) {
            	            	+ (string) (len=1) "c": ([]interface {}) (len=2) {
            	            	   (string) (len=1) "d",
            	            	-  (string) (len=1) "e",
            	            	-  (string) (len=1) "f"
            	            	+  (string) (len=1) "e"
            	            	  }
            	Test:       	TestUnmarshal/#171
            	Messages:   	error: <nil>

I'm willing to undertake this refactoring, if maintainers agree with me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions