PHPUnit entry case and phpunit entry case

Source: Internet
Author: User

PHPUnit entry case and phpunit entry case

PHPUnit

This case is an entry-level unit test case for creating a triangle. It is completed in the netbeans environment. You can refer to the following materials to build a phpunit in this environment:

Http://www.cnblogs.com/x3d/p/phpunit-in-netbeans8.html

Https://phpunit.de/manual/current/zh_cn/installation.html

Https://github.com/sebastianbergmann/phpunit-skeleton-generator

 

Original code class:

 

<? Phpclass Triangle {/*** the first side of the three sides * @ var int */protected $ a;/*** the second side of the three sides * @ var int */protected $ B; /*** the third side of the three sides * @ var int */protected $ c;/*** type * @ var string */protected $ type; /***** equedges */const TYPE_EQUILATERAL = 'equilateral';/***** Isosceles */const TYPE_ISOSCELES = 'Isosceles '; /*** common */const TYPE_ORDINARY = 'ordinary '; public function _ construct ($ a = 0, $ B = 0, $ c = 0) {$ this-> ini TSide ($ a, $ B, $ c );} /*** initialize the three sides ** @ param int $ a * @ param int $ B * @ param int $ c */protected function initSide (& $ a = 0, & $ B = 0, & $ c = 0) {$ this-> a = intval ($ a); $ this-> B = intval ($ B ); $ this-> c = intval ($ c); return $ this;}/*** construct */public function create ($ a, $ B, $ c) {return $ this-> initSide ($ a, $ B, $ c)-> verifySideIsValid ();}/*** get type */public function getType () {return $ this-> verifyType ()-> Type;}/*** verify that the three sides are valid * @ return boolean */protected function verifySideIsValid () {if (intval ($ this->) <= 0 | intval ($ this-> B) <= 0 | intval ($ this-> c) <= 0) {return false ;} if ($ this-> a + $ this-> B <= $ this-> c) {return false ;} if ($ this-> a + $ this-> c <= $ this-> B) {return false ;} if ($ this-> B + $ this-> c <= $ this-> a) {return false ;} if ($ this-> a-$ this-> B >=$ this-> c) {return false ;} If ($ this-> a-$ this-> c >=$ this-> B) {return false ;} if ($ this-> B-$ this-> c >=$ this-> a) {return false;} return true ;} /*** authentication type */protected function verifyType () {if ($ this-> isEquilateral () {$ this-> type = self: TYPE_EQUILATERAL; return $ this;} if ($ this-> isIsosceles () {$ this-> type = self: TYPE_ISOSCELES; return $ this;} $ this-> type = self:: TYPE_ORDINARY; return $ this;}/*** whether Is an equilateral triangle */protected function isEquilateral () {return ($ this-> a = $ this-> B) & ($ this-> B ==$ this-> c ))? True: false;}/*** whether it is an isosceles triangle */protected function isIsosceles () {return ($ this-> a ==$ this-> B) | ($ this-> B ==$ this-> c) | ($ this-> a ==$ this-> c ))? True: false ;}}

 

 

Generated test files:

<? Php/*** Generated by PHPUnit_SkeletonGenerator on 19:49:12. */class TriangleTest extends PHPUnit_Framework_TestCase {/*** @ var Triangle */protected $ object;/*** Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */protected function setUp () {$ this-> object = new Triangle;}/*** Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */protected function tearDown () {}/ *** @ dataProvider addDataProvider * @ covers Triangle: create * @ todo Implement testCreate (). */public function testCreate ($ a, $ B, $ c) {// Remove the following lines when you implement this test. /** $ this-> markTestIncomplete ('this test has not been implemented yet. '); ** // * Implementation Code */$ this-> assertTrue ($ this-> object-> create ($ a, $ B, $ c ));} /*** @ covers Triangle: getType * @ todo Implement testGetType (). */public function testGetType () {// Remove the following lines when you implement this test. $ this-> markTestIncomplete ('this test has not been implemented yet. ');}/*** test case * @ return array */public function addDataProvider () {return [[3, 4, 5], // yes [2, 2, 2], // yes [8, 10, 8], // yes [2, 3, 4], // yes [1, 2, 3], // no [5, 6, 7], // yes [8, 8, 15], // yes [0, 0, 0], // no [-10, 2, 5], // no [0, 2, 1], // no];}

Note that the test file class generated after "Create/update test" is executed is somewhat different from the above. The test cases here are manually added, for more information, see the instructions in the manual!

 

Attached execution result:

"/Usr/bin/php" "/usr/local/bin/phpunit" "-- colors" "-- log-junit" "/tmp/nb-phpunit-log.xml" "-- bootstrap ""/ var/www/html/phpunit/test/bootstrap. php ""/usr/local/netbeans-8.1/php/phpunit/NetBeansSuite. php "" -- "" -- run =/var/www/html/phpunit/test/core/triangleTest. php "PHPUnit 5.2.10 by Sebastian Bergmann and contributors ..... F .. FFFI 11/11 (100%) Time: 105 MS, Memory: 10.50 MbThere were 4 failures: 1) Tr IangleTest: testCreate with data set #4 (1, 2, 3) Failed asserting that false is true. /var/www/html/phpunit/test/core/triangleTest. php: 472) TriangleTest: testCreate with data set #7 (0, 0, 0) Failed asserting that false is true. /var/www/html/phpunit/test/core/triangleTest. php: 473) TriangleTest: testCreate with data set #8 (-10, 2, 5) Failed asserting that false is true. /var/www/html/phpunit/test/core /TriangleTest. php: 474) TriangleTest: testCreate with data set #9 (0, 2, 1) Failed asserting that false is true. /var/www/html/phpunit/test/core/triangleTest. php: 47 FAILURES! Tests: 11, Assertions: 10, Failures: 4, Incomplete: 1. Complete.

  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.