I'm trying to test my flask application using unittest. I want to refrain from flask-testing because I don't like to get ahead of myself.

I've really been struggling with this unittest thing now. It is confusing because there's the request context and the app context and I don't know which one I need to be in when I call db.create\_all().

It seems like when I do add to the database, it adds my models to the database specified in my app module (**init**.py) file, but not the database specified in the setUp(self) method.

I have some methods that must populate the database before every test\_ method.

How can I point my db to the right path?

```
def setUp(self):
    #self.db_gd, app.config['DATABASE'] = tempfile.mkstemp()
    app.config['TESTING'] = True
    # app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE']
    basedir = os.path.abspath(os.path.dirname(__file__))
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
        os.path.join(basedir, 'test.db')
    db = SQLAlchemy(app)
    db.create_all()
    #self.app = app.test_client()
    #self.app.testing = True
    self.create_roles()
    self.create_users()
    self.create_buildings()
    #with app.app_context():
    #   db.create_all()
    #   self.create_roles()
    #   self.create_users()
    #   self.create_buildings()

def tearDown(self):
    #with app.app_context():
    #with app.request_context():
    db.session.remove()
    db.drop_all()
    #os.close(self.db_gd)
    #os.unlink(app.config['DATABASE'])

```

Here is one of the methods that populates my database:

```
def create_users(self):
    #raise ValueError(User.query.all())
    new_user = User('Some User Name','xxxxx@gmail.com','admin')
    new_user.role_id = 1
    new_user.status = 1
    new_user.password = generate_password_hash(new_user.password)
    db.session.add(new_user)

```

Places I've looked at:

<http://kronosapiens.github.io/blog/2014/08/14/understanding-contexts-in-flask.html>

<http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvi-debugging-testing-and-profiling>

And the flask documentation:
<http://flask.pocoo.org/docs/0.10/testing/>