sen*_*aps 4 python pytest flask
我有一个简单的烧瓶应用程序,我想使用 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'''
assert self.app.config['SECRET_KEY'] == 'something hard'
assert self.app.config['DEBUG'] == True
assert self.app.config['TESTING'] == True
assert current_app is not None
assert self.app.config['SQLALCHEMY_DATABASE_URI'] == 'sqlite:////tmp/testing.db'
Run Code Online (Sandbox Code Playgroud)
编辑:我能够通过更改为:
conftest.py:
@pytest.fixture
def app():
app = create_app(TestingConfig)
return app
@pytest.fixture
def client(app):
return app.test_client()
Run Code Online (Sandbox Code Playgroud)
和我的测试文件:
def test_index(client):
assert True
Run Code Online (Sandbox Code Playgroud)
但是,test_index如果是这样,我仍然无法通过:
def test_index(client):
assert client.get(url_for('main.index')).status_code == 200
Run Code Online (Sandbox Code Playgroud)
但这一次,我收到一条错误消息,指出:
RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be executed when application context is available.
Run Code Online (Sandbox Code Playgroud)
我尝试了很多不同的东西。我更新pip了这个项目virtualenvironment并更新了pytest和pytest-flask。但没有一个工作。
我能够通过以下测试:
pytest和。pytest-flaskvirtualenvironmentflask-pytest我删除它的包(在env)virtualenvironment。我不知道这与测试有什么关系,但它有效。唯一不同的是我没有安装所说的flask-pytest东西。
小智 5
您需要删除不正确的(请参阅正确的client fixture实现)。pytest-flask
之后您需要安装pytest-flask并pytest在 内部virtualenv,您最好删除系统范围内的以避免混淆。
之后您应该能够运行测试。
| 归档时间: |
|
| 查看次数: |
7010 次 |
| 最近记录: |