Skip to content

Instantly share code, notes, and snippets.

@dhulihan
Created March 25, 2020 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhulihan/74d6c8c2ff24e66da61956c55b3d2477 to your computer and use it in GitHub Desktop.
Save dhulihan/74d6c8c2ff24e66da61956c55b3d2477 to your computer and use it in GitHub Desktop.
integration testing in go
  • Integration Testing
    • Filenames
      • main_integration_test.go - meh
      • package/integration_test.go - Better
        • Give it build tag of integration.
        • package/integration_foo_test.go
      • package/file_integration_test.go - hmmm.
    • Use -check.f to check for code patterns
       go test ... -check.f '.*Unit.*'
       go test ... -check.f '.*Integration.*'
      • Advantages
        • Can mingle test code in the same directories
      • Disadvantages
        • If you don't use the right pattern in your code, the test might be skipped
    • Use -short flag
      • go test -short to run unit tests only.
         func TestPostgresVersionIntegration(t *testing.T) {
         	if testing.Short() {
         		t.Skip("skipping integration test")
         	}
         	...
         }
      • Running without -short runs everything.
      • Simple and used in standard library.
      • Not as flexible.
    • Use -run flag
      • Naming Convention
        • TestUnitFoo
          • go test -run 'Unit'
        • TestIntegrationFoo
          • go test -run 'Integration'
      • Can mix and match with integrations
    • Using build flags
       // +build integration
       go test --tags=integration
    • Test configuration
      • Use global flags
         var fooAddr = flag.String(...)
        
         func TestToo(t *testing.T) {
         	f, err := foo.Connect(*fooAddr)
         	// ...
         }
      • Use shared package
        • testing/integration/integration.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment