{"id":29945,"date":"2015-08-03T16:21:10","date_gmt":"2015-08-03T16:21:10","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=29945"},"modified":"2024-02-22T04:31:34","modified_gmt":"2024-02-22T04:31:34","slug":"writing-tests-for-phpunit","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/","title":{"rendered":"Writing Tests using PHPUnit"},"content":{"rendered":"\n<p>In previous post <a href=\"http:\/\/webkul.com\/blog\/phpunit-basic-tutorial\/\" target=\"_blank\" rel=\"noopener\">PHPUnit Basic Tutorial<\/a> we learned overview of&nbsp; PHPUnit.<\/p>\n\n\n\n<p>Now in this post we Write a test class and execute it and read output of test result.<br><strong>Step 1#<\/strong>: Create a test class named as SampleClass in file named SampleClass.php<br>this class contain a method for division of two numbers<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\nclass SampleClass\n{\n    public function divideNumber($a=null,$b=null){\n        $c=$a\/$b;\n        return $c;\n    }\n}\n\n?&gt;<\/pre>\n\n\n\n<p><strong>Step 2#:<\/strong>now we write a test named SampleClassTest in file named SampleClassTest.php<br>for above class<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\nrequire_once(&#039;SampleClass.php&#039;);\/* include file which you want to test*\/\nclass SampleClassTest extends PHPUnit_Framework_TestCase\n{\n    protected $dataarr1=array();\n   \/* PHPUnit supports sharing the setup code. \n      Before a test method is run, a template \n      method called setUp() is invoked. *\/\n    public function setUp(){\n      \/*setUp() is where you create the objects against which you will test. *\/\n       $this-&gt;dataarr1=array(5,6,7,8,9);\n    }\n   \/* Once the test method has finished running, \n      whether it succeeded or failed, another template \n      method called tearDown() is invoked. *\/\n    public function tearDown(){ \n    \/* tearDown() is where you clean up the objects against which you tested.  *\/\n       $this-&gt;dataarr1=&quot;&quot;;\n    }\n  \/* in below method we pass all element of $this-&gt;dataarr1 and counter $i value *\/\n    public function testMyObject(){\n        \/\/ test to ensure that the object from addNumber  is valid\n        $connObj=new SampleClass();\/* create object of class which you want to test *\/\n        for($i=1;$i&lt;5;$i++){\n            \/*here value of $i is from 1 to 5 so it run successfully*\/\n           $this-&gt;assertTrue($connObj-&gt;divideNumber($this-&gt;dataarr1&#091;$i],$i) !== false);\n\t}   \n   }\n  \n   public function testMyObjectnext(){\n        $connObj=new SampleClass();\n       for($i=0;$i&lt;5;$i++){\n           \/*here value of $i is from 0 to 5 so it return error Division by zero  *\/\n           $this-&gt;assertTrue($connObj-&gt;divideNumber($this-&gt;dataarr1&#091;$i],$i) !== false);\n       }  \n   }\n}\n?&gt;<\/pre>\n\n\n\n<p><strong>For run test class<\/strong><br>$ phpunit SampleClassTest.php<\/p>\n\n\n\n<p><strong>output<br>PHPUnit 4.7.6 by Sebastian Bergmann and contributors.<br>.E<br>Time: 76 ms, Memory: 11.50Mb<br>There was 1 error:<br>1) SampleClassTest::testMyObjectnext<br>Division by zero<br>\/home\/users\/webkul\/www\/html\/phpunit\/SampleClass.php:5<br>\/home\/users\/webkul\/www\/html\/phpunit\/SampleClassTest.php:25<br>FAILURES!<br>Tests: 2, Assertions: 4, Errors: 1.<\/strong><\/p>\n\n\n\n<p>test method &#8220;testMyObjec()&#8221; of class &#8220;SampleClassTest&#8221; executed successfully so in output we get period (.) that indicate successfully execution of test method.<\/p>\n\n\n\n<p>and for method &#8220;testMyObjectnext()&#8221; of class &#8220;SampleClassTest&#8221; execution we get &#8220;E&#8221; which indicate error occurs while running the test method. also we get detail about error in output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In previous post PHPUnit Basic Tutorial we learned overview of&nbsp; PHPUnit. Now in this post we Write a test class and execute it and read output of test result.Step 1#: Create a test class named as SampleClass in file named SampleClass.phpthis class contain a method for division of two numbers Step 2#:now we write a <a href=\"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/\">[&#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,1],"tags":[2057,1978],"class_list":["post-29945","post","type-post","status-publish","format-standard","hentry","category-php","category-uncategorized","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>Writing Tests using PHPUnit<\/title>\n<meta name=\"description\" content=\"Writing Tests using PHPUnit\" \/>\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\/writing-tests-for-phpunit\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Writing Tests using PHPUnit\" \/>\n<meta property=\"og:description\" content=\"Writing Tests using PHPUnit\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/\" \/>\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-03T16:21:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-22T04:31:34+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/\"},\"author\":{\"name\":\"Abhishek Singh\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/573e459f54796eb4195511990de4bfd0\"},\"headline\":\"Writing Tests using PHPUnit\",\"datePublished\":\"2015-08-03T16:21:10+00:00\",\"dateModified\":\"2024-02-22T04:31:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/\"},\"wordCount\":163,\"commentCount\":5,\"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\/writing-tests-for-phpunit\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/\",\"url\":\"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/\",\"name\":\"Writing Tests using PHPUnit\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2015-08-03T16:21:10+00:00\",\"dateModified\":\"2024-02-22T04:31:34+00:00\",\"description\":\"Writing Tests using PHPUnit\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Writing Tests using PHPUnit\"}]},{\"@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":"Writing Tests using PHPUnit","description":"Writing Tests using PHPUnit","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\/writing-tests-for-phpunit\/","og_locale":"en_US","og_type":"article","og_title":"Writing Tests using PHPUnit","og_description":"Writing Tests using PHPUnit","og_url":"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2015-08-03T16:21:10+00:00","article_modified_time":"2024-02-22T04:31:34+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":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/"},"author":{"name":"Abhishek Singh","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/573e459f54796eb4195511990de4bfd0"},"headline":"Writing Tests using PHPUnit","datePublished":"2015-08-03T16:21:10+00:00","dateModified":"2024-02-22T04:31:34+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/"},"wordCount":163,"commentCount":5,"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\/writing-tests-for-phpunit\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/","url":"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/","name":"Writing Tests using PHPUnit","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2015-08-03T16:21:10+00:00","dateModified":"2024-02-22T04:31:34+00:00","description":"Writing Tests using PHPUnit","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/writing-tests-for-phpunit\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Writing Tests using PHPUnit"}]},{"@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\/29945","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=29945"}],"version-history":[{"count":16,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/29945\/revisions"}],"predecessor-version":[{"id":423581,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/29945\/revisions\/423581"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=29945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=29945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=29945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}