Automated testing, automatic packaging, and automatic deployment of php projects based on Jenkins

Source: Internet
Author: User
: This article describes how to implement automated testing, automatic packaging, and automatic deployment of php projects based on Jenkins. For more information about PHP tutorials, see. Based on Jenkins's continuous integration environment, we will continue to introduce Jenkins's automated testing and deployment in combination with the php project. I will not talk about it any more. I will work on it directly.

The server used by housebird is Ubuntu.

To implement php automated testing in jenkins, first install the php testing Framework on the Jenkins server. There are many php testing frameworks. Here we select PHPUnit Framework.

PHPUnit installation is simple:

sudo apt-get install phpunit

If the following error occurs:

PHP Warning: require_once(PHP/CodeCoverage/Filter.php): failed to open stream: No such file or directory in /usr/bin/phpunit on line 39PHP Fatal error: require_once(): Failed opening required 'PHP/CodeCoverage/Filter.php' (include_path='.:/usr/share/php:/usr/share/pear') in /usr/bin/phpunit on line 39

You can install it using the following method:

sudo pear channel-discover pear.phpunit.desudo pear channel-discover pear.symfony-project.comsudo pear channel-discover components.ez.nosudo pear channel-discover pear.symfony.comsudo pear update-channelssudo pear upgrade-allsudo pear install pear.symfony.com/Yamlsudo pear install --alldeps phpunit/PHPUnitsudo pear install --force --alldeps phpunit/PHPUnit

After installation, run phpunit -- version to return the version information. Indicates that the installation is successful.

root@dop-kvm-2:# phpunit --versionPHPUnit 3.7.28 by Sebastian Bergmann.

Next we will give Jenkins some plug-ins:

Subversion/Git: used to integrate the project version control software and select as needed (installed and used in the previous blog)

Phing/Ant: use Phing or Apache Ant for automated PHP project construction

CheckStyle: a tool for checking the code style using PHP CodeSniffer. This API is used to check whether PHP code violates a set of pre-configured PEAR encoding standards. It has built-in ZEND and PEAR encoding style rules.

Clover PHP: a unit test tool using phpunit. it can be used by xdebug extension to generate code coverage reports and can be integrated with phing for automatic testing, it can also be integrated with Selenium to complete large-scale automated integration testing.

DRY: use PHPCPD (php copy paste detector) to discover duplicate code in the project

HTML Publisher: used to publish the phpunit code coverage report

JDepend: use PHP Depend to analyze the static code in php and check the code scale and complexity in the project.

Plot: A tool that uses phploc to count the size of a php project. it can count the number of lines of php project code.

PMD: php mess dector is used to analyze the pdepend-based results. once the project exceeds the specific metrics in pdepend, a warning will be issued.

Violations: displays the pwd static code analysis results according to the code Defect severity

XUnit: Use the JUnit format to output the phpunit log file.


Note that these plug-ins are some provided by jenkins for the php project, but they are not required, therefore, the home bird only explains how to perform automated testing, package, and publish the most noteworthy tasks.

First, the directory structure of the project is given:

root@dop-kvm-2:/home/jenkins/api# tree.├── aa.php├── build.xml├── create.php└── test    ├── DemoTest.php    └── FunctionTest.php1 directory, 5 files

Note:

Aa. php and create. php are program files of the project.

DemoTest. php and FunxtionTest. php under the test directory are the test files of the project.

Build. xml is the call File for jenkins continuous integration testing, packaging, and deployment.


First, the build. xml file required by the project is provided:

 
         
          
          
          
          
          
          
          
          
          
              
               
               
           
          
                  
                   
                   
                   
           
          
                  
                                                                                           
           
          
                  
                                                                                                                                           
           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                  
                                                                                           
           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                  
                                                                                                                   
           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                  
                                                                                           
           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                  
                                                                                                                                           
           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                  
                                                                                           
           
          
                  
           
          
                  
           
          
              
               
                
                     
                  
                 
                
           
          
                  
                                                                                                                                                                   
           
          
              
               
               
               
               
           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
              
               
               
                   
                
           
  
 

After reading build. xml, you can understand the following content:


Project name, version, and package name:

         
          
          
          
          
          
          
          
          
  
 

Files and folders included during packaging: exclude can also be used to exclude files and folders:

            
             
             
         
 

Test file address:

                 
          
         
             
              
               
                    
                 
                
               
          
 

After learning about this, we started to create an autoTestTarAndPublish project in jenkins, and chose to build a free-style software project:

And specify the code library:

Now let's check the content of the test/DemoTest. php file:

 assertTrue(true);    }  public function testFail() {      $this->assertFalse(false);    }}?>

Let's change testFail () to the following:

 assertTrue(true);    }  public function testFail() {     $this->assertTrue(false);    }}?>

$ This-> assertTrue (false );

This is an incorrect conclusion:

Submit the file and build it again:

We can see that this build failed and the output result is as follows:

After the test case is modified to the correct one, the build is executed and the release is correct.

 assertTrue(true);    }  public function testFail() {      $this->assertFalse(false);    }}?>

The above describes how to implement automated testing, automatic packaging, and automatic deployment of php projects based on Jenkins. For more information, see PHP Chinese website (www.php1.cn )!

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.