/pytests-request-example

Simple way to test apis

Primary LanguagePython

Testing APIs with Python

There are a ton of ways to test APIs in python. What I am suggesting here is correct and what I think think the most intuitive python combo to use.

There are three important libraries we are going to leverage:

  • pytest :: this is a test framework that makes it easy to write tests. pytest will run any function in tests/test_*.py with a name that starts with test_*.
  • request a great default choice for alibrary to make HTTP/REST API requests.
  • yarl a library I'm quite fond of that makes writing api urls easier to construct.

Introduction

Install python requirements

pip install -r requirements.txt

Run the tests. Make sure you are running the command in the root of the repository.

pytest

Now make some modifications to tests/test_api.py and run pytest again to watch tests run. Some useful options to know with pytest.

pytest    # normal usage
pytest -s # show all print statements output within tests
pytest -x # stop the tests on the first one that fails

Any commit that you make back to the github repo will automatically trigger github-actions (CI) to run the tests.