{"id":13524,"date":"2023-11-21T10:00:00","date_gmt":"2023-11-21T01:00:00","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=13524"},"modified":"2023-11-21T10:00:35","modified_gmt":"2023-11-21T01:00:35","slug":"getting-started-with-unit-testing-in-laravel","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/13524","title":{"rendered":"Getting Started with Unit Testing in Laravel"},"content":{"rendered":"\n<p>Unit testing is a crucial aspect of any robust software development process. In Laravel, a popular PHP framework, writing unit tests is not only easy but also highly encouraged. In this tutorial, we will go through the basics of writing unit tests in Laravel,&nbsp;ensuring&nbsp;the reliability and stability of the individual code units.<\/p>\n\n\n\n<p><strong>Prerequisites<\/strong><\/p>\n\n\n\n<p>Before diving&nbsp;into writing unit tests, make sure the following prerequisites&nbsp;be&nbsp;installed:<\/p>\n\n\n\n<p>1. Composer<\/p>\n\n\n\n<p>2. Laravel Installed<\/p>\n\n\n\n<p>3. PHPUnit<\/p>\n\n\n\n<p><strong>Setting Up a Laravel Project<\/strong><\/p>\n\n\n\n<p>Use the following commands to set up a new project:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer create-project --prefer-dist laravel\/laravel my-laravel-app\n\ncd my-laravel-app<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Creating a Unit Test<\/strong><\/p>\n\n\n\n<p>Laravel provides a convenient Artisan command to generate a test class. Let&#8217;s create a simple unit test for a hypothetical `Calculator` class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:test CalculatorTest --unit<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>This will generate a test file located at `tests\/Unit\/CalculatorTest.php`. Open the file and you&#8217;ll see a basic test structure.<\/p>\n\n\n\n<p><strong>Writing a Unit Test<\/strong><\/p>\n\n\n\n<p>Now, let&#8217;s write a unit test for a basic addition method in our `Calculator` class. Open `CalculatorTest.php` and modify it as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace Tests\\Unit;\n\nuse PHPUnit\\Framework\\TestCase;\nuse App\\Calculator;\n\nclass CalculatorTest extends TestCase\n{\n    public function testAddition()\n    {\n        $calculator = new Calculator();\n        $result = $calculator-&gt;add(2, 3);\n\n        $this-&gt;assertEquals(5, $result);\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>In this example, assume we have a `Calculator` class in the `app` directory with an `add` method&nbsp;like that.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace App;\n\nclass Calculator\n{\n    public function add($a, $b)\n    {\n        return $a + $b;\n    }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Running Unit Tests<\/strong><\/p>\n\n\n\n<p>To run the unit tests, use the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan test<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>This will execute all the tests in the `tests` directory.<\/p>\n\n\n\n<p><strong>Assertions<\/strong><\/p>\n\n\n\n<p>Laravel provides a variety of assertions that can be used&nbsp;in the tests. In this example, we used `assertEquals` to verify that the addition method returns the expected result. Explore other assertions&nbsp;and details&nbsp;in the official&nbsp;documentation <a href=\"https:\/\/laravel.com\/docs\/10.x\/testing\">https:\/\/laravel.com\/docs\/10.x\/testing<\/a>.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>We&#8217;ve just written a first unit test in Laravel. As the application grows, writing tests for individual code units will become an integral part of the development process, ensuring that the code remains maintainable and reliable. Hope you enjoy that.<\/p>\n\n\n\n<p>Asahi<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13524\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/13524\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Getting Started with Unit Testing in Laravel\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13524\" data-text=\"Getting Started with Unit Testing in Laravel\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13524\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/13524\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Unit testing is a crucial aspect of any robust software development process. In Laravel, a popular PHP framewo [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/13524"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=13524"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/13524\/revisions"}],"predecessor-version":[{"id":13529,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/13524\/revisions\/13529"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=13524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=13524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=13524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}