Download the PHP package williams/testtube without Composer
On this page you can find all versions of the php package williams/testtube. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download williams/testtube
More information about williams/testtube
Files in williams/testtube
Package testtube
Short Description TestTube: A lightweight and extensible unit testing framework for PHP.
License MIT
Informations about the package testtube
TestTube: Unit Testing Framework
TestTube is a lightweight and extensible unit testing framework for PHP. Originally built to support the Xpression library, its design prioritizes flexibility, making it suitable for small to medium-sized projects.
Requirements
- Composer must be installed.
Installation
-
Create a new TestTube project (you can replace 'example' with your desired directory name):
-
Configure
composer.json
to autoload the PHP libraries you want to test: - Regenerate autoload files:
Writing Your First Test
Let’s create a simple test for a Calculator
class.
-
Inside the
tests
directory, create a folder to group your test files (e.g.,demo
). Create a PHP file for your tests, such ascalculator.php
: - Define a
boot
method to instantiate the object(s) required for your tests:
If more than one object is required, return an array containing the objects:
-
Create a test method. Each test method should take as parameters the objects returned by the
boot
method. By default, test method names must be camel-cased and start with 'test': -
Define the body of the test, using an appropriate
assert
method: - Run your tests from the project's root directory
Example Output:
- Modify the expected value to simulate a failed test:
Re-running the tests will produce:
Once a test file contains more than one test, the output will show both the filename and method name to help pinpoint failures.
Advanced Features
Assertions
TestTube includes several assert
methods:
assertEquals($expected, $actual, $failureMessage = null)
assertTrue($boolean, $failureMessage = null)
assertFalse($boolean, $failureMessage = null)
The $failureMessage
parameter is optional, and a default message will be generated if omitted.
Custom Test Method Names
If you need non-standard test method names, you can declare them using the $useMethods
property. You can also disable automatic detection of test methods starting with "test" by setting $useTestMethods
to false
:
Templates
To follow the DRY (Don't Repeat Yourself) principle, TestTube allows you to extend from templates stored in the templates
directory. For example, to share a boot
method across multiple test files:
-
Create a reusable template:
- In your test file, extend this template:
The setup
Method
The setup
method helps to maintain a separation of concerns between object creation and configuration. It allows for any further customisation of the objects returned by the boot
method before each test. Like test methods, it must accept each object returned by boot
as a parameter: