Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flask 测试烧瓶时,在测试之间有公共数据库_Flask - Fatal编程技术网

Flask 测试烧瓶时,在测试之间有公共数据库

Flask 测试烧瓶时,在测试之间有公共数据库,flask,Flask,我正在对DB运行DDL,并在每个测试的设置中创建一个应用程序上下文。有没有办法只运行一次?我正在使用test.py发现并运行这些测试 class TestStuff(unittest.TestCase): def setUp(self): self.app_context = app.app_context() self.app_context.push() db.create_all() # TODO remove def te

我正在对DB运行DDL,并在每个测试的设置中创建一个应用程序上下文。有没有办法只运行一次?我正在使用test.py发现并运行这些测试

class TestStuff(unittest.TestCase):
    def setUp(self):
        self.app_context = app.app_context()
        self.app_context.push()
        db.create_all() # TODO remove

    def tearDown(self):
        db.session.remove()
        db.drop_all() # TODO remove
        self.app_context.pop()

    def test_lookup(self):
        dostuff()

“测试之间”指的是同一类中的测试之间以及测试类本身之间的测试。

您可以使用SetupClass和tearDownClass,以便每个类只执行一次昂贵的操作

class TestStuff(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        db.create_all()

    @classmethod
    def tearDownClass(cls):
        db.drop_all()

    def setUp(self):
        self.app_context = app.app_context()
        self.app_context.push()

    def tearDown(self):
        db.session.remove()
        self.app_context.pop()

    def test_lookup1(self):
        dostuff()

    def test_lookup2(self):
        dostuff()
`

对于测试之间的设置和拆卸,您可以使用 模块中的任何测试将只调用设置和拆卸模块一次

import unittest


def setUpModule():
    print('Expensive Setup')


def tearDownModule():
    print('Expensive Tear Down')


class TestStuff(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        pass

    @classmethod
    def tearDownClass(cls):
        pass


class Test1(TestStuff):
    def test_1(self):
        print('Test1')


class Test2(TestStuff):
    def test_1(self):
        print('Test1')


if __name__ == '__main__':
    unittest.main()

在多个类之间共享数据库怎么样?你能提供点什么吗?