Backend Engineering : Automation Testing for PHP using PHPUnit

Okay, testing is a part of software development (hmm i am not sure), most of companies will do this, some will think it is just a bullshit and a waste of time (include me LOL. I would rather deliver it to client directly).

I will overview the automation testing I had done before for my college assignment. In this case I used Kenjis’ unit testing framework, you can check it here. At this time I will overview about CI Unit maybe next time I will write about Laravel or NodeJS as I have already move from CI to Laravel (I feel like code in Ruby on Rails) and Nodejs.

You can see there all the required file to get started with Unit-testing. I am a bit lazy to write it again here. And you can use your dark theme terminal or text editor to do this test, you can also use Netbeans (I suppose you are a kids, because legend doesn’t use an IDE).

Before we get started you can check CIUnit functions you can use to make your code coverage 100%. Here.


This is really simple and basic function in unit testing, basically this function is to test your REST url as there is also function to test Ajax and so on. First off before you use this function, it is necessary to understand about basic REST method like post, get, delete, and put but in my case, my app only use post and get because we actually did not intent to create an API, we just created a single app with backend and frontend in one server no separetion, so there was no REST but this method is fit with my app.


request('GET', 'REST API URL HERE')

Method above will have a callback response, it can be array or plain html or number. So you can compare it with your expected output with assert method.

class Welcome_test extends TestCase
public function test_index()
$output = $this->request('GET', 'welcome/index');
'<title>Welcome to Medium</title>', $output

It is so easy, isn’t it?

What if my request contain body? Do like this kids

$this->request('GET', 'welcome/index', [YOUR ARRAY BODY])
['namefields'=> 'string name', 'passwordfields' => '********']

As simple as that.

What if my API needs SESSION? just use a PHP method to set session

$_SESSION['my_session'] = true
or if your session contains an id
$_SESSION['my_session'] = 12

Naming Standard

For naming file in unit testing you need to follow the standard (I think so, Im not sure). If you want to test controller, you need to name with the same name of your targeted controller plus underscore test (_test).


Welcome_test.php refers to Welcome.php controller

For method naming is up to you plus (_test).



Up to you.

As I am a bit lazy to write again, I think it is enough.

Click Here

I will post about CRUD with javascript maybe in the next post.