Running unit tests corrupts main app db

Question:
cannot figure out why, when i run unit tests, my main app db gets corrupted and my app no longer runs. i’m using replit’s unit test module…

created a separate TestConfig…

class TestConfig:
  SQLALCHEMY_DATABASE_URI = 'sqlite:///test/testing_database.db'
  TESTING = True
  SQLALCHEMY_TRACK_MODIFICATIONS = False
  SESSION_TYPE = 'filesystem'

included this in the Initialization section of the unit test module…

# Flask app server
app = Flask(__name__, static_url_path='/static')
app.config.from_object(TestConfig)
db.init_app(app)
Session(app)

then i have this in the Setup section of the unit test module…

def setUp(self):
  with app.app_context():
    # Create tables for testing
    db.create_all()
    # Create a user for testing
    hashed_password = bcrypt.generate_password_hash("testpwdFOOBAR").decode('utf-8')
    test_user = User(username="testuserFOO", hashed_password=hashed_password)
    db.session.add(test_user)
    db.session.commit()
def tearDown(self):
  with app.app_context():
    db.session.remove()
    # Drop tables after testing
    db.drop_all()

why might running unit tests using the above settings corrupt my main db??

Repl link:

Hi there @crchicago

Can you share the link to your repl?

my repl is private, unfortunately. i don’t want to make public, but i’d also like to be able to get help from time to time. have cake, eat cake too situation. hoping i can still get some help.

if possible, create a public repl with the minimum code that reproduces the issue

1 Like

Did you double check to see if there’s no part of your test code or setup that accidentally imports or references the main application’s configuration rather than the TestConfig?

Also, see if you don’t have the main app’s database URI hardcoded anywhere else in your code.

You also could try to simplify your test setup and gradually reintroduce components until you identify the problematic part.

Tbh, best thing is do what @NuclearPasta0 said.