Category Archives: Testing
mountebank for mocking and stubbing http services (rest+graphql+grpc)
http://www.mbtest.org/docs/api/stubs can be used to stub http(s)/tcp/smtp protocols. And has community plugins for grpc, graphql, websockets. You can configure it with imposters.ejs like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "port": 80, "protocol": "http", "defaultResponse": { "statusCode": 404, "headers": {} }, "stubs": [ <% include stubs/service1.json %>, <% include stubs/service2.json %>, <% include stubs/service3.json %> ] } |
docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 |
version: "3.5" services: mountebank: container_name: mountebank image: jkris/mountebank:latest volumes: - ./:/mountebank ports: - "15050:80" command: --configfile /mountebank/imposters.ejs --allowInjection |
HTTP Toolkit — another alternative to Charles Proxy for http calls interception
https://httptoolkit.com/ It has a free version which does something similar to Charles Proxy, so you can hack into the middle of any request/response according to the rules specified.
Docker and Kubernetes basics for developers
Docker Docker containers are much more lightweight compared to classic VMs as they leverage host OS instead of starting their own OS. Containers are using 2 features of Linux-based OS: Namespaces and Cgroups (Control Groups). Namespace Lets you allocate resources in an isolated environment (like a sandbox). On a container start, docker daemon generates a …
Mock sql (sqlx) db on golang
I am using this library — https://pkg.go.dev/github.com/data-dog/go-sqlmock. That’s how you can use it for mocking db querying:
Fast and simple mock server app by swagger file
I’ve tried Mockoon for mocking by swagger/OpenAPI spec file, and it works pretty good. It autogenerates the response based on swagger format, and you are able to update any response field the way you want, save your changes and run the mock server with one click on the port you specify. It is a separate …
Types of load tests
Load test types explained by k6. Smoke tests Verify that your system can handle minimal load, without any problems. Like 1 rps for 1 minute. Load tests Assess system performance in terms of concurrent users or requests per second. If you want to make sure that you can handle 500 rps, you can gradually increase …
mmock for mocking microservices
It is a very easy to set up though pretty powerful tool with support for delays and handling query params. It also has a console for checking all the requests and corresponding responses matched. Here it is https://github.com/jmartin82/mmock. Below I describe a way to use it with docker-compose for local development.
Mocking for unit-tests and e2e-tests in golang
Some patterns to test your golang app. Mocking an interface
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
mockS3 := &mocks.S3API{} mockResultFn := func(input *s3.ListObjectsInput) *s3.ListObjectsOutput { output := &s3.ListObjectsOutput{} output.SetCommonPrefixes([]*s3.CommonPrefix{ &s3.CommonPrefix{ Prefix: aws.String("2017-01-01"), }, }) return output } // NB: .Return(...) must return the same signature as the method being mocked. // In this case it's (*s3.ListObjectsOutput, error). mockS3.On("ListObjects", mock.MatchedBy(func(input *s3.ListObjectsInput) bool { return input.Delimiter != nil && *input.Delimiter == "/" && input.Prefix == nil })).Return(mockResultFn, nil) listingInput := &s3.ListObjectsInput{ Bucket: aws.String("foo"), Delimiter: aws.String("/"), } listingOutput, err := mockS3.ListObjects(listingInput) if err != nil { panic(err) } for _, x := range listingOutput.CommonPrefixes { fmt.Printf("common prefix: %+v\n", *x) } |
https://github.com/vektra/mockery Mocking SQL database
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// a successful case func TestShouldUpdateStats(t *testing.T) { db, mock, err := sqlmock.New() if err != nil { t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) } defer db.Close() mock.ExpectBegin() mock.ExpectExec("UPDATE products").WillReturnResult(sqlmock.NewResult(1, 1)) mock.ExpectExec("INSERT INTO product_viewers").WithArgs(2, 3).WillReturnResult(sqlmock.NewResult(1, 1)) mock.ExpectCommit() // now we execute our method if err = recordStats(db, 2, 3); err != nil { t.Errorf("error was not expected while updating stats: %s", err) } // we make sure that all expectations were met if err := mock.ExpectationsWereMet(); err != nil { t.Errorf("there were unfulfilled expectations: %s", err) } } |
https://github.com/DATA-DOG/go-sqlmock Mocking HTTP server for unit-tests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
handler := func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "<html><body>Hello World!</body></html>") } req := httptest.NewRequest("GET", "http://example.com/foo", nil) w := httptest.NewRecorder() handler(w, req) resp := w.Result() body, _ := ioutil.ReadAll(resp.Body) fmt.Println(resp.StatusCode) fmt.Println(resp.Header.Get("Content-Type")) fmt.Println(string(body)) |
https://golang.org/pkg/net/http/httptest/ Stubbing HTTP server for e2e-tests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
{ "host": "example.com", "method": "GET|POST|PUT|PATCH|... (Mandatory)", "path": "/your/path/:variable (Mandatory)", "queryStringParameters": { "name": ["value"], "name": ["value", "value"] }, "headers": { "name": ["value"] }, "cookies": { "name": "value" }, "body": "Expected Body" } |
https://github.com/jmartin82/mmock Mocking for e2e-tests with dockertest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
var db *sql.DB func TestMain(m *testing.M) { // uses a sensible default on windows (tcp/http) and linux/osx (socket) pool, err := dockertest.NewPool("") if err != nil { log.Fatalf("Could not connect to docker: %s", err) } // pulls an image, creates a container based on it and runs it resource, err := pool.Run("mysql", "5.7", []string{"MYSQL_ROOT_PASSWORD=secret"}) if err != nil { log.Fatalf("Could not start resource: %s", err) } // exponential backoff-retry, because the application in the container might not be ready to accept connections yet if err := pool.Retry(func() error { var err error db, err = sql.Open("mysql", fmt.Sprintf("root:secret@(localhost:%s)/mysql", resource.GetPort("3306/tcp"))) if err != nil { return err } return db.Ping() }); err != nil { log.Fatalf("Could not connect to docker: %s", err) } code := m.Run() // You can't defer this because os.Exit doesn't care for defer if err := pool.Purge(resource); err != nil { log.Fatalf("Could not purge resource: %s", err) } os.Exit(code) } func TestSomething(t *testing.T) { // db.Query() } |
https://github.com/ory/dockertest
Integration tests with testcontainers-go
Here is the go library that simplifies integration tests with docker containers — https://github.com/testcontainers/testcontainers-go. That’s how you can use it to test sql — https://github.com/testcontainers/testcontainers-go/blob/master/docs/examples/cockroachdb.md. Project documentation — https://golang.testcontainers.org/features/docker_compose/ The idea is to prepare the environment, build your app, then make requests and check the DB state.
Test Push Notifications
https://github.com/onmyway133/PushNotifications