Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE</strong>: Tested and fixed</p> <p>Instead of making a new connection and re-creating your database every time (slow), you can use subsessions and do a rollback after each test.</p> <p>The connection are reused, so this also fix the problem you're having.</p> <pre><code>class TestCase(Base): @classmethod def setUpClass(cls): cls.app = create_app(MyConfig()) cls.client = cls.app.test_client() cls._ctx = cls.app.test_request_context() cls._ctx.push() db.create_all() @classmethod def tearDownClass(cls): db.session.remove() db.drop_all() db.get_engine(cls.app).dispose() def setUp(self): self._ctx = self.app.test_request_context() self._ctx.push() db.session.begin(subtransactions=True) def tearDown(self): db.session.rollback() db.session.close() self._ctx.pop() </code></pre> <p>If you need to also make an instance of the application for each test, just add it to the <code>setUp</code> method but leave it also in <code>setUpClass</code>.</p> <p>Full test example below requires flask_sqlalchemy and psycopg2. Create a test database named "test" and set its connection limit to 15.</p> <pre><code>from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy from unittest import TestCase as Base db = SQLAlchemy() def create_app(config=None): app = Flask(__name__) app.config.from_object(config) db.init_app(app) return app class MyConfig(object): SQLALCHEMY_DATABASE_URI = "postgresql://localhost/test" TESTING = True class TestCase(Base): @classmethod def setUpClass(cls): cls.app = create_app(MyConfig()) cls.client = cls.app.test_client() cls._ctx = cls.app.test_request_context() cls._ctx.push() db.create_all() @classmethod def tearDownClass(cls): db.session.remove() db.drop_all() def setUp(self): self._ctx = self.app.test_request_context() self._ctx.push() db.session.begin(subtransactions=True) def tearDown(self): db.session.rollback() db.session.close() self._ctx.pop() class TestModel(TestCase): def test_01(self): pass def test_02(self): pass def test_03(self): pass def test_04(self): pass def test_05(self): pass def test_06(self): pass def test_07(self): pass def test_08(self): pass def test_09(self): pass def test_10(self): pass def test_11(self): pass def test_12(self): pass def test_13(self): pass def test_14(self): pass def test_15(self): pass def test_16(self): pass if __name__ == "__main__": import unittest unittest.main() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload