aphpunit/aphpunit

Unittest framework executing every test in its own process

2.1 2022-12-07 22:20 UTC

README

Why APHPUnit?

When writing unit tests there are no great choice in frameworks. The most commonly used one is PHPUnit. Although it is a very good framework with many features, it just does not fit my needs.

How does APHPUnit differ from PHPUnit

So how do these frameworks differ? Briefly:

  • APHPUnit does not change your code
  • APHPUnit executes every test function as a single process
  • APHPUnit is not OOP
  • APHPUnit has much less features than PHPUnit
  • APHPUnit concentrates on testing

What does this mean in a nutshell?

Since your code is not changed by APHPUnit before it is executed, there are no side-effects of any kind, e.g. regarding static variables. BUT there are side-effects that are good: a great test-suite will uncover race-conditions which are more likely to occur, since multiple tests are executed at the same time.

Executing every test function as a single process means that tests can run simultaneously. Number of concurrent processes are configurable and can be scaled up to (and beyond) the amount of existing CPU cores (or threads). It also eliminates all side-effects every book warns you about. Standard lecture teaches you NOT to use static variables in your code because it is hard to test. That is plain wrong. Testing frameworks are indeed incapable of testing static variables without causing problems but APHPUnit can. So feel free to use Singletons, static variables, sessions etc., because every test function runs as an independent process, not affected by other tests running at that point in time. (https://en.wikipedia.org/wiki/Side_effect_(computer_science))

APHPUnit is not OOP. If you define a test-case you just define a test-case and finished. There is no need to define a class (which is not really a class since there are no class-variables or any OOP features involved).

Less features mean, that if you need features present in another framework, you should use another framework. APHPUnit concentrates on testing. It does not care about code coverage, generation skeletons or managing your test-suites. It expects you to care about your tests and to be pedantic.

Writing tests means, you need to write your application in a way so that it can be tested. If you do you will love APHPUnit. If you compromise somewhere - you should use another framework.

How-to-use examples

<?php

namespace APHPUnit\Testcases;

function setUp() {
  // setUp() is OPTIONAL and called before executing every test-function
}

function tearDown() {
  // tearDown() is OPTIONAL and called after executing every test-function
}

// every function starting with "test" is executed.
// functions *not* starting with "test" are not executed.
function testFoo() {

  // this will be true
  assertTrue(1 == 1);

  // this assertion will fail and reported as error
  assertFalse(1 == 1);

}

Reference: all Assertions

This is a full list of assertions which can be used:

  • assertTrue($mixValue, $strOptionalDescription = null)
  • assertFalse($mixValue, $strOptionalDescription = null)
  • assertEquals($mixReal, $mixExpected, $strOptionalDescription = null)
  • assertNotEquals($mixReal, $mixExpected, $strOptionalDescription = null)
  • assertSame($mixReal, $mixExpected, $strOptionalDescription = null)
  • assertNotSame($mixReal, $mixExpected, $strOptionalDescription = null)
  • assertEmpty($mixValue, $strOptionalDescription = null)
  • assertNull($mixValue, $strOptionalDescription = null)
  • assertArrayHasKey($mixKey, $arrArray, $strOptionalDescription = null)
  • expectException($cloThrower, $strExpectedException, $strOptionalDescription = null)

CLI-Reference

php APHPUnit/aphpunit.php Mandatory-Directory-or-File-Containing-Testcases Optional-Userdefined-Prefix

example: php APHPUnit/aphpunit.php myproject/tests aphpunitexceptions