Simple Unit Tests

version 1/130803 by Dannii Willis

  • Home page
  • Beginning
  • Next



  • Documentation


    This extension adds support for simple unit tests. Group tests together in rules added to the Unit test rulebook, and make individual tests with the assert phrase:

        Unit test:
            assert that 5 is 5;
            assert that "happy" is "happy";

    Simple Unit Tests uses Text Capture by Eric Eve to capture any text that is said between assertions. You can use "the captured output" to have the captured text automatically stripped of whitespace at its beginning and end. If you need to capture a lot of text then you will need to increase the buffer size used by Text Capture; see that extension for details.

        Unit test:
            say "happy";
            assert that the captured output is "happy";

    Failed assertions will display an error message. If you are asserting that two values are the same then the expected and actual values will be displayed. If you give your test rules names, those names will be displayed. So running

        Unit test (this is the failing rule):
            assert that 2 plus 2 is 5;
            assert that the player encloses a room;

    produces
        Failure for test: failing, assertion: 1. Expected: 5, Got: 4
        Failure for test: failing, assertion: 2. (Asserted condition is false)
        2 of 2 assertions failed.

    If you are writing an extension, we suggest you name all your test rules, and that you place your tests under a section titled:
        (not for release) (for use with Simple Unit Tests by Dannii Willis)

    The state of the VM will be saved before and restored after the unit tests are run, so you don't need to worry about your tests changing anything permanently.

    Lastly, run the tests by entering the command "run the unit tests" (or just simply "unit"), or use "try running the unit tests" in your code. Happy testing!

    The latest version of this extension can be found at <https://github.com/i7/extensions>. This extension is released under the Creative Commons Attribution licence. Bug reports, feature requests or questions should be made at <https://github.com/i7/extensions/issues>.