| Defined | src/docs/developer/unit_tests.diviner:1 |
|---|---|
| Group | Phabricator Developer Guides |
Simple guide to libphutil, Arcanist and Phabricator unit tests.
libphutil, Arcanist and Phabricator provide and use a simple unit test framework. This document is aimed at project contributors and describes how to use it to add and run tests in these projects or other libphutil libraries.
In the general case, you can integrate arc with a custom unit test engine (like PHPUnit or any other unit testing library) to run tests in other projects. See Arcanist User Guide: Customizing Lint, Unit Tests and Workflows for information on customizing engines.
To add new tests to a libphutil, Arcanist or Phabricator module:
Once you've added test classes, you can run them with:
Here's a simple example test:
class PhabricatorTrivialTestCase extends PhabricatorTestCase { private $two; public function willRunOneTest($test_name) { // You can execute setup steps which will run before each test in this // method. $this->two = 2; } public function testAllIsRightWithTheWorld() { $this->assertEqual(4, $this->two + $this->two, '2 + 2 = 4'); } }
You can see this class at PhabricatorTrivialTestCase and run it with:
phabricator/ $ arc unit src/infrastructure/testing/testcase/ PASS <1ms* testAllIsRightWithTheWorld
For more information on writing tests, see ArcanistPhutilTestCase and PhabricatorTestCase.
By default, Phabricator isolates unit tests from the database. It makes a crude effort to simulate some side effects (principally, ID assignment on insert), but any queries which read data will fail to select any rows and throw an exception about isolation. In general, isolation is good, but this can make certain types of tests difficult to write. When you encounter issues, you can deal with them in a number of ways. From best to worst: