Skip to main content
Topic: I've heard developers love writing tests! (Read 14848 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

I've heard developers love writing tests!

Really. I did read that somewhere, long time ago. :)
(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)
Quotenorv$repos/elkarte/tests$ php all_tests.php
all_tests.php
OK
Test cases run: 4/5, Passes: 12, Failures: 0, Exceptions: 0
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:
Code: [Select]
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. ;)
The best moment for testing your PR is right after you merge it. Can't miss with that one.

Re: I've heard developers love writing tests!

Reply #1

Yeah I saw this over at GitHub. Sounds awesome cool, but TBH I don't know how it works yet. Seems to be a common problem. :D

Is it useful for front end stuff, or is it mainly for back end?
Master of Expletives: Now with improved family f@&king friendliness! :D

Sources code: making easy front end changes difficult since 1873. :P