예제 #1
0
def pytest_assertrepr_compare(config, op, left, right):
    if type(left) != type(right) or op != "==":
        return

    explanation = []

    if isinstance(left, Pack):
        if left.name != right.name:
            if diff := assertrepr_compare(config, op, left.name, right.name):
                explanation += ["", "Differing attribute 'name':"] + diff[1:]
        if left.extra != right.extra:
            explanation += generate_explanation(config, left.extra,
                                                right.extra, "file")
        if dict(left) != dict(right):
            explanation += generate_explanation(config, left, right,
                                                "namespace")
예제 #2
0
def pytest_assertrepr_compare(config, op, left, right):
    left_name, right_name = inspect.stack()[7].code_context[0].lstrip().lstrip('assert').rstrip('\n').split(op)
    pytest_output = assertrepr_compare(config, op, left, right)
    logging.debug("{0} is\n {1}".format(left_name, left))
    logging.debug("{0} is\n {1}".format(right_name, right))
    with allure.step("校验结果"):
        allure.attach(str(left), left_name)
        allure.attach(str(right), right_name)
    return pytest_output
예제 #3
0
def pytest_assertrepr_compare(config, op, left, right):
    if op != '==':
        return

    if isinstance(left, TypeComparableDict):
        left = left.data

    if isinstance(right, TypeComparableDict):
        right = right.data

    return assertrepr_compare(config, op=op, left=left, right=right)
def pytest_assertrepr_compare(config, op, left, right):
    """
    Provide field-by-field comparisons if someone
    uses `assert a==b` to compare two entities.
    """
    left_ent = isinstance(left, Entity)
    right_ent = isinstance(right, Entity)
    if not (left_ent or right_ent):
        return None
    if left_ent:
        left = left.flatten()
    if right_ent:
        right = right.flatten()
    return assertrepr_compare(config, op, left, right)
예제 #5
0
def pytest_assertrepr_compare(config, op, left, right):
    if type(left) != type(right) or op != "==":
        return

    explanation = []

    if isinstance(left, Document):
        if left.assets != right.assets:
            explanation += ["", "Differing resource pack:"]
            explanation += generate_explanation(config, left.assets,
                                                right.assets)
        if left.data != right.data:
            explanation += ["", "Differing data pack:"]
            explanation += generate_explanation(config, left.data, right.data)

    if explanation:
        return [assertrepr_compare(config, op, left, right)[0]] + explanation
예제 #6
0
def pytest_assertrepr_compare(config, op, left, right):  # TODO: unit-test :pragma:nocover:
    if op == 'in' and '\n' in left:
        # Convert 'in' comparisons to '==' comparisons, for more usable error messaging.
        # Truncate the right-hand-side such that it has the longest common prefix with the LHS,
        # and the longest common suffix as well.
        # Given the diff of the two, this should pinpoint the difference.
        beginning = end = None
        for i in range(len(left)):
            if beginning and end:
                break

            if beginning is None and left[:i + 1] not in right:
                beginning = left[:i]

            if end is None and left[-i - 1:] not in right:
                end = left[-i:]

        right = right.split(beginning, 1)[-1].rsplit(end, 1)[0]
        right = ''.join((beginning, right, end))

        from _pytest.assertion.util import assertrepr_compare
        return assertrepr_compare(config, '==', left, right)
예제 #7
0
def pytest_assertrepr_compare(config: Config, op: str, left: Any,
                              right: Any) -> Optional[List[str]]:
    return util.assertrepr_compare(config=config,
                                   op=op,
                                   left=left,
                                   right=right)
def pytest_assertrepr_compare(config, op, left, right):
    return util.assertrepr_compare(config=config, op=op, left=left, right=right)
예제 #9
0
            explanation += generate_explanation(config, left.extra,
                                                right.extra, "file")
        if dict(left) != dict(right):
            explanation += generate_explanation(config, left, right,
                                                "namespace")
    elif isinstance(left, Namespace):
        if left.extra != right.extra:
            explanation += generate_explanation(config, left.extra,
                                                right.extra, "file")
        if dict(left) != dict(right):
            explanation += generate_explanation(config, left, right,
                                                "container")
    elif isinstance(left, NamespaceContainer):
        explanation += generate_explanation(config, left, right, "file")

    if explanation and (diff := assertrepr_compare(config, op, left, right)):
        return [diff[0]] + explanation


def plural(name, count):
    return name + bool(count - 1) * "s"


def generate_explanation(config, left, right, item_name):
    verbose = config.getoption("verbose")

    left_keys = set(left)
    right_keys = set(right)

    common = left_keys & right_keys
    same = {key for key in common if left[key] == right[key]}
예제 #10
0
def diff_eq(left, right):
    from _pytest.assertion.util import assertrepr_compare
    from pytest import config
    for line in assertrepr_compare(config, '==', left, right):
        print(line)
예제 #11
0
def diff_eq(left, right):
    from _pytest.assertion.util import assertrepr_compare
    from pytest import config
    for line in assertrepr_compare(config, '==', left, right):
        print(line)
예제 #12
0
 def print_compare(self, left, right):
     cfg = Config({'verbose': 2})
     output = assertrepr_compare(cfg, '==', left, right)
     if output:
         print('\n'.join(output) + '\n----')