Skip to content
Advertisement

a PHPpunit test fail randomly

I have a Quiz class. This class load 10 questions from a database depending on the level and the type of the quiz Object: level 0 load the ten first, level 1 load the next ten and so on.

So in my test i create in a test database 30 questions. Then i create quiz object with different level and i check that the first question in the quiz steps array match what i expect.

This test “quiz_contain_steps_depending_on_type_and_level()” failed randomly at least once every 5 launches.

This is the QuizTest class

JavaScript

This is the Trait who generate fake question

JavaScript

This is my Quiz class:

JavaScript

This is the Question class:

JavaScript

If anyone understands this behavior. I thank him in advance for helping me sleep better 🙂

Advertisement

Answer

Thanks to @AlessandroChitolina comments.

The set of questions created in my test was not always recorded in the same order by my in my database.

So instead of testing the expected question from my starting $questions array, i retrieve the questions from the database in a new $dbQuestions array. That solve my problème.

This is the new test:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement