Introduction
Getting started with testing an application or API can be difficult. You must consider designing, implementing, and maintaining a test infrastructure that appears to operate as its own application. Additionally, it can be confusing just to figure out what type of testing you want to do. Or, what type of testing you can do.
In software development (and in other industries) asking the right question can be just as important as getting the right answer.
In this article, we’ll explain what functional testing is and how it relates to other kinds of testing. Furthermore, we’ll walk through an example of how to set up a functional test with an automated testing tool.
Functional Testing
Many of us already know what makes something functional: it works. Therefore, we can begin to understand that we are testing if somethings works. However, this is too vague and only begins to help our understanding.
Instead, let’s take a look at a technical definition:
“Functional testing is a […] type of black-box testing that bases its test cases on the specifications of the software component under test. Functions are tested by feeding them input and examining the output, and internal program structure is rarely considered […]”
Kaner, Falk, Nguyen. Testing Computer Software. Wiley Computer Publishing, 1999, p. 42. (Wikipedia, Functional testing)
‘Black-box testing’ means that the testers are unaware of how the software is implemented. Therefore, quality assurance (QA) agents are sometimes the people that perform functional tests.
The process is straightforward from the perspective of the functional tester. Inputs are submitted into the component (function) and then outputs are compared to requirements. The requirements cover scenarios in which the component succeeds and scenarios where it fails.
What is a functional requirement?
Tests are pointless without defining the expected, or desired outcome. When building software, different parts of the software have a defined purpose (i.e “logs the user in”). Based on the defined purpose, we can begin to describe the ways that the component should respond provided all the ways that a user interacts with it.
For example, let’s create a couple of scenarios and outcomes involved in a log-in process. Additionally, we’ll assume that the authentication mechanism is an API:
- If the user submits valid credentials, return an HTTP status code of 200 with basic user information.
- If a user submits a username that does not exist, return a status code of 404.
The requirements above are performed from a client outside the application operated by QA. Then, the QA tester can easily determine if the login component is satisfying its functional requirements.
Difference Between Unit Tests and Functional Tests
There are a few differences between functional and unit testing. The main differences relate to where the tests are run, who runs the tests, and the scope of each test.
Unit tests are written by developers and are stored alongside the source code. Furthermore, unit tests are created using a testing framework that relates to the programming language the application is developed with.
This makes it possible to do ‘white box’ testing with unit tests because the tests are part of the application’s immediate environment. Conversely, functional tests are performed without consideration for the implementation (‘black box’ testing).
Types of Software Testing
Next, we’ll walk through a progression of some common testing types. Hopefully, this helps you see how the types work together to cover the processes of an application.
Unit Testing
Unit testing is dividing application code into units and writing tests for each unit. These tests are often independent of the other units in the code. Unit tests are stored and run alongside the source code of an application.
Integration Testing
Integration testing […] is the phase in software testing in which individual software modules are combined and tested as a group.
SO/IEC/IEEE International Standard – Systems and software engineering. ISO/IEC/IEEE 24765:2010(E). 2010. pp. vol., no., pp.1–418, 15 Dec. 2010. (Wikipedia)
Integration tests combine code (that already has unit tests) to fulfill higher-level requirements. This type of testing is similar to functional testing and may include functional requirements and tests.
Regression Testing
According to Software Quality Assurance, Testing and Metrics, regression testing is done to ensure that existing features do not break with new features. This is done by re-running functional tests.
In review, the process begins with requirements for the application. Then, unit tests are created to ensure the lower-level functionality of the code. Next, functional and integration testing is performed that checks the higher-level requirements. Finally, to maintain existing features and performance, regression testing is done by rerunning—and benchmarking—functional test suites.
Functional Testing Example
Previously, we mentioned that functional testing is done from outside the application. Therefore, performing functional tests has fewer constraints on who, what, or where they are executed. This has lead to the creation of many automated testing tools that helps QA, developers, etc. manage their test processes.
In the rest of this article, we are going to walk through a functional testing example with the automated testing tool RapidAPI Testing. This tool is, “…a functional API testing solution for creating and managing comprehensive API tests from development to deployment.”
RapidAPI testing focuses on API testing, but functional tests are run on graphical user interfaces (GUI) as well.
Prerequisites
- RapidAPI Account
- Subscribe to one of RapidAPI Testing‘s plans.
- You can sign up for free with the Basic plan that provides unlimited test cases and 100,000 API calls per month.
- Code (Text) Editor
1. Create API
If you have an existing API on RapidAPI you may use that for the test. However, if you do not, we can easily set up an API using the Pet Store Swagger API Spec.
Get the API Spec File
You can access the JSON data for the sample Swagger spec with this link.
Next, open up your text editor.
Create a file named swagger-pet-store.json
.
Then, copy and paste all the data that you see (on the webpage) into your new file.
Finally, save the file.
I am using VSCode. After I copy the data over and format the JSON, I see:
Now that you have the file, we are ready to create our API.
Add New API
Navigate to the testing dashboard at rapidapi.com/testing/dashboard. You may need to sign in to your RapidAPI account. After signing in, select Create API in the top right of the page.
In the pop-up, enter the new API’s information:
- Select the Context
- Enter an appropriate API name (Pet Store)
- Select the API specification file
- Choose OpenAPI for Definition type
Click OK.
The new API appears on your testing dashboard alongside any existing APIs. Also, it will appear in the account context that you chose when creating the API.
Click on the new API tile to enter the Pet Store API testing dashboard.
2. Build a Test Case
You can view an in-depth testing tutorial for RapidAPI here.
Next, we need a functional test case. Let’s say, after uploading a pet it is available by its ID.
This action requires two endpoints, and possibly even more code modules. However, that code is unit tested and now we are seeing if we can combine separate modules to fulfill a functional purpose.
In the Pet Store API testing dashboard, click Create Test.
Name the test Retrieve Added Pet by ID.
Then, after adding the test, you are taken to the test’s page.
Request Generator
Add Pet
Click on the Request Generator bar at the bottom of the page.
The Request Generator allows us to easily build test steps based on actual API calls.
On the left side panel, under pet, select the addPet endpoint. This populates the middle panel with information relevant to that endpoint
Click on the Body tab in the middle panel. Select “JSON” from the dropdown.
Then, create a unique string of numbers for the ID and enter a name for the new pet in the JSON data.
Next, click the Next button that is to the right of the URL. You should receive an HTTP response with a status code of 200 in the right panel.
Click the Add to Test button.
Finally, In the pop-up, modify the values to the only assert based on a couple of properties:
- status: Assert it equals ‘200’
- data.id: Assert property exists
- data.name: Assert value is of type ‘string’
These assertions, along with the API call, are added to the steps of your test. They are modified by clicking on the individual test steps.
Next, we need to retrieve the pet by the ID that we provide.
Get Pet By ID
First, open up the request generator again.
Select the endpoint getPetById.
Next, in the URL, replace undefined, with the ID of the pet from the first request.
Click the Send button.
Then, after you get a response of 200, click Add to Test.
Lastly, select the following assertions:
- status: Assert it equals ‘200’
- data.id: Assert it equals ‘[the ID you entered]’
- data.name: Assert property exists
We are almost ready to run our test. However, we need to add a cleanup API request so that this test can run multiple times. If we don’t clean up the test, our test will fail because we are trying to add a pet that already exists. Or, the same pet is added many times.
Delete Pet
Follow the same steps that we did for the GetPetById request. Except, this time select deletePet as the endpoint.
The only assertion we need to make is that the response status code equals 200.
Finally, add the assertion to the test.
3. Run Test
We set up the test using three endpoints. Although it may seem like this is a large test, it’s covering an important part of our sample API.
Next, we get to the easy part: running the test. In the top right corner of the Retrieve Added Pet by ID test page, click the Save & Run button.
This manually runs the test. All that is left is to view the results!
Scheduling Tests
RapidAPI Testing provides features for automating tests in a CI/CD pipeline or scheduling tests to run at different intervals from various locations around the world.
You can read more about all the testing features here.
4. View Results
You can view test results in a couple of different locations in RapidAPI testing.
Recent test results are located in the bottom left of the individual test pages.
Additionally, you can click on the Results navigation item.
Clicking on the results in either location opens up the Execution Report.
The Execution Report provides detailed information for each step of the test. Furthermore, you can view the execution time, environment, location, or access the permalink for the results.
Conclusion
Functional testing plays an important role in software testing. In the article, we defined functional testing and how it relates to other types of software testing. Additionally, we walked through how to quickly set up tests using RapidAPI Testing.
There’s more to be done with this testing tool (i.e test variables, environments) but the above example is a good start. I hope this article gave you a working definition and useful context.
Thanks for reading!
Leave a Reply