![Cover image for Automating your API tests using Python and Pytest](https://media.dev.to/cdn-cgi/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmk3ipivpafhhd6p7asr3.png)
Did you ever thought how you can tests your APIs using python? In the article we will learn how we can test our APIs using Python and the pytest fr...
For further actions, you may consider blocking this person and/or reporting abuse
Really good step by step introduction to testing with pytest.
I usually Mock external calls with Postman as I wish not to respect the service, which I don’t own, with my tests.
Postman has a good section here on how to mock external API
We would then inject URL and keys from environment variables 😉 which enable us to change Mocked and Real in our CI/CD without changing our code.
So the actions that do unit tests, will have mocked values, and ones doing integration will have real.
Really nice post! Always good to explain virtual environments while working with Python, and I like the straightforward pytest example you provide
Best practice with unit testing code that calls API's is to mock the API interface, so you don't have to ping an external service (idea of unit tests is to be able to run them very quickly with very little external dependencies, if any). Here's a great RealPython article that walks through how to do this mocking...and just a heads up this might get a little more advanced for some readers, but good to know it's out there!
realpython.com/testing-third-party...
^ heads up, that example uses "nosetest" library (extension of unittest) rather than "pytest"
Here's a great explanation of when it makes sense to actually call the external API in tests: pytest-with-eric.com/pytest-best-p...
For mocking external API calls responses and HTTPretty are my favourite helpers
Thanks! This article was focused more on integration tests than unit tests, I'll take a look at your recommendations.
Have you heard of schemathesis, a tool that automates your API testing to catch crashes and spec violations? Built on top of the widely-used Hypothesis framework for property-based testing.
Also available as a service
Nice! I'll take a look at this one, I've never heard before.
this should be
Python 3.9.7
Platform Windows-10-10.0.19045-SP0
Packages pytest: 7.4.3
pluggy: 1.0.0
Plugins html: 4.1.1
metadata: 3.0.0
The above is my environment and the --html-report=./report.html did not work in this environment.
BTW, Sorry I forgot to mention that your tutorial was excellent. Even though I have been using python for past twenty years, I understood and started using pytest only after reading this. Thanks for the simple and lucid tutorial
How I used pytest-html-reporter library, the recommendation on the documentation is to run:
--html-report=./report/report.html
You can check here more about it:
github.com/prashanth-sams/pytest-h...
Somehow, I am getting errors if my response includes dates. For the time being, I am using --html=./report/report.html. I need to find an alternative way to see a good HTML report that can show results in a graphical manner.
Omg, i'm a surprised with your article! Very nice job, congrats Alicia!
This article was incredible! Congrats on this series on showing how to test our code in different ways, you're awesome!
Thanks for sharing, cousin <3
Amazing how python is easy to work with and thanks for the didactics around the QA version of testing!
Great article, before reading this content I thought tests were something from another world, I'm looking forward to the next ones!
Perfect as always! Can't wait for the front-end test tutorials 🥰
Oi Alícia!
Parabéns pelo conteúdo top!
Que ajude muita gente.
Awesome. Very good article. I started learning Python a couple of weeks back and this aids me in automating one of my applications.