Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save cassettes without any interactions #79

Merged
merged 1 commit into from Sep 26, 2022

Conversation

annismckenzie
Copy link
Contributor

This is often required when writing table-driven tests where some of the test cases don't produce any interactions (like when validation fails) but the setup and test code is the same for every test case. In this case, on a replay these tests then fail because the cassette for the test cannot be found.

Imagine this test code (I'll try to make it as succinct as possible):

package example

import (
	"flag"
	"testing"

	"gopkg.in/dnaeon/go-vcr.v3/recorder"
)

var updateTestdata = flag.Bool("update", false, "update test data")

func TestSomething(t *testing.T) {
	type args struct {
		service string
	}
	tests := []struct {
		name string
	}{
		{
			name: "test 1",
		},
		{
			name: "test 2",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cassetteName := t.Name()
			opts := &recorder.Options{
				CassetteName: cassetteName,
				Mode:         recorder.ModeReplayOnly,
			}
			if *updateTestdata {
				opts.Mode = recorder.ModeRecordOnly
			}

			recorder, err := recorder.NewWithOptions(opts)
			if err != nil {
				t.Fatalf("creating recorder: %v", err)
			}

			// test code

			if err := recorder.Stop(); err != nil {
				t.Fatalf("stopping recorder: %v", err)
			}
		})
	}
}

Imagine this were testing an HTTP API and some tests would fail validation and not do any calls to the API that's being tested, resulting in no interactions for that cassette.

I have a Makefile target that runs this using go test -v ./... -updateTestdata which sets the mode to recorder.ModeRecordOnly. In CI the mode is then set to recorder.ModeReplayOnly. Because the cassette isn't there with the current code, it'll fail on those tests. I also decided against using a single cassette for the whole test function because that's brittle and will prevent us from adding more test cases in the middle and will result in one big cassette that can't be grasped easily.

Dropping these three lines of code will write a file like this:

---
version: 2
interactions: []

…which is perfectly good. Removing this code also doesn't break any tests.

I know, of course, that this is a behavior change. I can also make it configurable but I decided against that for now.

This is often required when writing table-driven tests where some of the test cases
don't produce any interactions (like when validation fails) but the setup and test
code is the same for every test case. In this case, on a replay these tests then
fail because the cassette for the test cannot be found.
@dnaeon dnaeon merged commit fb5b2d1 into dnaeon:v3 Sep 26, 2022
@dnaeon
Copy link
Owner

dnaeon commented Sep 26, 2022

Hey, @annismckenzie ! Merged, thanks!

@annismckenzie
Copy link
Contributor Author

Whaaaaat, thank you! <3

@annismckenzie annismckenzie deleted the save-empty-cassettes branch September 26, 2022 13:19
@annismckenzie
Copy link
Contributor Author

Could you tag v3.1.1? :)

@dnaeon
Copy link
Owner

dnaeon commented Sep 27, 2022

@annismckenzie done, thanks for the reminder :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants