質問

This is my application (using bottle):

import bottle
app = bottle.Bottle()
@app.get('/')
def home(db):
  return bottle.template('Hi!')
if __name__ == '__main__':
  bottle.install(bottle_sqlite.SQLitePlugin(dbfile='data.db'))
  bottle.run()

This is a unit test (with webtest and unittest):

import webtest
def test_home():
  app = webtest.TestApp(example.app)
  assert app.get('/').status == '200 OK'

It doesn't work, because data.db is not available during testing. The goal is to somehow inject that db into the application before running the tests. How should it be done?

役に立ちましたか?

解決

you should either create a mockup of the SQLitePlugin that fakes a database (by using a python dictionary), or create and destroy a SQLite database (with a dataset matching your tests) before and after each set of tests.

basically, you could do something like:

import webtest
def test_home():
  plugin = bottle_sqlite.SQLitePlugin(dbfile='my_test_data_set.db')
  example.app.install(plugin)
  app = webtest.TestApp(example.app)
  assert app.get('/').status == '200 OK'
  example.app.uninstall(plugin)

or

import webtest
def test_home():
  plugin = bottle_sqlite.SQLitePluginMockup(dataset)
  example.app.install(plugin)
  app = webtest.TestApp(example.app)
  assert app.get('/').status == '200 OK'
  example.app.uninstall(plugin)

where dataset would be a "hand made" dictionary faking the dataset for your application.

Or you may be able to do it once and for all in global context of your test script.

HTH

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top