📜 ⬆️ ⬇️

PHPUnit + Netbeans

Hello.

It's no secret that every module of the system must be tested before using it in a real system.

This reduces the risk of new bugs, critical holes, and so on.
')
In large offices for this there are special people who are engaged in what they write unit-tests .

Smaller offices, freelancers sin with this, and on small projects it is not always necessary. Agree, if the class is 100 lines, then writing tests for 200 lines seems like a waste of time.

Nevertheless, I will not go into the details of who need someone.

I want to show how you can test your NetBeans code with a convenient GUI.


I will say right away that this is not a manual for all functions of PHPUnit, nevertheless, this article will give a brief overview.

So, starting with NetBeans, it seems from 6.7 they added the ability to test the code ... I don’t remember the exact version - look at the bins website.

Creating a test class is carried out in a couple of clicks.

But, all in order.

In order to test, we need:
  1. Actually NetBeans
  2. PHPUnit
  3. Desire to sort out

Installing PHPUnit


Installing PHPUnit goes through PEAR.

The manual is here.


I think there will be no problems with this.

Let's move on to creating a class that we will test.


And we will test the calculator. Yes, yes, a classic example.

So the class will look like:
 <?php class calc { /** *    . * * @var Double */ protected $last; /** *   . * * @param Double $a * @param Double $b * @assert (1,2) == 3 * @assert (10,10) == 20 * @assert (1,0) == 1 * @assert (0,0) == 0 */ public function sum($a, $b) { $this->last = $a+$b; return $this->getLast(); } /** *   $a   $b * * @param Double $a * @param Double $b * @return Double * @assert (6,3) == 2 * @assert (10,5) == 2 * @assert (15,3) == 5 * @assert (15,0) == 0 */ public function div($a,$b) { if ( $b==0) throw new Exception("Division by zero"); $this->last = $a/$b; return $this->getLast(); } /** *     * * @return Double */ public function getLast() { return $this->last; } /** *  * * @param double $a * @param double $b * @return double * @assert (1,2) == -1 * @assert (10,2) == 8 * @assert (15,2) == 13 * */ public function minus($a, $b) { $this->last = $a-$b; if($a<$b) $this->last=0; //       (!!) return $this->getLast(); } /** * * @param double $a * @param double $b * @return double * @assert (10,20) == 200 * @assert (1,20) == 20 * @assert (4,-3) == -12 * @assert (10,0) == 0 */ public function umnojenie($a,$b) { $this->last = $a*$b; if( $this->last == 0 ) $this->last=1; //  return $this->getLast(); } /** *    * * @param double $a * @param double $b * @assert (1,2) == 1 * @assert (3,2) == 9 * @assert (10,-1) == 0.1 * @assert (10,0) == 1 * @assert (0,0) == 0 */ public function power($a, $b) { $this->last = pow($a,$b); } } ?> 

In this class, we have described the operation.
Documented in phpdoc style.

In this class admitted specifically.
In particular, the pow method does not return a value. Forgot about return.

What is assert ?
assert allows you to pre-designate what we give to the input and what we want to get to the output.
That is, PHPUnit, when creating a test class, will create a test function for each such assert .
Conveniently - we save time.

We proceed to the creation of a test class.


Now you need to create a tech class.
NetBeans allows you to do this in 2 clicks (!)


From the first time you will be asked where to keep the class.
Select a folder in the project, for example, I have a test folder.
Then you will open a window with the created class.
By how many assert methods were used it turned out as much as in the picture:


All testing methods have the “test” prefix.
The setUp, tearDown methods are called before and after each test run!
That is, they can connect to something or something else to do, in general, everything that is connected for successful work.

Run the SHIFT + F6 test.
You should get something like the one in the picture.


It turns out each test is completed and 60% successful.

It is seen that all tests power FAILED, because there is no return.
We fix.
 public function power($a, $b) { $this->last = pow($a,$b); return $this->getLast(); } 


We get 80% of successful tests.


We correct the received errors.
testDiv4 - throws an Exception which is not caught. It is necessary to catch this in the test.
For this before the public function testDiv4 method
need to add
@expectedException annotation.

that is, we write like this:
 /** * Generated from @assert (15,0) == 0. * @expectedException Exception */ public function testDiv4() { $this->assertEquals( 0, $this->object->div(15,0) ); } 


Thus, we tell PHPUnit that we are expecting an Exception.

Next, the testMinus function, I remind you, we forcibly forbidden to return a response less than 0.
Fix it.

Do the same with the umnojenie method.
 /** *  * * @param double $a * @param double $b * @return double * @assert (1,2) == -1 * @assert (10,2) == 8 * @assert (15,2) == 13 * */ public function minus($a, $b) { $this->last = $a-$b; return $this->getLast(); } /** * * @param double $a * @param double $b * @return double * @assert (10,20) == 200 * @assert (1,20) == 20 * @assert (4,-3) == -12 * @assert (10,0) == 0 */ public function umnojenie($a,$b) { $this->last = $a*$b; return $this->getLast(); } 


that is, it should now pass the test.

We start again.
After launch you will have 95% PASSED.

The last 4 percent go to the test testPower5.
Why? Because when writing assert was wrong
and it turned out that pow (0,0) == 1 and we expect 0.
For this we change to 1 and run.

20 TESTS PASSED.
100% PASSED.

All perfectly!

Lastly, let's write an example:
5 * 5 + 5 = 30.

 public function testClass() { $this->assertEquals(30, $this->object->sum( $this->object->umnojenie(5, 5), 5)); } 

Run OK.

What other checks are there


In this example, the primitive assertEquals is used.
But there are still many different methods.

Here is a brief description in Russian: habrahabr.ru/blogs/php/56289

- The next time a review of a more complex example will be made, work with database files.

Article in my blog - anton.in.ua/2009/09/18/phpunit-netbeans

Thanks to all.

Source: https://habr.com/ru/post/70046/


All Articles