mardi 5 décembre 2017

When creating unit/integration test using Given/When/Then is ok to assert facts during the Given?

Context

When writing test, I always use the Given-When-Then formula and in some cases I would like to make sure that the assertions during the Then are the consequence of the When and not that something that was done before the When and my test is given false positives.

Question

1) Is a bad practice to make some assertions during the Given to make sure that the environment is set as expected and the assertions during Then are direct consequence of the When actions?

Example in pseudocode:

//Given
clearAllElementsInDatabase
assertDatabaseIsEmpty

//When
insertElementInDatabase

//Then
assertDatabaseContainsInsertedElement

2) If is a bad practice, what other solution should be done to make sure that the assertions during Then are the consequence of the actions done in When ?

Aucun commentaire:

Enregistrer un commentaire