An important announcement from the founder of property-bee.com: The future of Property Bee is assured.

Documentation: Unit tests

An area to discuss the development and technical aspects of the toolbar

Documentation: Unit tests

Postby Beerhunter on Mon Oct 12, 2009 10:47 pm

I've just re-enabled this functionality (Bug #153) as rpp007 was asking about it.

This is a very early version that hasn't been kept up to date and it doesn't test everything!

However it was useful during the initial development / when I was learning javascript

To run the tests

To run the tests, there's under the new "Development" menu is "Run unit tests"!

Whilst its running there will be a few pop ups, just click ok or save!

When its complete it displays a webpage with a table of the results.

To write tests

It's quite straightforward, simply follow the pattern below;

Code: Select all
with(Components.classes["@property-bee.com/testharness;1"].getService().wrappedJSObject) {
   with(file("pb_utils_string.js")) {
      with(fn("String.trim")) {
         test("No trim", function() { return String("123").trim() == "123" } );
         test("Trim left", function() { return String(" 123").trim() == "123" } );
         test("Trim right", function() { return String("123 ").trim() == "123" } );
         test("Trim both", function() { return String(" 123 ").trim() == "123" } );
         test("Trim both and not middle", function() { return String(" 12 3 ").trim() == "12 3" } );
      }
      with(fn("String.tidy")) {
         test("No tidy", function() { return String("123").tidy() == "123" } );
         test("Tidy left", function() { return String(" 123").tidy() == "123" } );
         test("Tidy right", function() { return String("123 ").tidy() == "123" } );
         test("Tidy both", function() { return String(" 123 ").tidy() == "123" } );
         test("Tidy both and not middle", function() { return String(" 12 3 ").tidy() == "12 3" } );
         test("Tidy both and middle", function() { return String(" 12  3 ").tidy() == "12 3" } );
      }
      .....
   }
}


Code: Select all
with(file("pb_utils_string.js")) {

defines the source file being tested (appears in the 1st column of the table).


Code: Select all
with(fn("String.trim")) {

defines the function being tested (appears in the 2nd column of the table).

Code: Select all
test("No trim", ...

defines the test name (appears in the 3rd column of the table).

Code: Select all
function() { return String("123").trim() == "123" }

is the actual code to run.
If the function returns
* true, the result is a PASSED
* false or an exception, the result is a FAILED

I'm not actively maintaining the unit tests at the moment, but if you want to use the functionality to add some tests in please do.

There is a big limitation; it is synchronous, ie as the test function returns the next test is run. Which means its not useful for tests involving asynchronous operations (like timers or loading a webpages).

The future

Unit tests are a good idea, espsecially in regression testing, however the synchronous limitation of the current code is a killer!

I have a new and improved version of unit test framework which does get over this problem, and has a bit nicer (ie no withs!) syntax;

Code: Select all
registerTests(function() {
    this.namespace("global")
        .file("string.js")
         .fn("String.trim")
            .test("No trim", function() String("123").trim() == "123")
            .test("Trim left", function() String(" 123").trim() == "123")
            .test("Trim right", function() String("123 ").trim() == "123")
            .test("Trim both", function() String(" 123 ").trim() == "123")
            .test("Trim both and not middle", function() String(" 12 3 ").trim() == "12 3")
         .end_fn()
         .fn("String.tidy")
            .test("No tidy", function() String("123").tidy() == "123")
            .test("Tidy left", function() String(" 123").tidy() == "123")
            .test("Tidy right", function() String("123 ").tidy() == "123")
            .test("Tidy both", function() String(" 123 ").tidy() == "123")
            .test("Tidy both and not middle", function() String(" 12 3 ").tidy() == "12 3")
            .test("Tidy both and middle", function() String(" 12  3 ").tidy() == "12 3")
         .end_fn()
      .end_file()
    .end_namespace();

    this.namespace("PB")
        .file("document.js")
         .fn("PB.Document.getAttributeById")
            .asyncTest("Get existing attribute",
               function (finished) {

                  // Example of an async test
                  // - load an html document into the current tab
                  // - once the page is loaded;
                  //   - test PB.Document.getAttributeById
                  //   - return the result of the test back to the frame work via the call finishedTest()

                  PB.Browser.selectedTab().addEventListener("load",
                     function() {
                        var selectedTab = PB.Browser.selectedTab();
                        selectedTab.removeEventListener("load", arguments.callee, true);
                        var result = false;
                        try {
                           var document = new PB.Document(selectedTab.contentDocument);   
                           PB.assert("Unexpected attribute value.", function () document.getAttributeById('container', 'title') === 'a title');
                           result = true;
                        } catch (e) {
                           result = e;
                        }
                        finishedTest(result);
                     },
                     true
                  );
                  var testFile = new PB.Disk.FileSystem().testDirectory.file("pb_test_document.html");
                  PB.assert("Test file not found", function () testFile.exists());
                  PB.Browser.selectedTab().loadFile(testFile);
               }
            )
           .end_fn()
        .end_file()
    .end_namespace();
});


(the first part is the same tests as above, the second part is an example of an asynchronous test, which currently isn't possible in the 2.0 code)

As you can see my javascript has vastly improved since I wrote the original 2.0 toolbar :)
User avatar
Beerhunter
Site Admin
 
Posts: 1788
Joined: Tue Jan 22, 2008 12:05 am

Return to Development

Who is online

Users browsing this forum: No registered users and 7 guests

cron