pytest-vcrpandas

Test from HTTP interactions to dataframe processed.


License
MIT
Install
pip install pytest-vcrpandas==0.0.2

Documentation

https://travis-ci.com/gjeusel/pytest-vcrpandas.svg?branch=master Documentation Status Pypi package

pytest-vcrpandas

Combine vcrpy with pandas to not only mock your HTTP interactions, but also tests your post-processing work to get a pandas DataFrame.

Usage

# test_meteo_client.py

def test_meteo_get_wind_speed(vcrpandas, client):
    start = pd.Timestamp('2018-01-01', tz='CET')
    end = pd.Timestamp('2018-02-01', tz='CET')
    with vcrpandas("meteo_get_wind_speed") as recorder:
        df = client.get_wind_speed(start, end)
        recorder(df)
# call pytest for the first time to generate samples
> pytest --vcr-record="new_episodes"

# you can now replay it:
> pytest

Behind the hood, 2 files are generated by the first run:

  • a meteo_get_wind_speed.yaml, which is the reponse obtained by the first HTTP interaction.
  • a meteo_get_wind_speed.pickle that serve to compare the formatting from the mocked response to the pandas DataFrame, types included.

Installation

pip install pytest-vcrpandas

Advanced Usage

To configure VCR, here is how to procee:

@pytest.fixture
def my_vcrpandas(vcrpandas):
    custom_vcr_config = dict(
        serializer='json',
        cassette_library_dir='fixtures/myclient/cassettes',
        match_on=['uri', 'method'],
        filter_headers=['authorization'],  # filter information from HTTP Headers
        filter_query_parameters=['api_key'],  # filter information from HTTP query string
        filter_post_data_parameters=['client_secret'],  # filter information from HTTP post data
    )
    vcrpandas.config.update(custom_vcr_config)
    return vcrpandas


def test_mysupertest(my_vcrpandas):
    with my_vcrpandas("random_bucket_name") as recorder:
        recorder(CL.get_stuff())

Refere to `VCR configuration <https://vcrpy.readthedocs.io/en/latest/configuration.html`_ and VCR Advanced Features to get the full list of options.