-
Notifications
You must be signed in to change notification settings - Fork 588
Deflaking tests by refactoring the model fixture. #34
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ flake8==2.5.4 | |
flaky==3.1.0 | ||
pytest==2.8.7 | ||
pytest-cov==2.2.1 | ||
retrying==1.3.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright 2015 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""conftest.py is used to define common test fixtures for pytest.""" | ||
|
||
import bookshelf | ||
import config | ||
from gcloud.exceptions import ServiceUnavailable | ||
from oauth2client.client import HttpAccessTokenRefreshError | ||
import pytest | ||
from retrying import retry | ||
|
||
|
||
@pytest.yield_fixture(params=['datastore', 'cloudsql', 'mongodb']) | ||
def app(request): | ||
"""This fixtures provides a Flask app instance configured for testing. | ||
|
||
Because it's parametric, it will cause every test that uses this fixture | ||
to run three times: one time for each backend (datastore, cloudsql, and | ||
mongodb). | ||
|
||
It also ensures the tests run within a request context, allowing | ||
any calls to flask.request, flask.current_app, etc. to work.""" | ||
app = bookshelf.create_app( | ||
config, | ||
testing=True, | ||
config_overrides={ | ||
'DATA_BACKEND': request.param | ||
}) | ||
|
||
with app.test_request_context(): | ||
yield app | ||
|
||
|
||
@pytest.yield_fixture | ||
def model(monkeypatch, app): | ||
"""This fixture provides a modified version of the app's model that tracks | ||
all created items and deletes them at the end of the test. | ||
|
||
Any tests that directly or indirectly interact with the database should use | ||
this to ensure that resources are properly cleaned up. | ||
|
||
Monkeypatch is provided by pytest and used to patch the model's create | ||
method. | ||
|
||
The app fixture is needed to provide the configuration and context needed | ||
to get the proper model object. | ||
""" | ||
model = bookshelf.get_model() | ||
|
||
# Ensure no books exist before running. This typically helps if tests | ||
# somehow left the database in a bad state. | ||
delete_all_books(model) | ||
|
||
yield model | ||
|
||
# Delete all books that we created during tests. | ||
delete_all_books(model) | ||
|
||
|
||
# The backend data stores can sometimes be flaky. It's useful to retry this | ||
# a few times before giving up. | ||
@retry( | ||
stop_max_attempt_number=3, | ||
wait_exponential_multiplier=100, | ||
wait_exponential_max=2000) | ||
def delete_all_books(model): | ||
while True: | ||
books, _ = model.list(limit=50) | ||
if not books: | ||
break | ||
for book in books: | ||
model.delete(book['id']) | ||
|
||
|
||
def flaky_filter(info, *args): | ||
"""Used by flaky to determine when to re-run a test case.""" | ||
_, e, _ = info | ||
return isinstance(e, (ServiceUnavailable, HttpAccessTokenRefreshError)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ flake8==2.5.4 | |
flaky==3.1.0 | ||
pytest==2.8.7 | ||
pytest-cov==2.2.1 | ||
retrying==1.3.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ flake8==2.5.4 | |
flaky==3.1.0 | ||
pytest==2.8.7 | ||
pytest-cov==2.2.1 | ||
retrying==1.3.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ flake8==2.5.4 | |
flaky==3.1.0 | ||
pytest==2.8.7 | ||
pytest-cov==2.2.1 | ||
retrying==1.3.3 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
rebase?
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.
No this is right. This file didn't exist before.