Beispiel #1
0
    def wrapper(*args, **kwargs):
        if not os.path.exists(hash.HASH_LIBRARY_FILE):
            pytest.xfail('Could not find a figure hash library at {}'.format(hash.HASH_LIBRARY_FILE))
        # figure_base_dir is a pytest fixture defined on use.
        if figure_base_dir is None:
            pytest.xfail("No directory to save figures to found")

        name = "{0}.{1}".format(test_function.__module__,
                                test_function.__name__)
        # Run the test function and get the figure
        plt.figure()
        fig = test_function(*args, **kwargs)
        if fig is None:
            fig = plt.gcf()

        # Save the image that was generated
        figure_base_dir.mkdir(exist_ok=True)
        result_image_loc = figure_base_dir / '{}.png'.format(name)
        plt.savefig(str(result_image_loc))
        plt.close()

        # Create hash
        imgdata = open(result_image_loc, "rb")
        figure_hash = hash._hash_file(imgdata)
        imgdata.close()

        new_hash_library[name] = figure_hash
        if name not in hash.hash_library:
            pytest.fail("Hash not present: {0}".format(name))

        if hash.hash_library[name] != figure_hash:
            raise RuntimeError('Figure hash does not match expected hash.\n'
                               'New image generated and placed at {}'.format(result_image_loc))
Beispiel #2
0
    def wrapper(*args, **kwargs):
        if not os.path.exists(hash.HASH_LIBRARY_FILE):
            pytest.xfail(
                f'Could not find a figure hash library at {hash.HASH_LIBRARY_FILE}'
            )
        # figure_base_dir is a pytest fixture defined on use.
        if figure_base_dir is None:
            pytest.xfail("No directory to save figures to found")

        name = "{}.{}".format(test_function.__module__, test_function.__name__)
        # Run the test function and get the figure
        plt.figure()
        fig = test_function(*args, **kwargs)
        if fig is None:
            fig = plt.gcf()

        # Save the image that was generated
        figure_base_dir.mkdir(exist_ok=True)
        result_image_loc = figure_base_dir / f'{name}.png'
        plt.savefig(str(result_image_loc))
        plt.close()

        # Create hash
        imgdata = open(result_image_loc, "rb")
        figure_hash = hash._hash_file(imgdata)
        imgdata.close()

        new_hash_library[name] = figure_hash
        if name not in hash.hash_library:
            pytest.fail(f"Hash not present: {name}")

        if hash.hash_library[name] != figure_hash:
            raise RuntimeError('Figure hash does not match expected hash.\n'
                               'New image generated and placed at {}'.format(
                                   result_image_loc))
Beispiel #3
0
    def wrapper(*args, **kwargs):
        if not os.path.exists(hash.HASH_LIBRARY_FILE):
            pytest.xfail('Could not find a figure hash library at {}'.format(
                hash.HASH_LIBRARY_FILE))

        name = "{0}.{1}".format(test_function.__module__,
                                test_function.__name__)
        # Run the test function and get the figure
        plt.figure()
        fig = test_function(*args, **kwargs)
        if fig is None:
            fig = plt.gcf()

        # Save the image that was generated
        if not os.path.exists(test_fig_dir):
            os.makedirs(test_fig_dir)
        result_image_loc = os.path.join(test_fig_dir, '{}.png'.format(name))
        plt.savefig(result_image_loc)
        plt.close()

        # Create hash
        imgdata = open(result_image_loc, "rb")
        figure_hash = hash._hash_file(imgdata)
        imgdata.close()

        new_hash_library[name] = figure_hash
        if name not in hash_library:
            pytest.fail("Hash not present: {0}".format(name))

        if hash_library[name] != figure_hash:
            raise RuntimeError('Figure hash does not match expected hash.\n'
                               'New image generated and placed at {}'.format(
                                   result_image_loc))
def test_hashes(env):
    hash_list = envs[env]['hashes']
    # Check each figure has a hash that matches
    for fig_path in envs[env]['fig_paths']:
        with open(fig_path, 'rb') as f:
            fhash = hash._hash_file(f)
        fname = fig_path.stem
        if fname in hash_list:
            assert hash_list[fname] == fhash
        else:
            raise RuntimeError(f'The following figure does not have an associated hash: {fname}')
    # Check each hash has a figure
    stems = [p.stem for p in envs[env]['fig_paths']]
    missing = []
    for key in envs[env]['hashes']:
        if key not in stems:
            missing.append(key)

    if len(missing):
        missing = '\n'.join(missing)
        raise RuntimeError(f'The following figure tests are missing an image:\n{missing}')
Beispiel #5
0
    def wrapper(*args, **kwargs):
        if not os.path.exists(hash.HASH_LIBRARY_FILE):
            raise RuntimeError(
                f'Could not find a figure hash library at {hash.HASH_LIBRARY_FILE}'
            )
        # figure_base_dir is a pytest fixture defined on use.
        if figure_base_dir is None:
            raise RuntimeError("No directory to save figures to found")

        name = "{}.{}".format(test_function.__module__, test_function.__name__)
        # Run the test function and get the figure
        plt.figure()
        fig = test_function(*args, **kwargs)
        if fig is None:
            fig = plt.gcf()

        # Save the image that was generated
        figure_base_dir.mkdir(exist_ok=True)
        result_image_loc = figure_base_dir / f'{name}.png'
        # Have to set Software to None to prevent Matplotlib injecting it's version number
        plt.savefig(str(result_image_loc), metadata={'Software': None})
        plt.close('all')

        # Create hash
        imgdata = open(result_image_loc, "rb")
        figure_hash = hash._hash_file(imgdata)
        imgdata.close()

        new_hash_library[name] = figure_hash
        if name not in hash.hash_library:
            pytest.fail(f"Hash not present: {name}")

        expected_hash = hash.hash_library[name]
        if expected_hash != figure_hash:
            raise RuntimeError(
                f'Figure hash ({figure_hash}) does not match expected hash ({expected_hash}).\n'
                f'New image generated and placed at {result_image_loc}')