Skip to content

Instantly share code, notes, and snippets.

@messa
Last active February 16, 2017 13:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save messa/58c58aabf73a946efe72268df805159b to your computer and use it in GitHub Desktop.
Save messa/58c58aabf73a946efe72268df805159b to your computer and use it in GitHub Desktop.
Easier testing API reponses - Pyvo lightning talk
def test_classic_way_with_asserts(testdb):
campaign_id = insert_fixture_campaign(testdb)
app = App(testdb)
response = app.get_campaign(campaign_id)
assert response['name'] == 'Test campaign'
assert response['slug'] == 'test-campaign'
assert response['targeting']['segment'] == 'customers'
# assert ...
# ----------------------------------------------------------------
def test_against_stored_response(testdb):
campaign_id = insert_fixture_campaign(testdb)
app = App(testdb)
response = app.get_campaign(campaign_id)
with open('output_get_campaign.yaml', 'w') as f:
f.write(yaml.dump(response))
assert not git_dirty('output_get_campaign.yaml')
# to see what has changed:
# $ git diff output_get_campaign.yaml
# to mark response as valid:
# $ git add output_get_campaign.yaml
def git_dirty(path):
status = subprocess.check_output(
['git', 'status', '--porcelain', path],
universal_newlines=True)
return len(status) >= 2 and status[1] != ' '
# ----------------------------------------------------------------
def test_create_campaign_db_snapshot(testdb):
app = App(testdb)
app.create_campaign(id=100, name='Test campaign', ...)
dump_db(testdb, 'db_after_create_campaign.yaml')
assert not git_dirty('db_after_create_campaign.yaml')
def test_get_campaign_based_on_db_snapshot(testdb):
# instead of "expensive" setup we can just recreate the db content
# from snapshot created in the previous test
restore_db(testdb, 'db_after_create_campaign.yaml')
app = App(testdb)
response = app.get_campaign(100)
with open('output_get_campaign.yaml', 'w') as f:
f.write(yaml.dump(response))
assert not git_dirty('output_get_campaign.yaml')
'''
Tips:
- yaml.dump(obj, default_flow_style=False, width=120)
- normalize random ids, datetimes etc. before saving
'''
@messa
Copy link
Author

messa commented May 19, 2016

Do you have any feedback? Leave a comment! 🎈

One feedback from yesterday meetup: the function git_dirty should be tested 😃

@messa
Copy link
Author

messa commented Sep 21, 2016

Just a note: this approach is also used for regression testing of rendering React components - it's called Snapshot testing.

https://facebook.github.io/jest/blog/2016/07/27/jest-14.html

@Aprillion
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment