Understand unit testing

Tram Ho

Introduce.

  • The day before, I participated in a siminar session shared by Mr. Thang about unit testing in laravel projects, this article I perform the re-presentation of the shared content.

content

What is unit test?

  • Unit Testing is a software testing method where individual units of source code are tested to check if they are of a quality assurance to use.
  • Within the Laravel Project, writing Unit Test is the process where all Test Cases for each individual function / method are written.

What is code coverage?

  • Code coverage is a method of evaluation used to describe the extent to which a program’s source code has been executed, when a particular test suite has run. In other words, Code Coverage is a way to ensure that your Tests are actually testing your Codes!
  • For example, if your code coverage is 90%, that means that 90% of the codes in your project have been called to run Test.

Why write unit tests?

  • Improve code quality
  • Easy to detect bugs early (because every time you run a unit test, a dead unit test identifies a problem, either an updated unit test, or a logical error)
  • Easily adapt to change
  • Easy to debug when there is an error
  • Easy integration, making CI / CD really effective, reducing development effort, price, and possible risks.

Some misunderstandings about unit tests

  • Code Coverage 100% is a perfect test writing task. Code Coverage only shows which code has been run, when running the test. The logic of the code is actually true or not, Test has actually covered all the cases or not, will depend on the quality of the test, or the ability of the Test writer. Code Coverage is not a number that reflects the quality of the Test, but is the best measure we can have to assess the progress of writing Test.
  • Writing Tests according to Laravel’s ducument is writing Unit Tests
  • Writing Test in PHPUnit is writing Unit Test
  • Writing Test using RSpec is writing Unit Test
  • Writing Test in Laravel project is writing Unit Test

Facts:

  • Unit Test: Individual unit tests, independent of each other
  • Feature Test / Integration Test / Functional Test: each separate software unit is combined, and tested as a unified block
  • If Unit Test helps us make sure that each individual function works correctly, Integration / Feature / Functional Test helps us make sure that the other components of the project work perfectly when they are combined. In reality.

How to write unit tests correctly

  • Unit Tests need to be designed to test functions independently. In other words: A needs B to run. Even if B has an error, A’s Unit Test will STILL PASS if there is no problem with A
  • A good Unit Test does NOT do the following:
    • Trigger to run codes from other functions in the project
    • Query into database
    • Use file system
    • Network Access

The problem of writing unit tests

  • Time consuming learning, training time
  • It takes time / effort to write tests
  • The quality of the Test depends on the competence of the developer
Share the news now

Source : Viblo