PHP Unit / Unit test user data are not inserted in phpu_user table

PHP Unit / Unit test user data are not inserted in phpu_user table

by Basavraj Challmarad -
Number of replies: 2

Hello,

I have installed the PHP Unit on my local machine and doing unit testing (via PHPUnit). when I pass test user data then the result status is showing as OK (1 test, 2 assertions) at the same time I have checked in the phpu_user table that test data is not inserted. 

So I need help with this. Is this the moodle default behavior? or need to change in code. 

please check the attached files.

Thank You..!!!

Attachment phpu_user.png
Attachment Test_User_Data.png
Attachment Unit_Test_Result.png
Average of ratings: -
In reply to Basavraj Challmarad

Re: PHP Unit / Unit test user data are not inserted in phpu_user table

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
This is exactly as you should expect - every unit test needs to run with a fresh Moodle database, without it containing any data from previous test runs (which would break many of the subsequent tests).

There are two methods for doing this:
a) wrapping the entire test in a database transaction, which is rolled back at the end of the test - this is the usual method and means that the data never makes it into the 'real' database tables at all
b) by writing the data, then restoring it at the end of the test - this is needed in a few cases (including when you are trying to debug a test that is paused in the debugger) and can be achieved by adding $this->preventResetByRollback() at the start of the test

You should certainly not expect to be able to run a unit test in order to configure data in the database that you can make use of outside of the test (although you can do that, to an extent, with behat tests).
In reply to Davo Smith

Re: PHP Unit / Unit test user data are not inserted in phpu_user table

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Note that the way Behat does database reset is different - and it resets at the start of the next test. So, you can use Behat to setup a particular lots of test data - and that is often a very convenient trick.