Giter Club home page Giter Club logo

pytest-vcr-delete-on-fail's Introduction

pytest-vcr-delete-on-fail

PyPI PyPI - Python Version CI Status Coverage status

Sonarqube ratings: A

License: GPL-3.0 Maintained! Code style: black

A pytest plugin that automates vcrpy cassettes deletion on test failure.

$ pip install pytest-vcr-delete-on-fail

Then, in your test:

import pytest
import requests
import vcr

my_vcr = vcr.VCR(record_mode="once")

cassette_path = "tests/cassettes/this.yaml"

@pytest.mark.vcr_delete_on_fail(cassette_path)
def test_this():
    with my_vcr.use_cassette(cassette_path):
        requests.get("https://github.com")
    assert False

In this example a cassette will be saved on disk when exiting the use_cassette context manager, but since the test eventually fails, the cassette will be deleted after the test teardown.

Rationale

Sometimes when testing a function containing multiple http requests a failure will occur halfway through (this happens all the time when doing TDD). When using vcrpy to cache http requests, this could result in a test cache that only cover a fraction of the function under test, which in turn could prevent the function to ever succeed or the test to pass in subsequent run if the http requests that didn't get cached depended on a fresh context (maybe they are time sensitive or there's randomness involved).

This possibility leads to doubt and lack of trust towards the test suite, which is wrong on too many level.

This plugin provides tools to solve this uncertainty, by deleting a test http requests cache if it fails, so that it can start fresh on the next run.

Docs

More information and examples can be found in the in-depth documentation.

Development

Install invoke and poetry:

$ pip install invoke poetry

Now clone the git repo:

$ git clone https://github.com/CarloDePieri/pytest-vcr-delete-on-fail.git
$ cd pytest-vcr-delete-on-fail
$ inv install

This will try to create a virtualenv based on python3.8 and install there all project's dependencies. If a different python version is preferred, it can be selected by specifying the --python (-p) flag like this:

$ inv install -p python3.9

The test suite can be run with commands:

$ inv test         # run the test suite
$ inv test-cov     # run the tests suite and produce a coverage report

To run the test suite against all supported python version (they must be in path!) run:

$ inv test-all-python-version

To test the GitHub workflow with act:

$ inv act-dev               # test the dev workflow
$ inv act-dev -c shell      # open a shell in the act container (the above must fail first!)
$ inv act-dev -c clean      # stop and delete a failed act container

To write the documentation with autobuild and livereload launch:

$ inv docs-serve

pytest-vcr-delete-on-fail's People

Contributors

carlodepieri avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

clementj18

pytest-vcr-delete-on-fail's Issues

Drop support for old class setup logic

All code trying to solve the 'delete the class setup fixture cassette' problem must be removed:

  • it's an ugly, flimsy hack
  • it's not compatible with pytest 7.x

This includes:

  • has_class_scoped_setup_failed
  • has_class_scoped_teardown_failed
  • all relative tests and documentation

We will offer an alternative via #6.

Test Suite refactor

We need to refactor the test suite:

  • tests-in-strings is a bad approach, difficult to debug
  • it would be better to move those test to a separate library, inside actual files
  • investigate the use of pytester.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.