Writting py test for sqlalchemy app

前端 未结 2 1631
南笙
南笙 2021-02-09 22:30

I am trying convert unit test into py test. I am using the unit test example

class TestCase(unittest.TestCase):
    def setUp(self):
        app.config[\'TESTIN         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-02-09 23:11

    First off, py.test should just run the existing unittest test case. However the native thing to do in py.test is use a fixture for the setup and teardown:

    import pytest
    
    @pytest.fixture
    def some_db(request):
        app.config['TESTING'] = True
        app.config['CSRF_ENABLED'] = False
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db')
        db.create_all()
        def fin():
            db.session.remove()
            db.drop_all()
        request.addfinalizer(fin)
    
    def test_foo(some_db):
        pass
    

    Note that I have no idea about SQLAlchemy and whether there are better ways of handling it's setup and teardown. All this example demonstrates is how to turn the setup/teardown methods into a fixture.

提交回复
热议问题