flask_testing.TestCase
有自己的create_app
方法。
我们在测试所在目录上方的文件夹create_app
中定义一个__init__.py
方法。
我正在编写一个pytest固定装置,它调用我们的create_app
方法:
conftest.py
import pytest
from above_folder import create_app
@pytest.fixture(scope='session', autouse=True)
def app():
app = create_app()
return app
test_something.py
from flask_testing import TestCase
@pytest.mark.usefixtures('app')
class TestSomething(TestCase):
def test_something(self):
assert True
这会导致以下错误:
============================================================== FAILURES ==============================================================
_____________________________________________________ TestSomething.test_something _____________________________________________________
self = <above_folder.tests.test_something.TestSomething testMethod=test_something>, result = <TestCaseFunction 'test_something'>
def __call__(self, result=None):
"""
Does the required setup, doing it here
means you don't have to call super.setUp
in subclasses.
"""
try:
> self._pre_setup()
/usr/local/lib/python3.6/site-packages/flask_testing/utils.py:131:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python3.6/site-packages/flask_testing/utils.py:137: in _pre_setup
self.app = self.create_app()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <above_folder.tests.test_something.TestSomething testMethod=test_something>
def create_app(self):
"""
Create your Flask app here, with any
configuration you need.
"""
> raise NotImplementedError
E NotImplementedError
/usr/local/lib/python3.6/site-packages/flask_testing/utils.py:122: NotImplementedError
====================================================== 1 failed in 1.50 seconds =====================================================
这是您在执行类似操作时遇到的错误:
from flask_testing import TestCase
t = TestCase()
t.create_app()
...表示灯具正在使用flask_testing.TestCase
的方法,而不是我们在 conftest.py 中导入的方法。
现在,如果我们做这样的事情:
other_file.py
from flask_testing import TestCase
from above_folder import create_app
class OtherTestCase(TestCase):
def create_app(self):
return create_app()
并更改TestSomething
测试类,使其基类为OtherTestCase
,然后一切都会正常进行。我也尝试在灯具中对create_app
进行别名处理,但是收到了相同的错误。
为什么灯具使用其所属类的create_app
方法,而不使用灯具本身的定义中的create_app
方法?