{"id":29924,"date":"2015-08-03T12:22:22","date_gmt":"2015-08-03T12:22:22","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=29924"},"modified":"2015-08-03T14:43:44","modified_gmt":"2015-08-03T14:43:44","slug":"phpunit-basic-tutorial","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/","title":{"rendered":"PHPUnit Basic Tutorial"},"content":{"rendered":"<p class=\"hidden\">PHPUnit Basic Tutorial<\/p>\n<p><strong>PHPUnit:<\/strong> PHPUnit provides a simple framework for creating a test suite to automate testing of functions and classes.<\/p>\n<p>One of the rules of XP is to test small software components as often and early as possible, this way you will not have to fix bugs and errors in the API while setting up and testing larger applications which depend on the class.While unit testing is one of the fundimental rules in XP, you don&#8217;t have to switch to XP to benefit from PHPUnit. PHPUnit stands alone as a good tool for testing classes or a set of functions and will ease your development cycle and help you to avoid endless debug sessions.<\/p>\n<p><strong>Current Work routine (without PHPUnit):\u00a0 <\/strong>Normally, you would write a class, do some unsystematic tests using <em>echo()<\/em> or<em> var_dump()<\/em>. After this, you use the class in your application and hope everything is ok.<\/p>\n<p><strong>Test by using PHPUnit:<\/strong> To benefit from PHPUnit you should rethink the flow. The best way is to do this:<\/p>\n<p>1. design your class\/API<br \/>\n2. create a test suite<br \/>\n3. implement the class\/API<br \/>\n4. run the test suite<br \/>\n5. fix failures or errors and go to #4 again<\/p>\n<p>=&gt; It may seem that this will require a lot of time, but this impression is wrong. Creating the test suite using PHPUnit needs only a few minutes and running the test suite only seconds.<\/p>\n<p><strong>How To Install<\/strong><\/p>\n<p>\u279c wget https:\/\/phar.phpunit.de\/phpunit.phar<br \/>\n\u279c chmod +x phpunit.phar<br \/>\n\u279c mv phpunit.phar \/usr\/local\/bin\/phpunit<\/p>\n<p><strong>For check version<\/strong><br \/>\n\u279c phpunit \u00ad\u00adversion<\/p>\n<p><strong>How To Run Test Class<\/strong><\/p>\n<p>phpunit \/path\/RemoteConnectTest.php<\/p>\n<p>\/* RemoteConnectTest.php is test class file which i made for test *\/<\/p>\n<p><strong>Output<\/strong><\/p>\n<p>\u279c PHPUnit 3.4 by Sebastian Bergmann<br \/>\n\u279c .<br \/>\n\u279c Time: 1 second<br \/>\n\u279c Tests: 1, Assertions: 1, Failures 0<\/p>\n<p><strong>output Definition<\/strong><\/p>\n<p>For each test that\u2019s run, you\u2019ll either see a<\/p>\n<p>period (.) if it\u2019s successful (as above),<br \/>\nan \u201cF\u201d if there\u2019s a failure,<br \/>\nan \u201cI\u201d if the test is marked as incomplete, or<br \/>\nan \u201cS\u201d if it\u2019s been marked as skipped.<br \/>\nan \u201cE\u201d if it&#8217;s been marked as\u00a0 error occurs while running the test method.<br \/>\nan \u201cR\u201d if it&#8217;s been marked\u00a0 as risky.<\/p>\n<p><strong>Key points for remember<\/strong><\/p>\n<p>For test class<\/p>\n<p>=&gt;extends PHPUnit_Framework_TestCase class<br \/>\n=&gt;make setUp() for assign values in variable before test function run \/\/optional<br \/>\n=&gt;make tearDown() for unset values of variable after test function run done \/\/optional<br \/>\n=&gt;make test function with name that start with test<br \/>\n=&gt;Each function for test pass in $this\u00ad&gt;assertTrue();<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHPUnit Basic Tutorial PHPUnit: PHPUnit provides a simple framework for creating a test suite to automate testing of functions and classes. One of the rules of XP is to test small software components as often and early as possible, this way you will not have to fix bugs and errors in the API while setting <a href=\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[2057,1978],"class_list":["post-29924","post","type-post","status-publish","format-standard","hentry","category-php","tag-php","tag-phpunit"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHPUnit Basic Tutorial<\/title>\n<meta name=\"description\" content=\"PHPUnit Basic Tutorial\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHPUnit Basic Tutorial\" \/>\n<meta property=\"og:description\" content=\"PHPUnit Basic Tutorial\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2015-08-03T12:22:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-08-03T14:43:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Abhishek Singh\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Abhishek Singh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/\"},\"author\":{\"name\":\"Abhishek Singh\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/573e459f54796eb4195511990de4bfd0\"},\"headline\":\"PHPUnit Basic Tutorial\",\"datePublished\":\"2015-08-03T12:22:22+00:00\",\"dateModified\":\"2015-08-03T14:43:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/\"},\"wordCount\":416,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"PHP\",\"PHPUnit\"],\"articleSection\":[\"php\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/\",\"url\":\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/\",\"name\":\"PHPUnit Basic Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2015-08-03T12:22:22+00:00\",\"dateModified\":\"2015-08-03T14:43:44+00:00\",\"description\":\"PHPUnit Basic Tutorial\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHPUnit Basic Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/573e459f54796eb4195511990de4bfd0\",\"name\":\"Abhishek Singh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d4ac7e0e671bf743359d7e3f140c262d1b16d71106f0a1aeaecca327a2805ae4?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d4ac7e0e671bf743359d7e3f140c262d1b16d71106f0a1aeaecca327a2805ae4?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Abhishek Singh\"},\"description\":\"Adobe Commerce certified Magento developer with over 12 years of experience at Webkul. Passionate about scalable Magento 2-based webshops, AI, and multi-channel integrations, Abhishek consistently delivers innovative and efficient e-commerce solutions that propel businesses forward.\",\"sameAs\":[\"http:\/\/webkul.com\"],\"url\":\"https:\/\/webkul.com\/blog\/author\/abhishek\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHPUnit Basic Tutorial","description":"PHPUnit Basic Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"PHPUnit Basic Tutorial","og_description":"PHPUnit Basic Tutorial","og_url":"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2015-08-03T12:22:22+00:00","article_modified_time":"2015-08-03T14:43:44+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Abhishek Singh","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Abhishek Singh","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/"},"author":{"name":"Abhishek Singh","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/573e459f54796eb4195511990de4bfd0"},"headline":"PHPUnit Basic Tutorial","datePublished":"2015-08-03T12:22:22+00:00","dateModified":"2015-08-03T14:43:44+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/"},"wordCount":416,"commentCount":1,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["PHP","PHPUnit"],"articleSection":["php"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/","url":"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/","name":"PHPUnit Basic Tutorial","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2015-08-03T12:22:22+00:00","dateModified":"2015-08-03T14:43:44+00:00","description":"PHPUnit Basic Tutorial","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PHPUnit Basic Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/573e459f54796eb4195511990de4bfd0","name":"Abhishek Singh","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d4ac7e0e671bf743359d7e3f140c262d1b16d71106f0a1aeaecca327a2805ae4?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d4ac7e0e671bf743359d7e3f140c262d1b16d71106f0a1aeaecca327a2805ae4?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Abhishek Singh"},"description":"Adobe Commerce certified Magento developer with over 12 years of experience at Webkul. Passionate about scalable Magento 2-based webshops, AI, and multi-channel integrations, Abhishek consistently delivers innovative and efficient e-commerce solutions that propel businesses forward.","sameAs":["http:\/\/webkul.com"],"url":"https:\/\/webkul.com\/blog\/author\/abhishek\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/29924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=29924"}],"version-history":[{"count":11,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/29924\/revisions"}],"predecessor-version":[{"id":29989,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/29924\/revisions\/29989"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=29924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=29924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=29924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}