The code should always work correctly. This is the main rule of the developer. When you work in a large team where each part of the overall project is delivered separately by different devs, it is important to ensure coherent teamwork. That is why we write automated tests. The goal of the automated testing is to ensure the quality of the product, as well as to protect your code from getting broken. In this article, Front-end developer Vitaliy tells us about the autotesting on one of his projects.
Vitaliy, Front-end developer at Web-Systems Solutions
Automated testing has many advantages, especially when it comes to implementing automated tests on the large projects. They work faster than a human. Automated testing can be performed at night and even on the weekends as it doesn’t require human control. Error notifications will be sent automatically anyway. Automated tests are accurate. The successful automated testing means you can trust the code. Moreover, they will not let your colleagues accidentally break your code.
In order to give you more specific details about the automated testing, I’ll talk about one of our web projects where we’re writing such tests. First, let’s talk about testing itself on the project I’ll call it project A due to the legal reasons here. And then I’ll explain how we integrated the automated testing in Continuous Integration (CI).”
How we run automated testing on the project
We’re using Behat framework to autotest the project. This library is designed for Behavior driving development or BDD. The essence of the ideology of BDD is that the scripts that are written in a human-readable language, not necessarily in English, called Gherkin are translated into tests which are based on the BDD framework, in this case, on Behat framework. The goal of the Gherkin syntax is to allow non-technical specialists to provide the clear requirements for the product.
Given-when-and-then script is used to describe each test case scenario. This is done for better understanding of how the system should work. We can ask the client to fill in the scenario, so we can go through the script and see how the product should work from his or her perspective. Let’s say we do the website authorization, and we need to write an automated test case of the form validation. The script for this test will be:
Feature: Sign up
Scenario: Check validation
Given I am on the sign-up page
When I fill in the E-mail field
And I do not fill in the Password field
Then I see an indicator of validation errorIn fact, testing helps us simulate user interaction with a future web product and localize errors if there are any. The Behat framework handles the scripts written in the human-readable language. It reads the text and looks for matching keywords in certain files. When it finds a test case, it runs the code through the automated testing. The programmer, in turn, uses this framework to write a program code that will perform these tests.
How the automated tests are integrated with Continuous Integration
What are the continuous integration tools and what do we need them for? There are many continuous integration tools. We use the one called Circleci on the project A. We use this program to track the source code. All the project changes are uploaded to the git repository. When changes get to the repo, the program builds them and runs through the tests automatically. If the error occurs, it notifies us about it. First of all, the program warns the developer who was the last to made changes to the code. He receives an email with the information on where the error occurred and what caused it, so he can fix the mistake immediately. Such changes will not get on the production server. If the automated testing is successful, it will send the changes to the server.
Autotesting is a good solution for large projects, because, first, it speeds up the process of testing and fixing errors in the code, and, secondly, it helps to automate some work of the developers and manage the actions of the team. We don’t cover with tests the entire project. A large project can have thousands of different test scenarios. It’s natural that you can not automate all of them. And there are some moments that it’s just better to check manually. Actually, manual and autotesting complement each other. And if you use both, you can be sure that you will get a high-quality product as a result.