A short while ago I wrote a set of three PHP unit tests for Math that use test doubles to stub out external dependencies (in this case, the database-backed cache and the texvc executable). My intent was to demonstrate the technique to another developer, so I commented the code extensively. It occurred to me that other people might be interested, too, so I'm sharing it here.
The advantage of such tests is that they typically faster and far less brittle than tests that rely on external resources. They also make test results less noisy: if the test fails, you know that it's because your code was wrong, and not because the database happened to suffer an outage. Finally, they are more portable, because they don't require that you configure external dependencies to make them work.
If you are interested, check out the example, and the relevant chapter in the PHPUnit docs.
https://gerrit.wikimedia.org/r/#/c/49612/1/tests/MathTexvcTest.php
http://www.phpunit.de/manual/current/en/test-doubles.html
-- Ori Livneh
On Tue, Mar 5, 2013 at 1:25 PM, Ori Livneh ori@wikimedia.org wrote:
A short while ago I wrote a set of three PHP unit tests for Math that use test doubles to stub out external dependencies (in this case, the database-backed cache and the texvc executable). My intent was to demonstrate the technique to another developer, so I commented the code extensively. It occurred to me that other people might be interested, too, so I'm sharing it here.
The advantage of such tests is that they typically faster and far less brittle than tests that rely on external resources. They also make test results less noisy: if the test fails, you know that it's because your code was wrong, and not because the database happened to suffer an outage. Finally, they are more portable, because they don't require that you configure external dependencies to make them work.
If you are interested, check out the example, and the relevant chapter in the PHPUnit docs.
https://gerrit.wikimedia.org/r/#/c/49612/1/tests/MathTexvcTest.php
This is awesome, thanks for doing this Ori! Glad to see progress in the direction of our unit tests truly being unit tests.[1]
Rob [1] Optional reading on unit-tests vs integration tests for those that don't know what I'm talking about: http://www.sandywalsh.com/2011/06/effective-units-tests-and-integration.html
+1,000,000
On Wed, Mar 6, 2013 at 5:42 PM, Rob Lanphier robla@wikimedia.org wrote:
On Tue, Mar 5, 2013 at 1:25 PM, Ori Livneh ori@wikimedia.org wrote:
If you are interested, check out the example, and the relevant chapter
in the PHPUnit docs.
https://gerrit.wikimedia.org/r/#/c/49612/1/tests/MathTexvcTest.php
This is awesome, thanks for doing this Ori! Glad to see progress in the direction of our unit tests truly being unit tests.[1]
Rob [1] Optional reading on unit-tests vs integration tests for those that don't know what I'm talking about:
http://www.sandywalsh.com/2011/06/effective-units-tests-and-integration.html
Also notable are rules for what is not a unit test: http://www.artima.com/weblogs/viewpost.jsp?thread=126923
wikitech-l@lists.wikimedia.org