Introduction
Writing GraphQL Test Cases in Laravel is a bit challenging. Here we will explain how to write the test cases for laravel with graphQL. Writing the test cases for our application is a great way to develop the software with effeciency. There are so many ways to write the test cases like tdd, bdd, ddd develop the software. Before we move further, you must be familiar with the graphql mutation, queries, type, endpoint, schemas. We are using the graphql rebing package here.
Why Do We Write Test Cases?
- Writing the test cases for our application is a good practice when we are building a large scale application. I have covered all the scenarios like adding the new user to check validation, expected outputs, user responses, and identify any potential errors or application difficulties.
- It avoids unwanted errors and bugs in the application during its creation.
- To double check that all functionality code meets quality standards before the installment.
Configure The Write Test Cases
- Laravel supports the testing with in-built phpunit.xml files. These files will set up a system to run the test cases. These files are located at the laravel project root directory.
- In the phpunit.xml file so we create the one new env like .env.testing file in this file connection in another database for testing purposes. Now at this stage we are all set to write the test cases.
- We can use the RefreshDatabase trait inside the class for every time to remove the records from the database and create the new records.
- We are migrating the table using the php artisan migrate –env=testing, so in the testing database all tables are migrated.
How to Create & Run Test Cases
- We can create the test cases class by using the php artisan make:test NameOfTest command. This class is located inside the root directory in the tests folder. Now we can go deep about the test cases.
- Naming convention for test class is as Test.php
- The test cases file is created in the tests/Feature directory using the make test command.
- We have written all the test cases after the coding.
- After writing test cases, run them with the php artisan test.
- We run the particular test case function so that we write the php artisan test –filter = name_of_test_cases_function
Example Of Test Cases
- Prior knowledge of graphql queries, mutation, schemas, and types is necessary with the basic understanding of the endpoint and how to use the graphql apis.
- After adding a function to check the graphql endpoint and errors, add a common function to return the result in the tests/TestCases.php file.
Example of the Mutation Test Cases
Here we have creates the one test cases for validation fails
The $query variable is used in this example to write the same query as we run Postman or any other graphQL editor, and we are sending the value. name=”” is empty so it brings the validation errors(the mutation must additionally contain the necessary validation rules)
And $expected variable is declared for the actual results after graphql api. It is a common structure for returning the result graphQL Api.
After return one $this->returnResult() function to pass the $query and $expected variables this returnResult() function is define in the TestCase.php file and inside this function assertJsonStructure() function is call
In this assertJsonStructure() function using the $this->postJson($url, $params); call the post request for graphql api and return the result
$response = $this->postJson($url, $params);
Here the $response variable takes the response of the api call and after we check if the response of this api is expected or not using the try catch block.
- Second Example Of The Query
The RefreshDatabase Trait is used in this query example to retrieve all manufacturer details test cases, ensuring that all entries are removed from the database upon execution of the test case.
Below image runs the particular test cases in terminal, and if we run all the test cases so write the php artisan test - Create Manufacture Successfully Example Test Cases
This example creates the manufacture where we pass the variables and $variables are used to insert the dummy data by using the faker.Here we are passing the $variables in returnResult() function, so it checks if the output is expected or not. Here you can find more examples of test cases.
Conclusion
I hope that everyone who wants to include graphql test cases in a laravel project will find this blog to be helpful. I’ve made an effort to cover all potential test case scenarios in this blog. Adding graphQL test cases in this way helps to keep the code efficient and takes advantage of code reuse.