Elentra Platform Technical Documentation
  • Introduction
  • Reporting Issues
    • Security Notices
  • Administrators
    • Server Requirements
    • Application Server
      • Shibboleth Single Sign-on
      • Installing Supervisor
      • Branded API Setup
      • Installing Mailhog
    • Database Server
  • Developers
    • Getting Started
    • Overview
    • Contributions
      • Request for Change
      • Jira and GitHub Details
      • Coding Standards
      • Quickstart Guide
      • Code Review
    • Database
    • Dataviews for Analytics
    • Global Namespace
    • Elentra ACL
    • Authentication Methods
    • Compatibility Matrix
    • Elentra ME Versions
    • Elentra Settings
    • Elentra Deployment
    • Elentra API
    • Elentra JS
    • Feature Configuration
      • Event Resource Integration
      • Microsoft Teams Integration
    • Troubleshooting & Guides
      • New developer features in Elentra ME 1.22
      • Testing With Codeception
        • Unit Testing
          • Unit Testing Best Practises
          • Writing Unit Testable Code
          • PHPUnit Testing for API
          • PHPUnit Json String Compare Function
        • Functional Testing
          • Functional Testing Best Practices
        • Integration Testing
          • Integration Testing Best Practices
      • Upgrading elentra-developer Docker
      • VS Code Setup
      • Using XDebug in VSCode
      • Upgrading PHP
      • Switching Databases
      • Creating a new virtual host
      • Logging In
      • Composer
      • Curriculum Tag Selector
      • Performance Issues
      • Docker
      • Seed Data Generation
      • Fail Fast Programming
      • Advanced Custom Exception Classes
    • Support
  • Upgrade Guides
    • Upgrading to ME v28.0
    • Upgrading to ME 1.27
    • Upgrading to ME 1.26
    • Upgrading to ME 1.25
    • Upgrading to ME 1.24
    • Upgrading to ME 1.23
    • Upgrading to ME 1.22
    • Upgrading to ME 1.21
    • Upgrading to ME 1.20
    • Upgrading to ME 1.19
    • Upgrading to ME 1.18
    • Upgrading to ME 1.17
    • Upgrading to ME 1.16
    • Upgrading to ME 1.15
Powered by GitBook
On this page
  • What Are Unit Tests?
  • Why Unit Test?
  1. Developers
  2. Troubleshooting & Guides
  3. Testing With Codeception

Unit Testing

PreviousTesting With CodeceptionNextUnit Testing Best Practises

Last updated 1 year ago

What Are Unit Tests?

A unit test is an automated script designed to verify whether a section of the codebase (a 'unit') behaves as intended. It is a method of 'proving' that the unit of code works using a repeatable, automated methodology, by verifying whether the result matches what is expected.

Unlike a UI test, unit tests do not use the user interface but interact directly with the underlying API.

A unit test must not:

  • interact with a database

  • communicate across a network

  • touch the file system

  • require changes to the environment (such as config files) to run

Codeception uses as the back end for its unit tests, adding some classes and utilities to make writing and using unit tests simpler.

Unit tests allow you to test individual functions, using a variety of input. They are much faster than integration or functional tests, and they help you to be confident that the logic of the function is correct. This is especially useful when changing code within a function, because you can quickly confirm that you have not inadvertently changed its behaviour.

You can run unit tests by changing to the /developers directory and running:

../www-root/core/library/vendor/bin/codecept run unit [filename]:[testfunctionname]

As the above command indicates, you can choose to run only a single test file, or even a single test function within that test file:

../www-root/core/library/vendor/bin/codecept run unit Entrada/HooksTest

or

../www-root/core/library/vendor/bin/codecept run unit Entrada/HooksTest:testClassMethodHook

Why Unit Test?

Less time performing functional tests

Testing the functionality of software is not only time consuming but costly. Constantly re-testing an application manually can lead to inconsistent results as steps (or tests) are missed despite a seemingly meticulous approach to testing. Switching branches or configuring an application to duplicate the conditions of the test can take time and are often error prone. Each time a change is made to the application a variety of tests must be executed to ensure the stability of the application – this consumes a lot of user resources.

Unit tests – while they take time to develop – soon save money as the tests become available to all members of the engineering department as they ensure consistent test procedures. Unit tests can be as quick as a few milliseconds and do not require knowledge of the application.

Less coupled code

Code that is tightly coupled relies on external resources (objects, classes, etc.) and is very difficult – if not impossible – to test properly. Writing unit tests for your code will allow you to see the value in decoupling code which also leads to a cleaner application.

Executable documentation

It can often be unclear to other developers what a method does or how it behaves when passed certain parameters. By providing an extensive suite of well-named unit tests, each test should be able to verbosely demonstrate the expected output for any provided input. Additionally, it is important for unit tests to prove that the method actually works correctly under both valid AND invalid input conditions.

Protection against regression

When a change is made to the application a new defect can be introduced to the system – this is known as a Regression Defect. When a change is made to the application it is important for testers not only test the new changes, but test the rest of the application to ensure that nothing has been broken by the new change introduced – everything must still behave as expected.

With unit testing, it's possible to rerun your entire suite of tests after every build or even after you change a single line of code.

PHPUnit