Software Testing and Testing Automation with Python

PyTest-MPL

Purpose

Testing Plots

Often we want to ensure that our methods to plot data continue working in the same way. This is especially useful for ensuring that we catch when other libraries change defaults. To do this we create and store baseline images that we will compare against.

Instructor Code

import pytest

@pytest.mark.mpl_image_compare(remove_text=True)
def test_plotting_meteogram_defaults():
    """Test default meteogram plotting."""
    # Setup
    url = meteogram.build_asos_request_url('AMW',
                                           start_date=datetime.datetime(2018, 3, 26),
                                           end_date=datetime.datetime(2018, 3, 27))
    df = meteogram.download_asos_data(url)


    # Exercise
    fig, _, _, _ = meteogram.plot_meteogram(df)

    # Verify - Done by decorator when run with -mpl flag

    # Cleanup - none necessary

    return fig
pytest -k test_plotting_meteogram_defaults --mpl-generate-path=tests/baseline
Exercise 5

Solution

# Add direction lines if requested
if direction_markers:
    for value_degrees in [0, 90, 180, 270]:
        ax2b.axhline(y=value_degrees, color='k', linestyle='--', linewidth=0.25)
@pytest.mark.mpl_image_compare(remove_text=True)
def test_plotting_meteogram_direction_fiducials():
    """Test meteogram plotting with fiducial lines."""
    # Setup
    url = meteogram.build_asos_request_url('AMW',
                                           start_date=datetime.datetime(2018, 3, 26),
                                           end_date=datetime.datetime(2018, 3, 27))
    df = meteogram.download_asos_data(url)


    # Exercise
    fig, _, _, _ = meteogram.plot_meteogram(df, direction_markers=True)

    # Verify - Done by decorator when run with -mpl flag

    # Cleanup - none necessary

    return fig

Home