Assert clarity

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Assert clarity

Post by nielsene »

I have a few test cases where I feel the assertions lack clarity.

I'm checking for the existence of certain keys in a dictionary. It is aleady guarenteed that if the key exists its value is non-false; and that the dictionairy will return false if the key doesn't exist.

I've currently coded these assertions as

Code: Select all

$this->assertTrue($this->pb->getQuery("Key Name"));
However, an "assertNotFalse" would probably indicate the intention better. Of course there is no such assertion and I don't think it would really be worth adding it via a local subclass of UnitTestCase. Perhaps

Code: Select all

assertNotEqual(false,$this->pb->getQuery("Key Name"));
It just feels a little funny to use notEquals with false (or true) as one of expected values....


I could write it as

Code: Select all

$this->assertTrue(in_array("Key Name",$this->pb->getKeys()))
but that would be adding the geyKeys() purely to support easier testing -- there isn't a use-case demand for getKeys yet.

I guess the other option would be to use assertEquals with the actual returned value. However the returned values are slightly brittled and will likely have a fair bit of tweaking at the whitespace level that would be hard to properly right a regexp for.


Any other thoughts?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

// assertNotFalse
$this->assertTrue($this->pb->getQuery("Key Name") !== false);
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Maybe:

Code: Select all

$this->assertTrue(is_string(...));
Last edited by McGruff on Fri Aug 05, 2005 9:19 pm, edited 2 times in total.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Actually maybe this expresses the constraint better:

Code: Select all

$this->assertTrue(is_string(...) and str_len(...) > 0);
I'm assuming that all you're really interested in here is that you should get a string back (an sql query, going by your recent posts). On the principle that you don't want to add any unecessary constraints to possible implementations, it doesn't really matter whether the method returns false, null, or an empty string '' if the key doesn't exist.
Last edited by McGruff on Fri Aug 05, 2005 9:19 pm, edited 2 times in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

McGruff wrote:Maybe:

Code: Select all

$this->assertTrue(is_string(...));
I like it. Thanks.


Ooh, new version even better!

Assert changed to:

Code: Select all

$phrase=$this->pb->getQuery($aPhraseLabel);
      $this->assertTrue(is_string($phrase) && strlen($phrase)>0);
I wish I had a good way to compare the actual string to the expected string, but as one of the primary reasons for moving the SQL out to a PhraseBook was to allow a DBA-type person to do tuning at both the DB and query level. The PhraseBook is marked up with sql comments, etc, so its directly edittable/useable in SQL authoring tools and I expect the
linebreaks/indentation of multiline queries to fluctuate freely along with join orders/aliases.

I think they way I'll test the embeded SQL is against a test DB. As long as the named query retreives the right Result Set, its working properly, etc.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

nielsene wrote:I think they way I'll test the embeded SQL is against a test DB.
Yes: I'd do the same. Running the query past a database is the real test.
Post Reply