Some tips when writing test code coverage for Laravel projects are effective

Tram Ho

I. Introduction

1. What is test code coverage?

  • In the field of testing, test code coverage: is the creation of test cases to meet certain code-code coverage conditions (for example, test designers can create test cases so that all Both program statements are executed at least once.
  • Code coverage is the coverage of code that has been run through the test cases we have written. This means that if we determine the unit’s goal is wrong, dividing the wrong cases, the bug will still exist. The bugs here are usually logical bugs, bugs between different units.
  • However, does code coverage only ensure branching statements have passed through? You may discover that your code writes extra logic in the code through branching statements that never run through. The meaning of code coverage is to ensure that the code lines have been passed by the test cases

2. On what components is test code coverage calculated?

  • Code coverage is the average coverage that code has run through:
    • lines (code line),
    • functions & methods (functions and methods),
    • classes & traits

3. Why write test code coverage for a project?

  • Make sure the code written is clean , written in a logical and correct manner.
  • Make sure the least bugs happen with what we code out.
  • Make sure we are coding the right specs and requirements that our customers make.
  • The product brought to the tester, QA is a complete product with few bugs to accelerate the project release process.

II. main content

Test writing should be written in parallel during our product code. This helps us to shape the standard base for code and how to write the code correctly. In laravel we need to write the code according to the following:

1. Console Tests:

Console tests are tests of the commands that we create to see if they will run. In fact, people rarely use console tests to calculate code coverage.

2. Feature Tests

In essence, laravel has identified the feature test as HTTP Tests . A simple example of feature tests .

There is a simple example as follows: I have 1 api create a new post to the system.

Feature tests above are testing api to check some of the following:

  • Check if the api has passed validate input or not.

  • Check if the api has run through the code.

3. Integration Tests:

In laravel it is defined as: Database Testing . Is to check that our code has finished running and whether the data has been saved in the database or not. Integration Tests are also sometimes written in Feature Tests

4. Unit Test:

Unit tests are the use of mock to call classes, functions to create mocking, and test code coverage.

After successfully mocking the classes, functions we check that the code has called all the functions, methods, and outputs.

III. Some notes when writing tests

  • Only test and cover meaningful code, in Laravel we do not need to write tests for the base parts of Laravel such as: Provider , Middleware , or some command ( Console ) …
  • Should proactively separate the code appropriately, such as: In the Controller should not handle logic, we should move the manipulation code to the database into Repositories, computational logic code, conditional processing into services, factory, … And then we will test api ( feature tests ), repositories manipulate the database ( Integration Tests ), the parts handling logic services, … ( Unit Tests ).
  • Should write test before writing code.
  • Avoid intentionally passing tests to earn code coverage.

IV. Conclusion

Surely through the article you also have a note about writing test code coverage for the project, right? We hope to hear from you

Share the news now

Source : Viblo