Testing obsolete codes with phpunit

I have an outdated code base and I need to check this code using PHPUnit. Therefore, I am asking for suggestions based on your experience. What classes should I check first? Or give priority?

Should I start with simple / small classes or with base / superclass?

+4
source share
3 answers

My general assumption about introducing unit testing into an existing codebase would be:

  • Start testing some very simple classes to get a feel for writing tests.
  • It’s possible to even rewrite these tests again and make the right example β€œthis is how we should do it” from them
  • Take one of the largest and middle classes in the whole system and get this class as much as possible. This is an important step to show everyone in your team (and possibly management) that the unit checking your code base MAY WORK and is doable.

After that, I suggest you focus on three things:

  • Make sure new code gets tests
  • If you correct the error, create a test before correcting it to β€œprove” the error is actually fixed.
  • Use tests as a tool when you touch / modify old code to improve the quality of testing.

PHPUnit will provide you with a CodeCoverage report showing how well your codebase is tested. It can be pretty cool to see that the number increases from 0.3% to 5% to 20% within a month, but it is not a very strong motivator.

To make sure you are testing the new code, I would suggest using PHP_Change_Coverage as described in this blog posting

This tool will help you generate a lot of meaningful coverage reports, as it only shows NEWLY CREATED CODE as UNTESTED , not all the old things that you have.

With this, you have something at hand that makes it very easy to β€œget high% very early and continue testing new material,” while you create tests for everything old.

Before the changes in PHP have changed: http://qafoo.com/blog/images/phpccov_without_timerange.png

and after: http://qafoo.com/blog/images/phpccov_with_timerange.png

+10
source

There is often too much code on a system to test all of this as a first step. But most of this code is already working.

I would start with methods that have been modified recently. Presumably most of the rest of the software works to some extent, and testing that is unlikely to find so many bugs that will be found in new or recently redesigned code.

If you have finished work (I doubt that in the near future, if you have about 1 or more developers who work near you), you can go to methods that use modified methods, to methods with a high degree of complexity in accordance with software metrics and with methods critical for the safe operation of the system (login with password, storage of customer payment data, etc.).

One way to help decide what to consider in the next test is to use a testing tool. This is usually used to determine how well the software is tested, but if you do not have a large number of tests that you already know, it will tell you: your code is not very well tested: - {so there is no point in running it earlier in the test process construction. (As you get more tests, you and your managers will eventually want to find out.) However, coverage testing tools also tend to provide complete lists of code that have been implemented or not as part of your tests, and this gives an idea that you should check node: code that did not execute.

Our SD PHP Test Coverage tool works with PHP and will provide this information both through an interactive and an interactive viewer and as a generated report. He will tell you which methods, classes, files and subsystems (according to the directory hierarchy) were tested and to what extent. If the file named "login.php" has not been tested, you can easily see it. And this explicit view makes it much easier to make a smart decision what to test further than just guessing based on what you can know about the code.

+5
source

Check out PHPure

Their software records the inputs and outputs from the php working website and then automatically records the phpunit tests for functions that do not have access to external sources (db, files, etc.).

This is not a 100% solution, but it is a great start.

0
source

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


All Articles