📜 ⬆️ ⬇️

Easy way to start testing

If you are a PHP developer, and do not write tests for your applications for various reasons, then this article is for you. In it, I will try to briefly show where to start and what to do, so that writing tests brings you joy, and your application stability.

So the first tip. Forget everything you know about unit tests. Throw a stool at the person who told you that you can not do without them. Let's try to figure out in which cases it is necessary to use them, and in which cases it is inexpedient.

I am absolutely sure that PHP programmers rarely write tests, because they start at the wrong end. Everyone knows that tests are good and cool. But having opened the site of the same PHPUnit , and having read a colorful example about testing a calculator who can perform the operation a + b, they ask themselves: what relation does this calculator have to my application? The answer is no. Exactly like all similar examples, on sites of other testing frameworks. As if everyone had forgotten that PHP is primarily for the web. As if everyone is writing calculators, not sites based on the MVC paradigm.

Suppose you are creating a website or developing a web application. It already has some pages, forms, perhaps even interactive elements. How do you (or, say, your customer) check that the site is working? Surely you go to the site, click on the links, fill out forms, look at the result. And in a good way, all these routine processes of clicking and filling out forms should be automated first of all.
')
And therefore we will talk about functional tests (or acceptance tests).

Functional testing is the testing of software in order to verify the feasibility of functional requirements, that is, the ability of the software to solve the tasks that users need in certain conditions. Functional requirements determine what exactly the software does, what tasks it solves.

Wikipedia

Why writing tests should start with them? Yes, just to be sure that your site is functioning, and everything works there. Acceptance tests are equally well suited for a solid web application, as well as a simple website riveted overnight on the knee. Therefore, it is worth starting with them.

What do we have for functional testing? Of all I know, this is Codeception and, for example, PHPUnit + Mink or direct use of Selenium. I wanted to include Behat in this cookies, but its author asked not to use Behat for functional testing .

If testing with Selenium or PHPUnit + Mink was easy, you would probably already use them. Therefore we will stop on Codeception.

The test is described in it as follows:

<?php $I = new WebGuy($scenario); $I->wantTo('see my site is working'); $I->amOnPage('/'); $I->see('Welcome, on my site!'); ?> 


Just a few lines you check that at a minimum, the main page of your site opens. For a long work, break it is not so difficult.
In the same way, you can describe further actions:

 <?php $I->click('Feedback'); $I->see('Submit your feedback'); $I->fillField('Body','Your site is great!'); $I->click('Submit'); $I->see('Thanks for your feedback!'); ?> 


Here we are already checking that the feedback form on your site works. At least the user is shown that his opinion has been taken into account.

However, I already wrote about Habré about Codeception .

And now we will try to decide on unit-tests.

Unit testing, or unit testing (unit testing) is a programming process that allows you to check for the correctness of individual modules of the program's source code.

The idea is to write tests for each non-trivial function or method. This allows you to quickly verify whether the next code change did not lead to regression, that is, to the appearance of errors in the already tested program areas, and also facilitates the detection and elimination of such errors.

Wikipedia

The trouble with using them is that usually web applications are based on CMS or frameworks. And sometimes it is not always possible to select specific modules for testing. Especially inconveniently it turns out, when the application logic is scattered in various classes, and each specific unit in fact does nothing. The testing process will be further complicated by the fact that all modules are somehow interconnected, have many inherited methods, configurations, and where to select the code is yours, and where the library code is sometimes very difficult.

But if within the framework of functional tests you have checked everything that a user can do, then in unit tests try to take into account what he should not do.

For example, check if your code does not allow creating two users with the same name. Check that when you try to create a duplicate, the application will not fall, and the white screen of death will not show, but will intercept the error and show its text to the user. Agree, the user may mess things up, much more than you want. Therefore, it is sometimes impossible to foresee all possible scenarios and cover them with functional tests.

Or one more example: you have a special offer on your site - every 100th registered user receives a gift. How to verify that gifts are given out and not create 100 extra users? Here, write unit tests. Without them, most likely, in any way.

If in your application business logic is clearly expressed, for example: “this method creates a user, and this method allows changing the group status”, then it is quite reasonable to cover such methods with unit tests.

And another thing: if you create a website or application based on your framework, CMS, or some of your modules for these frameworks and CMS, be sure to write unit tests to them. There are no options. If you use third-party popular solutions, and do not invent bicycles, then most likely their code has already been tested by the author and you should not dwell on their testing.

For unit testing there are a lot of frameworks:

As an alternative to traditional unit tests:

Again, choose for yourself. PHPUnit is certainly a standard, but more modern frameworks can be simpler and more efficient.

Summing up, I would say this: tests are really needed and they are not as scary as you can imagine. Start writing tests with acceptance tests, continue with modular tests. Test what is important and test what you are not sure about .

Disclaimer : I apologize for a number of intentional simplifications and inaccuracies in the article. From my point of view, they are necessary to explain the basic principles of automatic testing of PHP applications. The more people who test their applications, the better the world will be. After all, the main thing is to start.

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


All Articles