Faculty of Engineering and Mathematical Sciences 
Not logged in (login)

help3403


This forum is provided to promote discussion amongst students enrolled in Agile Web Development (CITS3403).
 
Options:
RSS cloud
Jump to:

Unittest permanently modifies database?

3 of 419 articles shown, currently no other people reading this forum.
photo
From: Alan C.
Date: Fri 22nd May, 4:30pm
Actions: 
        Login-to-reply
Hi, I wanted to clarify database behaviour that I'm seeing when running tests.

I've built my files as demonstrated during last week's workshop. It seems that when I 
register a user into the test database, then run a system test which involves dropping 
all users in the setUp and tearDown methods, the original user is also removed. Is 
there some way to avoid this behaviour (i.e have the database restored to original 
state once the test finishes)? Or is this even supposed to happen in the first place?

It seems pretty dangerous that the database is permanently modified by the tests, 
especially if the db.dropall() is called. If someone forgets to switch from Config to 
TestConfig, then there's a risk of losing all your data and having to revert to an 
earlier commit or re-migrate your database.

Unittest permanently modifies database?

photo
From: Akhil N.
Date: Sat 23rd May, 6:09pm
Actions: 
        Login-to-reply
"Alan Cheng"                               wrote:

> Hi, I wanted to clarify database behaviour that I'm seeing when running tests.
> 
> I've built my files as demonstrated during last week's workshop. It seems that when I 
> register a user into the test database, then run a system test which involves dropping 
> all users in the setUp and tearDown methods, the original user is also removed. Is 
> there some way to avoid this behaviour (i.e have the database restored to original 
> state once the test finishes)? Or is this even supposed to happen in the first place?
> 
> It seems pretty dangerous that the database is permanently modified by the tests, 
> especially if the db.dropall() is called. If someone forgets to switch from Config to 
> TestConfig, then there's a risk of losing all your data and having to revert to an 
> earlier commit or re-migrate your database.


In our case, we had a separate duplicate testing database made form the same models as the 
production/development database. You can do this changing the SQLALCHEMY_DATABASE_URI to 
have a different file name in the TESTING configuration. This way, your original database 
remains untouched during testing.

Unittest permanently modifies database?

photo
From: Timothy F.
Date: Sun 24th May, 10:20am
Actions: 
        Login-to-reply
"Alan Cheng"                               wrote:

> Hi, I wanted to clarify database behaviour that I'm seeing when running tests.
> 
> I've built my files as demonstrated during last week's workshop. It seems that when I 
> register a user into the test database, then run a system test which involves dropping 
> all users in the setUp and tearDown methods, the original user is also removed. Is 
> there some way to avoid this behaviour (i.e have the database restored to original 
> state once the test finishes)? Or is this even supposed to happen in the first place?
> 
> It seems pretty dangerous that the database is permanently modified by the tests, 
> especially if the db.dropall() is called. If someone forgets to switch from Config to 
> TestConfig, then there's a risk of losing all your data and having to revert to an 
> earlier commit or re-migrate your database.

Not a bad idea to have a guard at the top of your unittests to ensure you are in Testing 
config before you blindly execute tests.
This Page


Program written by: [email protected]
Feedback welcome
Last modified:  8:27am May 24 2020