An example of how TestLess’ TestDetectors work


In this post, I’ll illustrate the workflow that you can use to extend TestLess so it can analyze any test suite. I will use a real example from a real customer to show you how easy it is to extend TestLess.

 

 

The Problem

Recently, I received the following question from a customer:

 

I’d like to run TestLess on javascript code, could you please advice [sic] on how to configure removeemptyspaces so that i may run it?
Tests are run using Karma/jasmine.
thanks for your help.

 

It is important to first reference the blog post I recently published that explains how TestLess’ TestDetectors work.

Having read that post, let’s work together to answer the customer’s question.

 

 

Step-by-Step Solution

Because the customer mentioned the use of Jasmine, I recommend doing the following steps.

First, in commentRemover.rb, add “jasmine” like this:

 when "java", "javascript", "junit4", "junit3", "jasmine"

 

Then in removeEmptySpaces.rb, add these lines to the case block:

when "jasmine"
   require File.join(File.dirname(__FILE__), 'TestDetector_JavaScript_Jasmine')
   testDetector = TestDetector_JavaScript_Jasmine.new( debug )

 

Note: the class TestDetector_JavaScript_Jasmine does not yet exist. We will create it in the steps below. Keep reading.

 

Because we’re using Jasmine, I assume that the tests have patterns like the ones below:

describe('example test', function() {
   it('should be true', function() {
      expect('foo').toBe('foo');
   });
});

 

I would therefore make a copy of TestDetector_RUBY_RSPEC.rb (included in the trial package) and name it TestDetector_JavaScript_Jasmine.rb. Why? This is because Rspec and Jasmine are both used for BDD and BDT, and they share similar syntax.

Make sure you update the name of the class of the new file TestDetector_JavaScript_Jasmine.rb like this:

class TestDetector_JavaScript_Jasmine < TestDetector

 

In the method post_initialize of the new TestDetector_JavaScript_Jasmine.rb file, modify the variable @endOfNestedContextPattern to contain “}.*\\);”, like this:

 @endOfNestedContextPattern = "}.*\\);"

 

Note: you don’t have to worry about specifying the whitespace before the closing nested context, because the superclass TestDetector handles that for you already.

 

Then, modify the return string in the get_test_style method to return “jasmine”.

After that, in the method build_pattern_for_end_of_test, modify the variable endTestPattern so that it is like this:

 endTestPattern = "^" + preTestWhitespace + "}.*\\);"

 

 

Voila!

The customer will now be able to run TestLess on their Jasmine test suites like this:

java -jar ortask_testless.jar -t <your jasmine test suite> -r outputfile -c <number of CPUs> -l jasmine

 

And, of course, they can also use the GUI!

As you can see, extending TestLess is a breeze!