When we develop any application, testing is a major part of the development. After development, functionality testing is crucial as it can easily help identify any flaws in the code.
After you are sure your code works well as per requirements, you can integrate it with the system and perform integration tests to make sure it works with the other units too.
Developers are often faced with a challenge where the methods may be easy, but the application doesn’t work because of a small bug.
Now imagine, that you have a system with hundreds of methods, and suddenly a bug appears, and you need to track it and resolve it.
This might take a very long time. It is in situations like this that Code testing and Unit testing in a particular section can help prevent this type of problem.
So today we are going to discuss it in detail with the PHP development framework Laravel. The Laravel development framework is also a PHP Unit testing framework itself.
PHP Unit is one of the most popular and widely useful testing frameworks. PHP Unit allows us to create both kinds of testing-Unit testing and Functional Testing.
We will learn the introduction of Unit and functional testing. Now, we’ll learn how to develop unit testing and functional testing in Laravel.
If you already know about PHP Unit testing, we can divide testing into two different sections- Unit Testing and Functional Testing.
public function getPostTitle($value)
{
return ucfirst($value);
}
Before we go ahead and create an actual test, we need to set up a couple of things that will be used in software testing. We will create Posts modal and related migration.
Go ahead and run the below command in the terminal.
$php artisan make:model Posts –migration
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
//
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create(‘posts’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘post_name’);
$table->timestamps();
});
}
/**
* Reverse the migrations.
* @return void
*/
public function down()
{
Schema::dropIfExists(‘posts’);
}
}
$php artisan migrate
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
/*
get post title name convert, first character in Uppercase
*/public function getPostTitle($value)
{
return ucfirst($value);
}
}
<?php
// app/Http/Controllers/PostController .php
namespace App\Http\Controllers;
use App\Posts;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class PostController extends Controller
{
public function index(Request $request)
{
// get the post-id from request params
$post_id = $request->get(“id”, 1);
// load the requested post
$post = Posts::find($post_id);
// check the name property
return ($post)?$post->name:”No Data”;
}
}
Route::get(‘posts/index’, ‘PostController@index’);
In the precedent section, we did the initial setup that’s going to be useful to us in this and upcoming sections. In this section, we are going to create an example that demonstrates the concepts of unit testing in Laravel.
$php artisan make:test PostsTest –unit
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class PostsTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/public function testExample()
{
$this->assertTrue(true);
}
}
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use DB;
use App\Posts;
class PostsTest extends TestCase
{
public function testAccessorTest()
{
// load post manually first
$db_post = DB::select(‘select * from posts where id = 1’);
$db_post_title = ucfirst($db_post[0]->name);
// load post using Eloquent
$model_post = Posts::find(1);
$model_post_title = $model_post->name;
$this->assertEquals($db_post_title, $model_post_title);
}
}
$php artisan make:test PostsTest
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class PostsTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/public function testExample()
{
$this->assertTrue(true);
}
}
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use DB;
class PostsTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
// load post manually first
$db_post = DB::select(‘select * from posts where id = 1’);
$db_post_title = ucfirst($db_post[0]->name);
$response = $this->get(‘/posts/index?id=1’);
$response->assertStatus(200);
$response->assertSeeText($db_post_title);
}
}
$phpunit
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.
.. 2 / 2 (100%)
Time: 140 ms, Memory: 16.00MB
OK (2 tests, 3 assertions)
i.e. I have changed the in a file at ‘tests/Unit/PostsTest.php‘. Just replace the code ‘$db_post = DB::select(‘select * from posts where id = 2′);’ in place of ‘$db_post = DB::select(‘select * from posts where id = 1′);’ and run command ‘phpunit‘. You will get the test results below.
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 115 ms, Memory: 16.00MB
There was 1 failure:
1) Tests\Unit\PostsTest::testAccessorTest
Failed asserting that two strings are equal.
— Expected
+++ Actual
@@ @@
-‘Post 2′
+’Post 1’
/var/www/html/your-laravel-site-folder/tests/Unit/PostsTest.php:24
/usr/share/php/PHPUnit/TextUI/Command.php:206
/usr/share/php/PHPUnit/TextUI/Command.php:162
FAILURES!
Tests: 2, Assertions: 3, Failures: 1.
So, this is all about unit testing, here we have to share How Laravel developers test their own code and how it can help you to better manage and control projects. As part of IT Staff Augmentation, developers leverage the technique of Unit Testing to ensure that their code works as intended by validating individual components and functionalities in isolation. If you have any confusion regarding this please contact our expert at OneClick.
Diving deep into SwiftUI This blog post drops us into…
Corporate efficiency and customization are vital in today's fast-paced world,…
Flutter Codemagic CI/CD makes your Flutter app build, test, and…
This website uses cookies.