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

Task - s'assurer que les tests soient exécutés de façon séquentielle #38

Closed
benoitdm-oslandia opened this issue Sep 19, 2022 · 5 comments
Assignees
Milestone

Comments

@benoitdm-oslandia
Copy link
Owner

In GitLab by @benoitdm-oslandia on Sep 19, 2022, 09:13

@benoitdm-oslandia
Copy link
Owner Author

In GitLab by @lowzonenose on Oct 7, 2022, 11:31

Pour assurer une séquentialité des tests (ordre), il existe une possibilité native à golang avec la notion de Subtests.

Ex. avec le fichier de tests des opérations CRUD :

package service

import (
	"testing"
	"time"

	log "github.com/sirupsen/logrus"
)

type CRUDTests struct {
	Test *testing.T
}

func TestRunnerHandlerCRUD(t *testing.T) {

	// initialisation avant l'execution des tests
	beforeRun()

	t.Run("DELETE", func(t *testing.T) {
		// liste de tests sur la suppression des features
		test := CRUDTests{Test: t}
		test.ApiContainsDeleteFeature()
		test.DeleteExistingFeature()
		test.DeleteFeatureErrorMalformedFeatureId()
		test.DeleteFeatureErrorUnknownCollection()
		test.DeleteFeatureErrorUnusedQueryParameters()
		test.DeleteUnknownFeature()
	})
	t.Run("PUT", func(t *testing.T) {
		// test := CRUDTests{Test: t}
	})
	t.Run("POST", func(t *testing.T) {
		// test := CRUDTests{Test: t}
	})
	t.Run("UPDATE", func(t *testing.T) {
		// test := CRUDTests{Test: t}
	})
	t.Run("PATCH", func(t *testing.T) {
		// test := CRUDTests{Test: t}
	})

	// nettoyage après execution des tests
	afterRun()
}

func beforeRun() {
	log.Info("beforeRun")
}

func afterRun() {
	log.Info("afterRun")
}

Ex. d'impl. d'une des fonctions pour les opérations DELETE

func (t *CRUDTests) ApiContainsDeleteFeature() {
	resp := hTest.DoRequest(t.Test, "/api")
	body, _ := ioutil.ReadAll(resp.Body)

	var v openapi3.Swagger
	errUnMarsh := json.Unmarshal(body, &v)
	util.Assert(t.Test, errUnMarsh == nil, fmt.Sprintf("%v", errUnMarsh))

	path := v.Paths.Find("/collections/{collectionId}/items/{featureId}")
	util.Assert(t.Test, path != nil, "Delete feature path does not exist")
	util.Equals(t.Test, "deleteCollectionFeature", path.Delete.OperationID, "Delete path not present")
	util.Equals(t.Test, 2, len(path.Delete.Parameters), "2 parameters expected")
	util.Assert(t.Test, path.Delete.Parameters.GetByInAndName("path", "collectionId") != nil, "collectionId path parameter not found")
	util.Assert(t.Test, path.Delete.Parameters.GetByInAndName("path", "featureId") != nil, "featureId path parameter not found")
}

Exécution :

$ /usr/bin/go test -timeout 30s -run ^TestRunnerHandlerCRUD$ github.com/CrunchyData/pg_featureserv/internal/service
=== RUN   TestRunnerHandlerCRUD
=== RUN   TestRunnerHandlerCRUD/DELETE
=== RUN   TestRunnerHandlerCRUD/PUT
=== RUN   TestRunnerHandlerCRUD/POST
=== RUN   TestRunnerHandlerCRUD/UPDATE
=== RUN   TestRunnerHandlerCRUD/PATCH
--- PASS: TestRunnerHandlerCRUD (5.01s)
    --- PASS: TestRunnerHandlerCRUD/DELETE (1.01s)
    --- PASS: TestRunnerHandlerCRUD/PUT (1.00s)
    --- PASS: TestRunnerHandlerCRUD/POST (1.00s)
    --- PASS: TestRunnerHandlerCRUD/UPDATE (1.00s)
    --- PASS: TestRunnerHandlerCRUD/PATCH (1.00s)
PASS
ok      github.com/CrunchyData/pg_featureserv/internal/service  5.028s

@benoitdm-oslandia
Copy link
Owner Author

In GitLab by @lowzonenose on Oct 7, 2022, 11:33

Si la méthode est validée, je pousse une version sur le gitLab pour les tests séquentiels sur les opération CRUD.

@benoitdm-oslandia
Copy link
Owner Author

C pas mal comme solution : ca permet autant de regrouper les tests et de s'assurer qu'ils ne s'exécute pas en parallèle !

@benoitdm-oslandia
Copy link
Owner Author

cloned to #48

@benoitdm-oslandia
Copy link
Owner Author

marked this issue as related to #48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

2 participants