Search code examples
pythonapitestingpytestfunctional-testing

Is RESTful API functional testing with pytest possible/a good practice?


I am still a novice programmer and I cannot get my head around how to properly test a RESTful API. I already implemented some unit tests that test each endpoint individually, I would like some tests that test several API endpoints calls in sequence in the same test. Would that be a functional test? Is it usually a common practice? Thanks!


Solution

  • A functional test would be a series of actions simulating user actions on your page. As contrary to unit tests, functional tests aren't aware of your code implementation, they just test the actual outcome - you may even create a functional test in a different programming language or testing framework. Selenium is the recommended tool for creating functional tests in Python.

    So, for example and implying you have a web frontend to your API, a test could be named test_anonymous_user_cannot_retrieve_data. You initialize and open a browser on the related page and check that, for example, "Authorization required" header is showing.

    Another functional test could be test_authenticated_user_can_retrieve_data. Open browser, use a routine that will log in test user with hardcoded credentials, go to the related page and assert there's test data actually presented to the test user.

    I suggest buying (even if you can read it online for free) and reading Obey the Testing Goat!.