我有一个简单的烧瓶应用程序,我想使用 pytest 对其进行测试。
我的conftest.py:
@pytest.fixture
def app(self):
app = create_app(TestingConfig)
return app
Run Code Online (Sandbox Code Playgroud)
我的test_view.py:
class TestMainView:
def test_ping(self, client):
res = client.get(url_for('main.index'))
assert res.status_code == 200
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,pytest它会抛出一个错误说:
fixture 'client' not found
> available fixtures: app, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory
> use 'pytest --fixtures [testpath]' for help on them.
Run Code Online (Sandbox Code Playgroud)
我有另一个测试文件,我在其中测试默认配置并通过:
class TestTestingClass(object):
app = create_app(TestingConfig)
def test_app_is_test(self):
''' test the test enviroment is set okay and works''' …Run Code Online (Sandbox Code Playgroud) 因此,我仍在使用Flask-mysql(请不要仅出于这个原因就不要投票……)。
我正在获取数据库上下文(mysql变量),并且可以在数据库上查询/获取结果。只是插入不起作用:它没有抱怨(抛出异常)。它True从insert方法返回。
这应该在提交时插入记录来完成,但是由于某些原因,当我用观看MySQL数据库时MySQL Workbench,表中没有插入任何内容(并且不会从insert方法中引发异常):
我将其传递给insertCmd:
"INSERT into user(username, password) VALUES ('test1','somepassword');"
Run Code Online (Sandbox Code Playgroud)
我已经检查了数据库中列的长度,并将命令复制到MySQL Workbench其中(它成功将行插入到表中)。
我很茫然。我看过的所有示例似乎都遵循这种格式,并且我具有良好的数据库上下文。您可以在评论中看到我尝试过的其他内容。
def insert(mysql, insertCmd):
try:
#connection = mysql.get_db()
cursor = mysql.connect().cursor()
cursor.execute(insertCmd)
mysql.connect().commit()
#mysql.connect().commit
#connection.commit()
return True
except Exception as e:
print("Problem inserting into db: " + str(e))
return False
Run Code Online (Sandbox Code Playgroud)