Use phphunit test unit

What is unit testing? In short, the term refers to the process of dividing software into the smallest testable components (called units). The component can be independently tested to determine whether it meets the requirements and works as expected. Unit testing has many advantages. Most importantly, you (and stakeholders) can determine the maturity of the software. As long as you run the test cases, you can easily determine whether the code is working as expected. For developers, test cases can be very important, so developers can: The
Before submitting, verify whether the code works as expected and whether the software has problems due to recent changes when handling exceptions. It is not without shortcomings to create test cases for unimplemented units and realize the automation of the test process of the work plan (called test led development). Test cases in particular require time to write existing code. Undoubtedly, this will be offset by the time saved in the future, but it is reasonable to say that the development is suspended slightly in the initial stage. In addition, writing test cases for existing software often becomes an impossible and tedious task. The
There are also human errors. There may be errors in the test case, which may cause you to mistakenly believe that the unit works. Therefore, it is not recommended to rely entirely on unit tests. If someone checks the code and performs a manual test (if applicable), they can save the skin! Now that the handout section of the article is over, let’s start by checking whether all the content has been set. You can phpunit here to install phpunit manually. Alternatively, if you use composer, you can add phpunit as a dependency. Must be added with \
It is also important to the correct version according to the version of PHP to be developed. The phphphunit site must show which phphunit versions support which PHP versions. Now you need to the phar file. Linux, UNIX, and OSX Nix systems can easily create it. Just open the terminal in the directory where the phar file is ed, and then run the following command: Chmod +x phpunit Phar
Sudo MV phpunit The command on phar\/usr\/local\/bin\/phpunit makes the phar file executable and moves it to the appropriate directory. To verify that the installation is correct, run the following command in a different directory: The
Phpunit– if using the version window windows, move the ed files to a directory in path, or create a new directory and add it to path. In my case, the c:\bin directory contains all the custom executables. Windows cannot have the same files running on a Nix system, so you must also create a batch file. Phpunit Phpunit is located in the same directory near phar. Create a bat file. Open the batch file in a text editor and paste:@ economic recession
Phpbin= set \
\
In the second line, you must change \
Please confirm. Phpunit- if the version does not work, open a new command prompt. The console reads the path variable only at startup, so you must close and reopen the console when you change the path. The
Consider the following classes to create test cases:\/**
*Is a simple class with a digital counter. Only positive addition is allowed.
*\/The
Class counter
The{
\/**The
*Internal counter.
*The
*@Var integer
*\/The
Protect $\u C;
\/**The
*Create a new instance.
*The
*@Param integer$i[optional] initial value. Default: 0
*\/The
Public function \u construct ($i=0)
The{
$this->\u C = $i;
}The
\/**The
*Add a number to this counter.
*String and floating point are converted to integers.
*The
*@Param integer$x the number to add.
*\/The
Add public function ($x)
The{
$a = intval ($x);
If ($a male \u C += $a;
}The
\/**The
*Gets the counter value.
*The
*@Return integer internal counter value.
*\/The
Public function get()
The{
Return $this->\u C.
}The
}Class has an internal number, and you can add a positive number. Don’t you think there are bugs in such a simple class? You must be surprised. – look carefully and you’ll find one! The
Let’s create a test case. The countertest class expands phpuunit\u framework\u testcase.
The{
\/**The
*An instance for each test.
*The
*@Var counter
*\/The
Protected $counter;
\/**The
*This method will be called before the test runs.
*\/The
Protected function setup()
The{
$this->counter= new counter (0);
}The
\/**The
*This method will be called after the test run.
*\/The
Protected function teardown()
The{
}The
\/**The
*Test the get method.
*The
*@Covers counter:: get()
*\/The
Public function testget()
The{
Assertequals (0, $this->counter->get());
}The
\/**The
*Test the add method with a positive integer parameter.
*The
*@Covers counter:: add()
*\/The
Public function testadd()
The{
$this->counter->add (6);
$this->assertequals (6, $this->counter->get());
}The
\/**The
*Test the add method with a negative integer parameter.
*The
*@Covers counter:: add()
*\/The
Public function testadd negative()
The{
$this->counter->add (-3);
$this->assertequals (0, $this->counter->get());
}The
\/**The
*Test the add method with string parameters.
*The
*@Covers counter:: add()
*\/The
Public function testaddnumericstring()
The{
$this->counter->add (\
$this->assertequals (12, $this->counter->
Get());
}The
\/**The
*Test the add method with an invalid string parameter.
*The
*@Covers counter:: add()
*\/The
Public function testaddinvalidstring()
The{
$this->counter->add (\
$this->assertequals (0, $this->counter->get());
}The
\/**The
*Test the add method with the float parameter.
*The
*@Covers counter:: add()
*\/The
Public function testaddfloat()
The{
$this->counter->add (5.98);
$this->assertequals (5, $this->counter->get());
}The
}One thing to note: this test case is incomplete! First, we do not test creators who can pass negative integers. The counter::add () method can also add tests to determine how it works with array parameters, negative string parameters, and so on. The
However, although the above test is too simplified, it shows all the basic contents of unit test. First, notice how the test case extends the phpunit_framework_testcase class in phpunit. The first two methods are actually redefined in this class to set up the test instance and perform cleanup after completion. In the rest of the class’s methods, phphunit looks for methods that begin with the prefix \
Through these tests, even if the code changes, you can ensure that the class produces the same results as required. For example, suppose you make the following changes: Public function \u construct ($i=0)
The{
$this->\u C = 0;
$i-> add ($i);
}The
Add public function ($x)
The{
$this->\u C += max (0, intval ($x));
}Remember when I said that this class is too simple to generate bugs? Even if this is true, the test case can help you safely declare whether the new logic works the same as the previous logic. Unit tests are not implementation related. As long as the result is consistent with the expected value, the test will pass. Also, this is not the first time that min () and max () are confused. The
In the last section of \

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *