Really. I did read that somewhere, long time ago. ![Smiley :)](https://www.elkarte.net/community/smileys/default/smiley.svg)
(The article is for Java and JUnit, but it works just the same.)
I submitted a PR to Elk, to add SimpleTest framework for unit testing Elk. SimpleTest allows to write tests in good ole' SUnit/JUnit style:
# extend UnitTestCase class; (one test case class per each of our classes, respectively file for procedural)
# add your own test methods with names prefixed with test() (descriptive name i.e. testTopicInfo(), or testTopicInfoForBigBoards(), whatever you feel could be a runtime situation which can fail);
# and add your class to a test suite. (a suite with "all tests we haz" will do)
# run the test suite.
With only this, we can get into unit testing Elk.
I've added a skeleton for the test suite: all_tests.php collects all tests files, and running it will do all the rest: load the classes, execute all the testSomething() methods it finds in each of them in order, and print out the results.
(start-up PR https://github.com/elkarte/Elkarte/pull/527)
Or, all_tests.php can be run in the browser, in which case it looks all nice and green. For now, 'cuz I cheated and added only very basic tests.
SimpleTest covers a lot of automated testing, not only pure function/method tests. The very nice thing with it is, it also gives the possibility to write web UI tests: to follow links, submit forms, and check the HTTP responses, in a very simple way. To use the web tester, testcases extend WebTestCase instead:
class TestMyPage extends WebTestCase
{
function testStuffButton()
{
$this->get('http://localhostish_elk/index.php');
$this->click('Stuff');
$this->assertTitle('Awesome stuff.');
}
}
With even only the links we have as a guest (without counting yet forms submissions), we can make simple tests for each, to be sure that when Ema we write some code, the responses are still what we expect, and there'll be no fatal error somewhere or not cool stuff like that.
You may want to check out SimpleTest's hands-on documentation: http://www.simpletest.org/en/start-testing.html
There's a long time I wanted to cover this software with unit testing. The huge advantage is, the more specific cases we have covered, the most likely it is to receive immediate feedback from the tests. For a commit or PR, we can try to write some test(s) for the changes made. And, of course, run all the suite to make sure it doesn't break something else.
One more note I'd make: we can also write tests before implementing a feature or refactoring. It can play the role of a specification of behavior. Those are the most fun.