问题描述
我有一个Flask应用程序,其中REST API是使用flask_restful和MongoDB后端构建的。我想使用pytest和mongomock编写功能测试,以模拟MongoDB来测试API,但无法对其进行配置。任何人都可以指导我提供一个可以实现相同目标的示例吗? 这是我在conftest.py文件中使用的灯具:
@pytest.fixture(scope='module')
def test_client():
# flask_app = Flask(__name__)
# Flask provides a way to test your application by exposing the Werkzeug test Client
# and handling the context locals for you.
testing_client = app.test_client()
# Establish an application context before running the tests.
ctx = app.app_context()
ctx.push()
yield testing_client # this is where the testing happens!
ctx.pop()
@pytest.fixture(autouse=True)
def patch_mongo():
db = connect('testdb',host='mongomock://localhost')
yield db
db.drop_database('testdb')
disconnect()
db.close()
def test_mongo(test_client,patch_mongo):
headers={
"Content-Type": "application/json","Authorization": "token"
}
response=test_client.post('/users',headers=headers,data=json.dumps(data))
print(response.get_json())
assert response.status_code == 200
问题在于pytest不在使用testdb,而是在生产数据库中创建用户。配置中缺少什么吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)