1916 1153 1758 1917 2000 1153 1552 1551 1948 1591 1262 1862 1249 1539 1571 1432 1457 1652 1423 1959 1383 1489 1850 1886 1366 1394 1048 1177 1893 1709 1015 1106 1849 1902 1109 1014 1989 1689 1344 1145 1271 1275 1919 1053 1440 1620 1740 1240 1401 1559 1436 1991 1923 1546 1037 1088 1050 1465 1133 1492 1928 1123 1710 1940 1938 1709 1913 1025 1371 1980 1893 1738 1943 1035 1081 1460 1189 1970 1524 1007 1058 1750 1067 1315 1349 1200 1290 1209 1768 1830 1161 1679 1823 1141 1930 1958 1760 1649 1009 Automated Testing 101 | PHPnews.io

PHPnews.io

Automated Testing 101

Written by Blackfire / Original link on Aug. 30, 2018

Setting up your first tests and automation can be achieved by a single person in less than 15 minutes.

Don’t feel ashamed

I’m not a developer. I’ve never written any test in my life, and the last bit of code I wrote myself was during my engineering studies. When I started working at cutting-edge web development company SensioLabs, on Blackfire, it came to me that every developer in the world was writing tests. At least that was what I saw around me every day. Then I started selling Blackfire to the “real world”.

You, reading those words, are not writing tests? Don’t feel ashamed. I can’t count anymore how many times I’ve had that embarrassing moment when I asked customers “OK, I guess you’re already writing unit tests and integration tests?”. And that the customer would hesitate for some seconds and say “Y… yes, we… just started!”.

Automated Testing for the Real World

As a 101 course, let’s first make sure we ask the right questions. What tools to use? How to setup a Jenkins/Travis CI? Which infrastructure do you need? How do you write a test? What is TDD, really? And above all, what are you trying to achieve?

That last question should definitely help you find a guideline. It should support you in gaining experience and confidence in what to do and how. Step by step. Don’t believe you’ll get to bug-free code every time you deploy, overnight.

Blackfire at the rescue

Blackfire’s underlying technology is a profiler. Even though it is first intended to help you find performance bottlenecks in your code and fix it, it does more that that.

As a profiler, it “reads” all of the code which is executed in a request, at runtime. All of the good code, and all of the bad code. And “bad” can mean in terms of performance, but also quality, and security. It tells you the bare truth: what is happening in your code at runtime.

So, how do you write tests?

With Blackfire, at first, you just don’t. We’ve got you covered with “recommendations“. Those are tests written by experts in various frameworks (Symfony, Drupal, Typo3, eZPlatform, Magento) and plain PHP. And they cover performance, quality and security. Each of those tests is fully documented, so that if ever one of them fails on your code, you’ll know:

That last point is key. Writing a test in Blackfire is pretty much like writing tests with any other tool. It is about writing a list of expectations for your code’s behavior, storing and changing that list together with your code. The good part is that with the Blackfire recommendations, the tests are already written, and that you can copy/paste them to your .blackfire.ymland adjust them to your liking. They’re indeed just “recommendations”. They may, or may not, be relevant to your code. So you can always discard them or make them more relevant!

This gives a first answer to “How do you write a test”. Hopefully it can help you bootstrap.

Now you definitely want to dig further, and understand why you might not want to write a test based on time, even if your objective is to improve your code’s performance.

How do you setup a test infrastructure?

With Blackfire, at first, you don’t. Blackfire is built to be used in production, test, staging, and development. First of all, all of the tests you’ll use from the previous paragraph can be run directly on you local machine.

Now if you’re looking at automated testing, there’s a simple solution. Even if we’d recommend testing before pushing to production, it obviously makes sense to at least test there. And you’re about 5 minutes away from there. Two things.

  1. Remember that .blackfire.yml file? Go ahead and list the most business critical URLs in your app, and use them to write scenarios. It probably includes your home page, some checkout or product page. Basically any bit of code that has a direct impact on your app’s mission.
  2. Create an “environment” in Blackfire (you’ll need a Premium or Enterprise subscription, try Premium for free) and simply configure periodic builds and email notifications. All in a few clicks.

Even if you didn’t write any custom tests, Blackfire will start profiling automatically all of the requests you listed. If ever it detects your code doesn’t comply with one of our recommendations, you’ll get an email to warn you about it.

Of course, you need to have a look at Jenkins/Travis CI and other solutions. But Blackfire is a very low hanging fruit to get started!

In a next article, we’ll figure out which next easy step you can make, so you become a pro at testing.

Happy testing!

 

2.5.0.0

googleblog ircmaxell krakjoe rdohms tutorials blackfire

« Episode 12: Whole lot of Xdebug - 8 years on »