Python如何通过单元测试检查内部功能?

问题描述 投票:1回答:1

我有以下测试

from unittest.mock import ANY

try:
    from unittest import mock  # python 3.3+
except ImportError:
    import mock  # python 2.6-3.2

import pytest

from data_cleaning import __main__ as data_cleaning


@mock.patch('Repositories.repository.Repository.delete')
@pytest.mark.parametrize("argv", [
    (['-t', 'signals']),
    (['-t', 'signals', 'visualizations']),
    (['-t', 'signals', 'visualizations']),
    (['-d', '40', '--processes', 'historicals'])])
def test_get_from_user(mock_delete, argv):
    with mock.patch('data_cleaning.__main__.sys.argv', [''] + argv):
        data_cleaning.main()

    mock_delete.assert_has_calls([ANY])


pytest.main('-x ../data_cleaning'.split())

它试图涵盖以下代码

import argparse
import logging
import sys

from Repositories import repository
from common import config

_JSONCONFIG = config.config_json()
config.initial_configuration_for_logging()


def parse_args(args):
    parser = argparse.ArgumentParser()
    parser.add_argument('-d', '--days', help='Dias de datos que se quiere mantener.',
                        required=False, type=int, default=30)
    parser.add_argument('-p', '--processes', help='Tablas a procesar.',
                        required=True, nargs='+', choices=['signals', 'not_historicals', 'historicals'])
    return parser.parse_args(args)


def main():
    args = parse_args(sys.argv[1:])
    try:
        repo = repository.Repository()
        for process in args.processes:
            if process == 'signals':
                tables_with_column = get_tables_with_column(_JSONCONFIG['SIGNAL_TABLES'])
                for table, column in tables_with_column:
                    repo.delete(column, table, args.days)
            elif process == 'not_historicals':
                tables_with_column = get_tables_with_column(_JSONCONFIG['NOT_HISTORICALS_TABLES'])
                for table, column in tables_with_column:
                    repo.delete(column, table, args.days)
            elif process == 'historicals':
                tables_with_column = get_tables_with_column(_JSONCONFIG['HISTORICAL_TABLES'])
                for table, column in tables_with_column:
                    repo.delete(column, table, args.days)
                    repo.execute_copy_table_data_from(table, 'historica')

    except AttributeError as error:
        logging.exception(f'AttributeError: {repr(error)}')
    except KeyError as error:
        logging.exception(f'KeyError: {repr(error)}')
    except TypeError as error:
        logging.exception(f'TypeError: {repr(error)}')
    except Exception as error:
        logging.exception(f'Exception: {repr(error)}')


def get_tables_with_column(json_object):
    tables_with_column = convert_values_to_pairs_from(json_object, 'table_name', 'column_name')
    return tables_with_column


def convert_values_to_pairs_from(obj: [dict], ket_to_key: str, key_to_value: str) -> [tuple]:
    return [(item[ket_to_key], item[key_to_value]) for item in obj]

如何通过测试100%覆盖我的代码? 指定的测试用例,它的实现情况如何?我的测试要覆盖哪些内容,才能完全覆盖这个模块?

我应该如何覆盖这段代码的测试?我开始从事单元测试,但我已经2个多月了,我很难理解我应该测试什么。

python unit-testing pytest
1个回答
1
投票

你可以使用 assert_called 函数来断言该方法是否被调用。

在你的例子中,它可以看起来像这样。

@mock.patch('Repositories.repository.Repository')
@pytest.mark.parametrize("argv", ...)
def test_get_from_user(mock_repository, argv):
    with mock.patch('data_cleaning.__main__.sys.argv', [''] + argv):
        data_cleaning.main()
        mock_repository.delete.assert_called_once()
© www.soinside.com 2019 - 2024. All rights reserved.