zl程序教程

您现在的位置是:首页 >  Java

当前栏目

【Flask】当企业级项目在大多数视图登录性能优化以及测试覆盖的详细解决方案

2023-02-18 16:28:15 时间

大多数视图

对于大多数视图,用户需要登录。测试中最方便的方法是使用客户端发出POST请求并将其发送到登录视图。不是每次都写,而是写一个类,使用class方法来完成,并使用固件将其传递给每个被测试的客户端。

class AuthActions(object):
    def __init__(self, client):
        self._client = client

    def login(self, username='test', pasord='test'):
        return self._client.post(
            '/auth/login',
            data={'username': username, 'password': password}
        )

    def logout(self):
        return self._client.get('/auth/logout')


@pytest.fixture
def auth(client):
    return AuthActions(client)

通过auth固件,您可以调用authLogin()作为测试用户登录。用户数据已写入应用固件。 注册视图应在GET请求时成功呈现。在POST请求中,当表单数据合法时,视图应重定向到登录URL,并且用户的数据已保存在数据库中。如果数据非法,则应显示错误消息。

import pytest
from flask import g, session
from flaskr.db import get_db


def test_register(client, app):
    assert client.get('/auth/register').status_code == 200
    response = client.post(
        '/auth/register', data={'username': 'a', 'passd': 'a'}
    )
    assert 'http://localhost/auth/login' == response.headers['Location']

    with app.app_context():
        assert get_db().execute(
            "select * from user where username = 'a'",
        ).fetchone() is not None


@pytest.mark.parametrize(('username', 'password', 'message'), (
    ('', '', b'Username is required.'),
    ('a', '', b'Password is required.'),
    ('test', 'test', b'already registered'),
))
def test_register_validate_input(client, username, password, message):
    response = client.post(
        '/auth/register',
        data={'username': username, 'password': password}
    )
    assert message in response.data

客户Get()发出Get请求,Flask返回Response对象。类似的客户端Post()发出Post请求,并将数据字典转换为表单数据。 要测试页面是否成功呈现,请发出一个简单的请求,并检查是否返回了200OK状态_代码如果渲染失败,Flask将返回500内部服务器错误代码。 当注册视图重定向到登录视图时,标头将具有包含登录URL的Location标头。 数据包含以字节为单位的响应正文。如果要在呈现的页面中检测值,请在数据中检测它。字节值只能与字节值进行比较。如果要比较Unicode文本,请使用get_data(as_text=True) pytest.mark。Parameterize告诉Pytest使用不同的参数运行相同的测试。这用于测试不同的非法输入和错误消息,以避免三次写入相同的代码。 登录视图的测试与寄存器的测试非常相似。后者是测试数据库中的数据,前者是会话应该包含测试login_id之后的用户

测试覆盖

为应用程序编写单元测试可以检查代码是否按预期执行。Flask提供了一个测试客户端,它可以模拟向应用程序发送请求并返回响应数据。

INSERT INTO user (username, pa)
VALUES
  ('test', 'pbkdf2:sha256:50000$TCI4GzcX$0de171a4f4dac32e3364c7ddc7c14f3e2fa61f2d17574483f7ffbb431b4acb2f'),
  ('other', 'pbkdf2:sha256:50000$kJPKsz6N$d2d4784f1b030a9761f5ccaeeaca413f27f2ecb76d6168407af962ddce849f79');

INSERT INTO post (title, body, author_id, created)
VALUES
  ('test title', 'test' || x'0a' || 'body', 1, '2018-01-01 00:00:00');

你应该尽可能多地测试。函数中的代码仅在调用函数时运行。分支中的代码(如if块中的代码)只有在满足条件时才会运行。测试应涵盖每个功能和每个分支。

import os
import tempfile

import pytest
from flaskr import create_app
from flaskr.db import get_db, init_db

with open(os.path.join(os.path.dirname(__file__), 'data.sql'), 'rb') as f:
    _data_sql = f.read().decode('utf8')


@pytest.fixture
def app():
    db_fd, db_path = tempfile.mkstemp()

    app = create_app({
        'TESTING': True,
        'DATABASE': db_path,
    })

    with app.app_context():
        init_db()
        get_db().executescript(_data_sql)

    yield app

    os.close(db_fd)
    os.unlink(db_path)


@pytest.fixture
def client(app):
    return app.test_client()


@pytest.fixture
def runner(app):
    return app.test_cli_runner()

越接近100%的测试覆盖率,就越能确保代码修改后不会发生意外。然而,100%的测试覆盖率不能保证应用程序无错误。通常,测试不包括用户如何在浏览器中与应用程序交互。然而,在开发过程中,测试覆盖率仍然非常重要。

from flaskr import create_app


def test_config():
    assert not create_app().testing
    assert create_app({'TESTING': True}).testing


def test_hello(client):
    response = client.get('/hello')
    assert response.data == b'Hello, World!'

Pytest通过将固件函数名与测试函数的参数名匹配来使用固件。例如,下面的write-test _ hello函数有一个客户端参数。Pytest将匹配客户端固件函数,调用此函数,并将返回值传递给测试函数。

def test_init_db_command(runner, monkeypatch):
    class Recorder(object):
        called = False

    def fake_init_db():
        Recorder.called = True

    monkeypatch.setattr('flaskr.db.init_db', fake_init_db)
    result = runner.invoke(args=['init-db'])
    assert 'Initialized' in result.output
    assert Recorder.called