test_utils.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:inspire-query-parser 作者: inspirehep 项目源码 文件源码
def parametrize(test_configurations):
    """Custom parametrize method that accepts a more readable test conf. format.

    It accepts a dictionary whose keys are the test names (ids equivalent) and
    the value of each key is a dictionary of test configuration, in the form of
    { test_parameter1: x, test_parameter2: y}

    Example:
        {
            'Unicode tokens': {'query_str': '?-radiation', 'unrecognised_text': ''},
            'Simple token: {'query_str': 'foo', 'unrecognized_text': ''}
        }
    """
    if not test_configurations:
        __tracebackhide__ = True
        pytest.fail('In parametrize test configurations parameter cannot be empty.')

    if not isinstance(test_configurations, dict):
        __tracebackhide__ = True
        pytest.fail('In parametrize test configurations parameter must be a dictionary.')

    ordered_tests_config = OrderedDict(sorted(viewitems(test_configurations)))

    for test_name, test_configuration in iteritems(ordered_tests_config):
        ordered_tests_config[test_name] = OrderedDict(sorted(viewitems(test_configuration)))

    # Extract arg_names from a test configuration
    arg_names = list(iterkeys(next(itervalues(ordered_tests_config))))

    # Generate list of arg_values
    arg_values = [ordered_tests_config[test_config].values() for test_config in ordered_tests_config]

    # Generate ids list
    ids = list(iterkeys(ordered_tests_config))
    return pytest.mark.parametrize(argnames=arg_names, argvalues=arg_values, ids=ids)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号