PHPUnit Starting from scratch (2): Writing PHPUnit test ____php

Source: Internet
Author: User

The plan will never catch up with the change, and this month the program completes all PHPUnit blog content. Today a look at the calendar found all TMD February end, and I just wrote one. The feeling is so ...

Write a second article today, detailing how to write a test case.
Here will be involved in some of the automatic loading and so on, I will not add here, you can check the relevant PHP automatic loading data.
First of all, my project structure is as follows:

+phpunit//Project name
+SRC//Storage project code, code to be tested
+tests//test User Presence Directory
+vendor//Composer generated directories, third party packages stored
Composer.json//composer required configuration files
index.php//Entry files, mainly related to automatic loading of relevant code
Phpunit.xml//test Case configuration file

The general situation of each folder and file here, now in my src directory has a file, name:money.php

<?php

namespace SRC;

/**
 * @author: Helei
 * @createTime: 2016-01-07 17:45
 * @description: * *
class money
{
    Private $amount;

    Public function __construct ($amount)
    {
        $this->amount = $amount;
    }

    Public Function Getamount ()
    {return
        $this->amount;
    }

    Public function negate ()
    {return
        new money ( -1 * $this->amount);
    }

When you want to write something to a print statement or debug an expression, don't do it, write it as a test instead. –martin Fowler

This is what the programmers say, if you want to test whether the internal logic of a method is correct, write a test case, test the method, and give him a value to see if the result is the expected value.

OK, now specific to me this method, I want to test money this class, I give him initialization, input 1, after Money::multiple () method doubled, his value should be: 2, and then through Money::getamount () to get doubled after the value, and 2 to compare, Check by assertion is not 2.
So the above is the test case to verify the thing, concrete to the actual coding, under the tests folder to create a new moneytest.php file,

<?php

namespace Tests;

Use Src\money;

/**
 * @author: Helei
 * @createTime: 2016-01-07 17:47
 * @description: * *
class Moneytest extends \ Phpunit_framework_testcase
{public

    function testcanbenegated ()
    {
        //arrange
        $a = new (1);

        ACT
        $b = $a->multiple ();

        Assert
        $this->assertequals (2, $b->getamount ());
    }

Now that all the code is finished, it's time to use the test cases. Here it is necessary to give a supplementary information, through this official document, you can learn more about the use of the various commands. All the commands I used in the back don't explain it. Please move here to view.

At the cmd command line, enter the directory where the project is located, and manipulate the following figure.
The right situation:

.
Output when the test is successful.
F
Output when an assertion fails during the test method run.
E
Output when an error occurs during the test method run.
R
Output when a test is marked as risky.
S
When the test is skipped out of date output.
I
Output when the test is marked as incomplete or not implemented.
Through observation, we can find that the command line output is a · , representing the money class, output our expected results. Passed the test.

You can modify the corresponding values at the assertion, and then look at the results of the output.

Write, found that the basic knowledge of phpunit is so little, the back seems to write can not write like, because in the official documents are written clearly, I am embarrassed to copy the document again, and then write an original.

Yes, so if I don't write, that's not the end of the PHPUnit blog content. Haha, well, finish the 2016 first task, learn phpunit and related blogs. The next step is to start writing related to the design pattern and synchronize the GitHub.

Love to learn and write friends, do not purple with me, clearly can also write a five seven, on two and then stopped short. Of course, if there is no understanding of the place, you can leave a message to ask questions, I will add a few after a few are not.

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.