question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] usage for doctests

See original GitHub issue

Foreword: thank you for this plugin, I like the API, and it is really useful.

Is your feature request related to a problem? Please describe. The plugin works very well for normal tests, but I’d really like to use it with doctests. I run those with pytest --doctest-modules and/or pytest --doctest-glob="*.rst". I can define a conftest.py file, but in it I can define fixtures, and not tests, so I cannot use the pytest.mark.vcr

Describe the solution you’d like I’d like to be able to configure the tests to use a cassette/several cassettes for the tests in (say) the README and the modules, and then the pytest --doctest-modules would record and use the cassette instead of calling the network.

Describe alternatives you’ve considered I can add this in conftest.py:

from vcr import use_cassette

@pytest.fixture(autouse=True)
def with_cassette():
    with use_cassette("tests/doctests/cassette.yaml", record_mode="once"):
        yield

But this does not respect whatever will be passed to the command line arguments as it hardcodes the cassette and record mode. If it creates a cassette for each file, that would be better, instead of using one big cassette.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
FlorianGDcommented, Nov 24, 2021

For what it’s worth, I hacked something like this, I put it here for others / if I do not have the bandwidth to make a proper PR:

In conftest.py

import vcr 

@pytest.fixture(autouse=True)
def with_cassette(request):  # type: ignore
    # nodeid is like `filepath::module`
    name = request.node.nodeid.split("::")[1].replace(".", "_")
    with vcr.use_cassette(
        f"tests/doctests/cassette_{name}.yaml",
        record_mode="once",
        filter_headers=["authorization"],
    ):
        yield
1reaction
FlorianGDcommented, Nov 24, 2021

OK, thanks for your quick reply! If I come up with something usable, I’ll make a PR, but not sure I can do it in the next few weeks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

doctest — Test interactive Python examples — Python 3.11.1 ...
The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they...
Read more >
How To Write Doctests in Python - DigitalOcean
We'll create a new file — counting_vowels.py — in our text editor, you can use nano on the command line, like so: nano...
Read more >
Python's doctest: Document and Test Your Code at Once
In this tutorial, you'll learn how to add usage examples to your code's documentation and docstrings and how to use these examples to...
Read more >
Function Doctests
The strength of Doctests is that they are written right next to the code they test and require no extra setup, so they...
Read more >
doctest – Testing through documentation - PyMOTW
To run the tests, use doctest as the main program via the -m option to the ... Examples cannot usually stand on their...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found