@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1@title Writing Unit Tests
2@group developer
3
4Simple guide to Arcanist and Phorge unit tests.
5
6= Overview =
7
8Arcanist and Phorge provide and use a simple unit test framework. This
9document is aimed at project contributors and describes how to use it to add
10and run tests in these projects or other custom libraries.
11
12In the general case, you can integrate `arc` with a custom unit test engine
13(like PHPUnit or any other unit testing library) to run tests in other projects.
14See @{article:Arcanist User Guide: Customizing Lint, Unit Tests and Workflows}
15for information on customizing engines.
16
17= Adding Tests =
18
19To add new tests to a Arcanist or Phorge module:
20
21 - Create a `__tests__/` directory in the module if it doesn't exist yet.
22 - Add classes to the `__tests__/` directory which extend from
23 @{class:PhabricatorTestCase} (in Phorge) or
24 @{class@arcanist:PhutilTestCase} (elsewhere).
25 - Run `arc liberate` on the library root so your classes are loadable.
26
27= Running Tests =
28
29Once you've added test classes, you can run them with:
30
31 - `arc unit path/to/module/`, to explicitly run module tests.
32 - `arc unit`, to run tests for all modules affected by changes in the
33 working copy.
34 - `arc diff` will also run `arc unit` for you.
35
36= Example Test Case =
37
38Here's a simple example test:
39
40 lang=php
41 class PhorgeTrivialTestCase extends PhabricatorTestCase {
42
43 private $two;
44
45 public function willRunOneTest($test_name) {
46 // You can execute setup steps which will run before each test in this
47 // method.
48 $this->two = 2;
49 }
50
51 public function testAllIsRightWithTheWorld() {
52 $this->assertEqual(4, $this->two + $this->two, '2 + 2 = 4');
53 }
54
55 }
56
57You can see this class at @{class:PhabricatorTrivialTestCase} and run it with:
58
59 phorge/ $ arc unit src/infrastructure/testing/testcase/
60 PASS <1ms* testAllIsRightWithTheWorld
61
62For more information on writing tests, see
63@{class@arcanist:PhutilTestCase} and @{class:PhabricatorTestCase}.
64
65= Database Isolation =
66
67By default, Phorge isolates unit tests from the database. It makes a crude
68effort to simulate some side effects (principally, ID assignment on insert), but
69any queries which read data will fail to select any rows and throw an exception
70about isolation. In general, isolation is good, but this can make certain types
71of tests difficult to write. When you encounter issues, you can deal with them
72in a number of ways. From best to worst:
73
74 - Encounter no issues; your tests are fast and isolated.
75 - Add more simulated side effects if you encounter minor issues and simulation
76 is reasonable.
77 - Build a real database simulation layer (fairly complex).
78 - Disable isolation for a single test by using
79 `LiskDAO::endIsolateAllLiskEffectsToCurrentProcess();` before your test
80 and `LiskDAO::beginIsolateAllLiskEffectsToCurrentProcess();` after your
81 test. This will disable isolation for one test. NOT RECOMMENDED.
82 - Disable isolation for your entire test case by overriding
83 `getPhabricatorTestCaseConfiguration()` and providing
84 `self::PHABRICATOR_TESTCONFIG_ISOLATE_LISK => false` in the configuration
85 dictionary you return. This will disable isolation entirely. STRONGLY NOT
86 RECOMMENDED.