[英]Automated testing in Flask
I am having a problem with making automated tests for flask in python 3. I have tried unittest, pytests, nosetests but I still can't figure out how to form automated tests for flask application.我在为 python 3 中的 flask 进行自动化测试时遇到问题。我已经尝试过 unittest、pytests、nosetests,但我仍然无法弄清楚如何为 Z319C3206A7F10C17C4B76911 应用程序形成自动化测试。
Following is the code I have wrote using unittest and pytest以下是我使用 unittest 和 pytest 编写的代码
unittest:单元测试:
import unittest
from flaskfolder import flaskapp
class FlaskBookshelfTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
# creates a test client
self.flaskapp = flaskapp.test_client()
# propagate the exceptions to the test client
self.flaskapp.testing = True
def tearDown(self):
pass
def test1(self):
result = self.flaskapp.get('/')
self.assertEqual(result.status_code, 200)
In this code i am having error that flaskapp doesn't has any test_client() function.在这段代码中,我遇到错误,flaskapp 没有任何 test_client() function。
pytest: pytest:
import pytest
from flaskfolder import flaskapp
@pytest.fixture
def client():
db_fd, flaskapp.app.config['DATABASE'] = tempfile.mkstemp()
flaskapp.app.config['TESTING'] = True
with flaskapp.app.test_client() as client:
with flaskapp.app.app_context():
flaskapp.init_db()
yield client
os.close(db_fd)
os.unlink(flaskapp.app.config['DATABASE'])
def test1():
result = client.get('/')
assert result.data in 'Hello World'
In this error that "'function' doesn't has any attribute get" is recieved and if done: def test1(client) it gives an error that flaskapp doesn't have any attribute init_db.在这个错误中,收到“'function'没有任何属性get”,如果完成:def test1(client)它会给出一个错误,即flaskapp没有任何属性init_db。
Because client is a pytest fixture, you need to include it as anargument to your test, so this should solve your current issue因为客户端是 pytest 夹具,您需要将其作为参数包含在您的测试中,所以这应该可以解决您当前的问题
def test1(client):
result = client.get('/')
assert result.data in 'Hello World'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.