Writing a manual functional test
1. Writing a manual functional test
Welcome back! We just learned how to build a JSON CRUD API. Now let's learn how to test the API endpoints with manual functional tests.2. What Are Functional Tests?
Let's begin by defining functional tests. Since we just covered system tests, we can compare functional tests with them. While system tests focus on isolated system operations, functional tests focus on the integrated system functionality. This means that system tests are designed to validate how specific parts of the system function, whereas functional tests are designed to validate different parts of the system working together. In the case of FastAPI, system tests are scoped to an endpoint. Functional tests are scoped to the whole application. For both, you need a Python environment with access to the running application. On the left, we see a system test. It calls a GET endpoint that validates a successful response via status code. On the right, we see a functional test. It calls a DELETE endpoint and validates the DELETE response. Then it calls a GET endpoint for the same object to validate the "not found" response. The big difference is that the functional test validates the interaction between two endpoints.3. Test Workflows
Test workflows are a critical concept in functional testing. If we just test endpoints randomly, we don't know whether they should succeed in the context of the application or not! Workflows are specific sequences of application actions. Defining workflows forces us to identify endpoint combinations that should succeed or fail. This lets us build functional tests that test the overall application and not just specific endpoints.4. Functional Test Workflow Examples
The CRUD API pattern makes some test workflows quite obvious. For example, we should be able to create an object, then read it. We should also be able to update an object after creating it. And of course, we should be able to delete it. Note that this is not an exhaustive list of successful workflows - we can come up with many more! Failing workflows for CRUD APIs are almost like counter examples. We can't read an object without creating it. We can't update an object after we have deleted it. And we can't delete an object we haven't created. Again, this is not an exhaustive list of failing workflows. Can you think of others?5. Functional Test Scripts
We usually run test workflows using functional test scripts. These scripts typically run outside of a test framework like pytest. This is because we want the tests to run as if they were a real user, who would have no idea what framework we were using! Since we run these tests as scripts, we call them manual tests. The requests module is a great tool for manual functional tests with FastAPI, since it's Python's standard library for HTTP operations. Here is an example of a manual functional test script. In this example, we use the requests library to create item rock with a POST endpoint, and then read item rock with a GET endpoint. Both operations are expected to be successful. It's important to know that functional tests depend heavily on application state. In this example, the create operation should not be successful, if the item rock already exists.6. Let's practice!
Now that we have learned how to write manual functional tests, let's practice what we have learned!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.