ktosiek / pytest-vcr

Py.test integration with VCR.py
MIT License
150 stars 25 forks source link

latest release does not record any cassettes #22

Closed dazza-codes closed 5 years ago

dazza-codes commented 5 years ago

versions

$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.2 LTS"

$ poetry --version
Poetry 0.12.11

$ pip freeze | grep vcr
pytest-vcr==1.0.2
vcrpy==2.0.1

$ pip freeze | grep pytest
pytest==4.4.1
pytest-cov==2.7.1
pytest-forked==1.0.2
pytest-mock==1.10.4
pytest-ordering==0.6
pytest-runner==4.4
pytest-vcr==1.0.2
pytest-xdist==1.28.0

Removed all tests/cassettes and ran pytest --vcr-record=all to replace all the cassettes, but there's no output cassettes and all the tests that should record cassettes are passing. There no custom configuration settings and tests use the basic @pytest.mark.vcr() decorators.

$ pytest --fixtures

----------------------------------------------------------------------------- fixtures defined from pytest_vcr ------------------------------------------------------------------------------
vcr_config
    Custom configuration for VCR.py
vcr
    The VCR instance
vcr_cassette
    Wrap a test in a VCR.py cassette
vcr_cassette_dir
    .venv/lib/python3.7/site-packages/pytest_vcr.py:92: no docstring available
vcr_cassette_name
    Name of the VCR cassette
ktosiek commented 5 years ago

Thank you for the report! Which version are you upgrading from?

dazza-codes commented 5 years ago

Prior versions were (from git commit on Pipfile.lock):

pytest==4.0.2
pytest-vcr==1.0.1
vcrpy==2.0.1

But those versions are not working right now. Don't recall if prior working versions were on python 3.6, but currently using Python 3.7.2.

ktosiek commented 5 years ago

That is strange, especially if downgrade did not help. Do you use unittest.TestCase? Can you:

dazza-codes commented 5 years ago
# all the library imports are good, plus:
import pytest

@pytest.mark.vcr()
def test_fetch_json_with_site_string(dates):
    site = "01115183"
    q = USGSClient.query(site, dt_start=dates.begin, dt_end=dates.end, data_format='json')
    response = USGSClient.fetch(q)
    check_json_response(response)
$ pytest --vcr-record=all tests/test_usgs_fetch.py -k 'test_fetch_json_with_site_string'
============ test session starts ====================
platform linux -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.9.0
rootdir: /home/joe/src/jupiter/usgs-water-services, inifile: pytest.ini
plugins: requests-mock-1.6.0, xdist-1.27.0, vcr-1.0.1, ordering-0.6, mock-1.10.4, forked-1.0.2, cov-2.7.1
collected 6 items / 5 deselected                                                                                                                                                            

tests/test_usgs_fetch.py .                                                                                                                                                            [100%]
$ cat pytest.ini 
[pytest]
log_format = %(asctime)s - %(name)-12s - %(levelname)-8s - %(message)s
log_date_format = %Y-%m-%d %H:%M:%S

filterwarnings =
    module::DeprecationWarning

With or without that pytest.ini file, the behavior is the same. The tests/cassettes directory is the same before/after, i.e.

tests/
├── cassettes

Also added the README test and it passed OK, but without any cassette recordings, i.e.

import pytest
import requests

@pytest.mark.vcr()
def test_iana():
    response = requests.get('http://www.iana.org/domains/reserved').text
    assert 'Example domains' in response
$ pytest -v --vcr-record=all tests/test_usgs_fetch.py
============ test session starts ============
platform linux -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.9.0 -- 
cachedir: .pytest_cache
rootdir: /home/dlweber/src/jupiter/usgs-water-services, inifile:
plugins: requests-mock-1.6.0, xdist-1.27.0, vcr-1.0.1, ordering-0.6, mock-1.10.4, forked-1.0.2, cov-2.7.1
collected 7 items                                                                                                                                                                           

tests/test_usgs_fetch.py::test_iana PASSED                [100%]

Adding an assert False fails the test, as expected.

dazza-codes commented 5 years ago
# pyproject.toml snippets

[tool.poetry.dependencies]
python = "^3.7"
lxml = "^4.3"
numpy = "*"
OWSLib = "^0.17.0"
pandas = "^0.24.0"
pytz = "*"
pyyaml = "*"
requests = "^2.0"
scipy = "*"
urllib3 = "*"

[tool.poetry.dev-dependencies]
alabaster = "*"
black = {version = "^18.3-alpha.0", allows-prereleases = true}
codecov = "*"
docutils = "*"
"flake8" = "*"
"flake8-type-annotations" = "*"
freezegun = "*"
ipdb = "*"
ipython = "*"
invoke = "*"
invoke-release = "*"
pylint = "^2.3"
pytest = "^4.0"
pytest-cov = "*"
pytest-mock = "*"
pytest-ordering = "*"
pytest-runner = "*"
pytest-vcr = "*"
pytest-xdist = "*"
requests_mock = "*"
sphinx = "^2.0"
tox = "*"
detox = "*"
twine = "^1.0"
yapf = "*"
wheel = "*"
dazza-codes commented 5 years ago

Removing lxml (4.3.3) and the cassettes are back again. Issue resolved, sorta.

$ tree tests/
tests/
├── cassettes
│   ├── test_fetch_json_to_data_frames.yaml
│   ├── test_fetch_json_with_site_string.yaml
│   ├── test_fetch_json.yaml
│   └── test_iana.yaml
$ pip freeze | grep pytest
pytest==4.4.1
pytest-cov==2.7.1
pytest-forked==1.0.2
pytest-mock==1.10.4
pytest-ordering==0.6
pytest-runner==4.4
pytest-vcr==1.0.2
pytest-xdist==1.28.0
ktosiek commented 5 years ago

Can you reproduce the issue after adding lxml back? If not, I'll write this off as some kind of bit flip for now.

If anyone else sees a similar problem please comment. I couldn't reproduce the problem, but any additional details might help.

dazza-codes commented 5 years ago

Still works after adding lxml back (got 4.3.3 again).

/shrug

ktosiek commented 5 years ago

Closing this issue until the problem comes back.

alex-hsp commented 4 years ago

I had the simial problem.

My actual http requests were made in a fixture that was used by test marked with vcr. So it seems like vcr was not able to see any requests made from fixture. So i had to to is to manuall wrap with vcr.use_cassette in my fixture.

neozenith commented 4 years ago

We had a similar problem but after running pytest --setup-only --setup-show We figured out that one of the fixtures we expected to be recorded was at the class level and the vcr cassette was only getting mounted at the function level.

The cassette only gets mounted at function level (for recording or replay) to maintain test isolation so each can run in parallel.

We split the fixture so that some setup was class level but the part that needed to be recorded with each test was function level scopes fixtures.

Started working.