/behave/features/steps/service.py

https://github.com/AndyLPK247/behavior-driven-python · Python · 36 lines · 14 code · 13 blank · 9 comment · 0 complexity · 3f4824698f92cebb28801720a6f25ec7 MD5 · raw file

  1. """
  2. This module contains step definitions for service.feature.
  3. It uses the requests package:
  4. http://docs.python-requests.org/
  5. """
  6. import requests
  7. from behave import *
  8. # "Constants"
  9. DUCKDUCKGO_API = 'https://api.duckduckgo.com/'
  10. # Whens
  11. @when('the DuckDuckGo API is queried with')
  12. def step_impl(context):
  13. first_row = context.table[0]
  14. params = {'q': first_row['phrase'], 'format': first_row['format']}
  15. context.response = requests.get(DUCKDUCKGO_API, params=params)
  16. # Thens
  17. @then('the response contains results for "{phrase}"')
  18. def step_impl(context, phrase):
  19. # A more comprehensive test would check 'RelatedTopics' for matching phrases
  20. assert phrase.lower() == context.response.json()['Heading'].lower()
  21. @then('the response status code is "{code:d}"')
  22. def step_impl(context, code):
  23. assert context.response.status_code == code