How to Write Unit Test Case on Slim 3 to Test API | Devstringx

Back to Blog

How to Write Unit Test Case on Slim 3 to Test API | Devstringx


  • PHP 7.3
  • Visual Studio Code

1) Install slim via composer

//run this command on terminal composer require slim/slim:”3.*”

install slim via composer

2) Install PHPUnit

//run this command on terminal

composer require-dev PHPUnit/PHPUnit ^7


Now, PHPUnit successfully installs you can see it in the demo/composer.json file and in the vendor folder.

3) Create a Demo/phpunit.xml Configuration File

Now we have to create a demo/phpunit.xml configuration file which use to configure the PHPUnit


4) Create a Demo/Tests Folder

Now we have to create our demo/tests folder in our project.


5) Create the Demo/src Folder Inside the SRC Folder

Now create the demo/src folder inside the src folder you have to create a file name routes.php in which you will write your API.


In the routes.php file, I have created an API for the addition of two numbers.

6) Set Up the Autoload to Autoload

Now we have to set up the autoload to autoload everything from the tests directory in the composer.json file.

composer dump-autoload

Write “autoload-dev”: { “psr-4”: { “Tests\\”: “tests/” } } this in your composer.json file And run command-

composer dump-autoload –o

7) Create a Functional Folder

Now coming to the demo/tests folder inside the tests folder we have to create a Functional folder inside that I have created a BaseTestCase class which is extended from TestCase class.

App function

Inside BaseTestCase class, we have created the run app function passing the $requesMethod (POST, GET), $requestUri(API name), and $requestData(array of data), inside this function I have created mock environment objects with custom information. Mock Environment objects are only useful when writing unit tests.after that I have setting up the $request = Request::createFromEnvironment($environment) object. Now adding the requested data if exist. now setting up the response object. create an object $app = new App();

After that process, the application $response = $app->process($request, $response) And returns the response.

8) Create the File for Testing

Now create the file for testing out API demo/tests/Functional/routesApiTest


In this we have created a class routesApiTest extended from BaseTestCase class If you are writing a test case then you always have to write test as a prefix of the function like testaddNum or you can write as I wrote in my case /** * @test */.now create a function addendum inside this function I have called runApp method in which I am passing the 3 parameters first-method which is post,second-API name,third-data array. Now I am using assertEquals methods in the first parameter is what I expect and the second parameter $response->getBody() is what it returns.

9) Run the Command In Terminal

Now run the command in terminal

./vendor/bin/PHPUnit tests

  • What is a slim test?

SLIM Test Method. Fit’s substitute is Slim (Simple List Invocation Method). Slim maintains all of that activity active in FitNesse rather than doing all of the HTML processing, comparisons, and colorization in the System Under Test (SUT).

  • What is slim3?

slim framework 3 is a PHP micro-framework that makes it simple yet effective to create APIs and online apps. slim 3 routes is fundamentally a dispatcher that receives an HTTP request, calls the necessary callback procedure, and then sends back an HTTP response. I’m done now.

  • What is the purpose of unit Testing?

Unit testing’s primary goal is to separate written code for testing to see if it functions as intended. Unit testing is a crucial stage in the development process because, when done properly, it can aid in finding early code issues that could be more challenging to identify in subsequent testing phases.

  • How do I download slim?

slim test Installation. Installing Composer along with Slim is advised. To run the bash command, go to the root directory of your project and type the command given below. With this command, the Slim Framework and its external dependencies are downloaded into the vendor/ directory of your project.

  • Is Slim a good framework?

SOLID concepts, design patterns, security principles, and dependency injection are all compatible with the slim framework. Compared to full-stack frameworks like Laravel or Symfony, slim testing is simpler to learn.

If you are interested in even more software testing-related articles and information from us here at Devstringx, then we have a lot to choose from for you.

Share this post

Back to Blog