Unit Testing for Exceptions

Discuss all feature requests you have for a new Servoy versions here. Make sure to be clear about what you want, provide an example and indicate how important the feature is for you

Unit Testing for Exceptions

Postby jgarfield » Tue Jul 03, 2012 6:35 pm

In order for unit tests to really provide full coverage you need to make sure you are testing that your function throws exceptions when it's supposed to (lack of arguments, bad arguments, etc).

Currently we're rolled our own solution for this with the following function

Code: Select all
function assertException(fMethod, aArg, sMessage)
{
   var bCaught = false;
   try
   {
      fMethod.apply(null, aArg);
   }
   catch (e)
   {
      bCaught = true;
   }
   
   if (!bCaught)
   {
      jsunit.fail(sMessage);
   }
}


But would be nice if our jsunit natively provided this kind of functionality. In other unit testing packages I've seen things like assertFail or assertThrows.

Official Request: https://support.servoy.com/browse/SVY-2596
Programmer.
adBlocks
http://www.adblocks.com
jgarfield
 
Posts: 223
Joined: Wed Sep 28, 2005 9:02 pm
Location: Boston, US

Re: Unit Testing for Exceptions

Postby omar » Thu Jul 05, 2012 11:23 pm

Hi,

Did you see the help for the JSUnit node in the Solution Explorer? Unless you mean something else what you ask for is already there?

Code: Select all
//Fails a test. AssertionFailedError is always thrown.
jsunit.fail("Fail test");
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: Unit Testing for Exceptions

Postby jgarfield » Fri Jul 06, 2012 3:08 pm

Yes, I've checked that out...it's actually what I use to make my work-around method above :-)

jsunit.fail() is a way to force a unit test failure
what I'm looking for is a way to test for the successful throwing of an exception.
For instance:

Code: Select all
function add(a, b) {
   if (a == null | b == null) {
      throw "ArgumentException";
   }
   return a + b;
}

function test_add(){
   //Unit test should pass because "ArgumentException" was thrown
   jsunit.assertException(add(null, null));
}
Programmer.
adBlocks
http://www.adblocks.com
jgarfield
 
Posts: 223
Joined: Wed Sep 28, 2005 9:02 pm
Location: Boston, US

Re: Unit Testing for Exceptions

Postby jcompagner » Tue Jul 10, 2012 6:20 pm

things like that is normally done like:

Code: Select all
function test_add() {

   try {
     add(1,2);
     jsunit.fail();
   } catch (e) {
    // should come in here
   }
}
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8828
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet


Return to Discuss Feature Requests

Who is online

Users browsing this forum: No registered users and 9 guests