-
Notifications
You must be signed in to change notification settings - Fork 60
Pytest and vcrpy to improve sh.hubstorage tests #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Current coverage is 47.87% (diff: 100%)@@ master #32 diff @@
==========================================
Files 14 14
Lines 1178 1178
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
Hits 564 564
Misses 614 614
Partials 0 0
|
b002127
to
c058106
Compare
@chekunkov All the hubstorage tests were converted to pytest, and works perfectly with/without VCR.py. Last step is to commit all the locally generated cassetes and enable tests for sh.hs back. But these cassetes take a lot of space, about 6.5MB, so after the commit it will be very hard to review the changes itself; so I propose to review the changes, fix and merge it, and add the cassetes & enable the tests in other PR. |
@chekunkov Last changes:
The listed things helped to reduce cassettes size to ~800KB, wonder if it's ok. Most cassettes are already small enough, but probably we could win some space by rewriting tests with largest cassettes.
PS Not all the test modules add new jobs, so initially I added |
"--update-cassettes", action="store_true", default=False, | ||
help="make tests with real services instead of vcr cassettes") | ||
|
||
import shutil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.
Imports should be grouped in the following order:
standard library imports
related third party imports
local application/library specific imports
You should put a blank line between each group of imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I missed this one, sorry!
with my_vcr.use_cassette(cassette_name): | ||
remove_all_jobs(hsproject) | ||
yield | ||
remove_all_jobs(hsproject) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's no need to remove all jobs after the test case, it's only needed to prepare environment for starting test case
request.function.__name__ | ||
) | ||
with my_vcr.use_cassette(cassette_name): | ||
remove_all_jobs(hsproject) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can reduce size of the cassettes even more by excluding this setup if we are using cassettes? It's needed only when you run tests against live servers and brings no value while using requests recorded in the cassettes. E.g. we can do this cleanup only if config.option.update_cassettes:
def pytest_addoption(parser): | ||
parser.addoption( | ||
"--update-cassettes", action="store_true", default=False, | ||
help="make tests with real services instead of vcr cassettes") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about adding another option that doesn't drop existing cassettes, just optionally ignores them? E.g. to check tests with real services, but not override existing cassettes without the need
{'foo': 42}, | ||
{'_key': []}, | ||
{'_key': 'large_test', 'value': 'x' * 1024 ** 2}, | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to use @pytest.mark.parametrize
instead?
Don't forget to add vcrpy to requirements-test.txt |
# - before_record returning None means skipping all the requests | ||
global my_vcr | ||
my_vcr.record_mode = 'all' | ||
my_vcr.before_record_request = lambda request: None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
collection = get_test_collection(hsproject) | ||
clean_collection(collection) | ||
yield collection | ||
clean_collection(collection) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cleaning after the test is not necessary
remove_all_jobs(hsproject) | ||
yield | ||
remove_all_jobs(hsproject) | ||
unset_testbotgroup(hsproject) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove_all_jobs and unset_testbotgroup are not necessary after the test
return str(hsproject.ids.spider(TEST_SPIDER_NAME, create=1)) | ||
|
||
|
||
@my_vcr.use_cassette() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doubt that you need vcr here... get_test_collection doesn't produce requests, cleanup makes sense only when it's running with real services, like in https://github.com/scrapinghub/python-scrapinghub/pull/32/files#diff-5a347e09fdac148d5d13fac9f567fc37R121
@pytest.fixture(autouse=True, scope='session') | ||
def setup_session(hsclient, hsproject, hscollection): | ||
set_testbotgroup(hsproject) | ||
remove_all_jobs(hsproject) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do set_testbotgroup and remove_all_jobs make sense when running from vcrpy cassettes? if not - good candidate for vcr removal and condition like in https://github.com/scrapinghub/python-scrapinghub/pull/32/files#diff-5a347e09fdac148d5d13fac9f567fc37R121
@my_vcr.use_cassette() | ||
@pytest.fixture(scope='session') | ||
def hsspiderid(hsproject): | ||
return str(hsproject.ids.spider(TEST_SPIDER_NAME, create=1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, after some thinking - let's keep it, minor thing and we cannot guarantee the same spider id in dev environment
jobq = hsproject.jobq | ||
for queuename in ('pending', 'running', 'finished'): | ||
info = {'summary': [None]} # poor-guy do...while | ||
while info['summary']: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, this is not Pythonic :P
while True:
info = jobq.summary(queuename)
if not info['summary']:
break
# ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, sure! I just moved the function as-is and didn't notice it, thanks
help="test with real services rewriting existing vcr cassettes") | ||
parser.addoption( | ||
"--ignore-cassettes", action="store_true", default=False, | ||
help="test with real services skipping existing vcr cassettes") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, this has to be added to the tests/conftest.py
, otherwise it doesn't work with pytest tests
otherwise `pytest --ignore-cassettes tests` doesn't work
Please, review.