What exactly is a PHP unit test?

Source: Internet
Author: User
Tags pear

Preface: really write PHP code also has 3 years of time, barely a php programmer, but the bottom of my heart has not been emboldened. All say Test-driven development, but what is unit testing in my program development? This basic Programmer's literacy is

It is not clear, the lesson from the bitter, the decision of these basic knowledge and skills to have some understanding and mastery. otherwise, has been using other people out of the framework, write some simple business logic code, the Baby's heart is actually a panic:).

This article is good: https://www.sitepoint.com/tutorial-introduction-to-unit-testing-in-php-with-phpunit/

On the stack-overflow, someone answered:

url:http://stackoverflow.com/questions/282150/how-do-i-write-unit-tests-in-php

Question: How does I write unit tests in PHP? [closed]

I ' ve read everywhere about how great they is, but is some reason I can ' t seem to figure out how exactly I ' m supposed to Test Something. Could Someone perhaps post a piece of example code and how they would test it? If it ' s not too much trouble:)

Answer:

There is a 3rd "framework", which are by far easier to learn-even easier than simple Test, it ' s called Phpt.

A Primer can found here:http://qa.php.net/write-test.php

Edit: Just saw your request for sample Code.

Let's assume you has the following function in a file called lib.php:

<?phpfunction foo($bar){ return $bar;}?>

Really simple and straight forward, the parameter your pass in, is Returned. So let's look at a test for this function, we'll call the test file foo.phpt:

--TEST--Foo() function -A Basic test to seeIfIt works. :)--file--<? Phpinclude  ' lib.php ' ;//might need to adjust path if not in the same Dir $bar =  ' Hello World ' ;foo ( $bar ?>--expect --string (11    "Hello world"        

In a nutshell, we provide the parameter with value and we the response of the function call to $bar "Hello World" var_dump() foo() .

To run the This test, use:pear run-test path/to/foo.phpt

This requires a working install the PEAR on your system, and which is pretty common in the most Circumstances. If you need to install it, I recommend to install the latest version Available. In the case of you need help to set it up, feel free to ask (but provide OS, etc).

another answer: There is frameworks you can use the for unit testing. SimpleTest and PHPUnit, which I prefer. Read the tutorials on what to write and run tests on the homepage of PHPUnit. It's quite easy and well Described.

Here is the official entry document for Phpunit: https://phpunit.de/getting-started.html

In addition, Simpletest's official document: http://www.simpletest.org/

well, Let's start learning phpunit, starting today, slowly understand it, come on!

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------

Add: here is another article:

http://code.tutsplus.com/articles/the-beginners-guide-to-unit-testing-what-is-unit-testing--wp-25728

Depending on your background, if or may is not having heard of unit testing, Test-driven development, Behavior-driven deve lopment, or some other type of testing methodology. Often times, These methodologies is applied in the context of larger software systems or applications and less in the con Text of wordpress-based projects (though it is getting better!)

frankly, The development community is a bit divided on automated software testing–you has some people who think you sho Uld has tests for 100% in your code, some believe that 80% was sufficient, some 50%, and some are content with 20% or So. Whatever The case could be, this article was not on arguing a case for the level of tests you should has in your project Nor is it taking a position on the general software Testing.

Instead, we ' re going to take a look at what's required to get up and running with unit testing your WordPress development Projects. We ' re going to being approaching this series from the perspective of a absolute beginner so that we can understand the BENEF Its of the unit testing and how to configure we environment to the support Unit testing libraries so, we can begin to does this We work for the Future. Finally, all of this would be-done by building and testing a simple, testable plugin from the ground up.

What is Unit testing?

Before we get started setting up our environment and writing any code, let's define exactly what unit testing are, why it ' s Worth doing, and how to get started in incorporating it in our Projects.

At a high-level, unit testing refers to the practice of testing certain functions and areas–or units–of our Code. This gives us, the ability to verify, that we functions work as Expected. That's to say this for any function and given a set of inputs, we can determine if the function is returning the proper V Alues and would gracefully handle failures during the course of execution should invalid input be Provided.

ultimately, This helps us to identify failures in we algorithms and/or logic to help improve the quality of the code that Composes a certain Function. As you begin to write + and more tests, you end up creating a suite of tests so can run at any time during Develo Pment to continually verify the quality of your Work.

A second advantage to approaching development from a unit testing perspective are that you'll likely be writing code that I s easy to Test. Since unit testing requires that your code is easily testable, it means that your code must support this particular type O F Evaluation. As such, you ' re more likely to has a higher number of smaller, more focused functions that provide a single operation on A set of data rather than large functions performing a number of different operations.

A third advantage for writing solid unit tests and well-tested code is so you can prevent the future changes from breaking F Unctionality. Since you ' re testing your code as you introduce your functionality, you ' re going to begin developing a suite of test cases That can is run each time your Logic. When a failure happens is know, you are something to Address.

Of course, this comes at the expense of investing time to write a suite of tests early in development, but as the project Grows you can simply run the tests so you've developed to ensure that existing functionality isn ' t broken when new funct Ionality is Introduced.

.........

What exactly is a PHP unit test?

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.