I recently started to write test codes and I got curious about setting up database for test.
Let's say that my web server has a feature that calculates all the data from users and verify the output.
In this case, I would need lots of original(remaining) data in my database.
But in another case, I need to check if my web server if it redirects user to the right page according to existence of specific data.
This is my test fixture code and as you can see, in db.teardown it drops all data.
@pytest.fixture(scope='session', autouse=True)
def app(request):
settings_override = {
'DEBUG': False,
'TESTING': True,
'SQLALCHEMY_DATABASE_URI': f'{DB_CONFIG["VENDOR"]}://{DB_CONFIG["USER_NAME"]}:{DB_CONFIG["PWD"]}@{DB_CONFIG["HOST"]}/{DB_CONFIG["SCHEMA"]}'
}
app = _app
app.config.update(settings_override)
ctx = app.app_context()
ctx.push()
def teardown():
ctx.pop()
request.addfinalizer(teardown)
return app
@pytest.fixture(scope='session', autouse=True)
def db(app, request):
_db.app = app
_db.create_all()
def teardown():
_db.drop_all()
request.addfinalizer(teardown)
return _db
@pytest.fixture(scope='session', autouse=True)
def client(app):
_client = app.test_client()
def get_user_config():
conf = {
'user_imin': USER_CONFIG['user_imin'],
'user_token': encrypt_imin_random(USER_CONFIG['user_imin']),
}
return conf
_client.application.config.update(get_user_config())
return _client
@pytest.fixture(scope='function', autouse=True)
def session(db, request):
conn = db.engine.connect()
transaction = conn.begin()
options = dict(bind=conn, binds={})
session = db.create_scoped_session(options=options)
db.session = session
def teardown():
transaction.rollback()
conn.close()
session.remove()
request.addfinalizer(teardown)
return session
My data will still be there if I comment out the line but then I can't test some cases that needs to be done with empty dataset.
I thought setting up more fixture to start with so that I can
I think it would be great if there is an option for test case to decide whether it does its job as if dataset is empty or not.
If there isn't, what is the best case to set up test suite in my case?
Aucun commentaire:
Enregistrer un commentaire