Archive for Testing

Nifty Rails Stuff

I’ve been kinda on a rails spree lately, I guess I’ve had some free time and also redoing a rails 1.1.6 project (and was noob then and no clue of what I was doing) and updating it to use REST.

I got Geoffrey Grosenbach PeepCode’s REST Tutorial, which is very awesome and well worth the 9 bucks. Its over an hour long! I also got the RJS Tutorial which is also awesome. I can’t wait till payday to get the $40 pack so I can get some more! There’s just something about SEEING something work that makes it stick sometimes.

Also I spent some time on Railscasts which features a ton of short demos on rails topics. Very cool. In particular i liked Testing Without Fixtures which is cool because sometimes fixtures are a PITA!

When watching one of the PeepCode screencasts I saw him using “autotest” .. a cool toy which I must have too! I searched for it and found this posting on his blog. Now I’m autotesting all the time :) It basically monitors your rails site and runs the test everytime you change your code.

I’ve been using e-TextEditor and slowly learning the shortcuts. I love the font that was on PeepCode’s screen cast and found a link on the PeepCode FAQ where I can download it. For Free! And it mentioned the name of the theme used in Textmate — which happened to be the one I had found in e-Text-Editor and was using. No wonder I liked it! I also used this font in my Putty settings and changed the awful default colors (esp that dark blue! hard to see!). e-TextEditor is in Beta but its in great shape and i was so happy to see a “close all tabs” feature put into the latest build. I’ll be ecstatic when they make a linux version.

Comments

Test More for Java?!

Any one know, does there exist a TestSimple/More (from Perl) for Java like the module for Perl?

whats that you say? I wrote about TestMore in Perl and PHP Here and here also!

I did a quick scan with google didn't give me much hope. So I wrote this quickly on the train this morning, since I can't hardly stand to program in any language without a TestMore like implementation.. (I know there's JUnit, and I'ved used it before, but its alot of overhead.. I just needed some quick tests!)

Class: (not making any claim this is the best or greatest way, and not 100% TAP protocol)

JAVA:
  1. package com.myawesomesite.util;
  2.  
  3. public class TestSimple {
  4.  
  5.  private int testCount;
  6.  
  7.  public TestSimple() {
  8.   this.testCount = 0;
  9.  }
  10.  
  11.  public void ok(boolean truth, String message) {
  12.   this.testCount++;
  13.   System.out.println( (truth ? "ok " : "not ok ")
  14.                       + this.testCount + " - "
  15.                       + message);
  16.  }

Usage:

JAVA:
  1. package com.myawesomesite.java;
  2.  
  3. import com.myawesomesite.java.*;
  4. import com.myawesomesite.util.TestSimple;
  5.  
  6. public class TestAnimal {
  7.  
  8.  public static void main(String[] args) {
  9.   TestSimple test = new TestSimple();
  10.  
  11.   Dog bob = new Dog();
  12.  
  13.   bob.setColor("green");
  14.   bob.setCollarSize(10);
  15.  
  16.   test.ok(bob.getColor() == "green", "bob's color is green");
  17.   test.ok(bob.getCollarSize() == 10,"bob's collar size is 10");
  18.   test.ok(bob.getCollarSize() == 0,"bob's collar size 0");
  19.  
  20.  
  21.  }
  22.  
  23. }

output is:

ok 1 - bob's color is green
ok 2 - bob's collar size is 10
not ok 3 - bob's collar size is 0

Comments

Response to Test Freak.

Update: Chris Shiflett posted an informative reponse on Test-More for PHP and testing in general

I started responding to the comment to my last post, then realized it was going to be long. So I put it in a post.

Thanks Damien Gilles, I have looked at that. Problem is, I have some resistance at work to using outside code. So I would need to work up a case for it beforehand and analyize it.

For really simple tests, this sort of testing (my friend Keith calls it Sanity testing) works fine. I don't really see the need to use a Unit Test framework. I have used PHPUnit before and it works fine, but ends up being just more typing (I think, but I'll try it and compare my results).

Here's an example of how I used it..

include_once("config.php");
include_once("User_Model.class.php");

$t = new Test_Simple(array("tests" => 20, "eol"=>" n"));

connect_db();
test_setup();

// Test object get/set methods
$user = new User_Model();
$user->setUserName("jdoe");
$user->setRealName("John Doe Test");
$user->setEmail("john@doe.com");
$user->setPermission("1");

// test true values
$t->ok( $user->getRealName()   == "John Doe Test", "get RealName" );
$t->ok( $user->getUserName()   == "jdoe", "get UserName" );
$t->ok( $user->getEmail()      == "john@doe.com", "get Email" );
$t->ok( $user->getPermission() == 1, "get Permission");

// test false values
$t->ok( $user->getRealName()   != "Susie Doe", "Not Realname Suzie Doe" );
$t->ok( $user->getUserName()   != "sdoe", "Not Username sdoe" );
$t->ok( $user->getEmail()      != "suz@doe.com", "Not email suz@doe.com" );
$t->ok( $user->getPermission() != 0, "Not Permission 0" );

// Save user
$saveResult = $user->save();
$t->ok( $saveResult == true, "Save was successful");

// Get ID of last user
$id = $user->id;

// clear object
$user = null;

// load user again and test again
$user = new User_Model($id);

// test true values
$t->ok( $user->getRealName()   == "John Doe Test", "get RealName after load" );
$t->ok( $user->getUserName()   == "jdoe", "get UserName after load" );
$t->ok( $user->getEmail()      == "john@doe.com", "get Email after load" );
$t->ok( $user->getPermission() == 1, "get Permission after load");

// change username
$user->setRealName("Mary Jane Test");
$user->setUserName("mjane");
$user->setEmail("mary@jane.com");
$user->setPermission("2");
$updateResult = $user->save();

// test true values
$t->ok( $updateResult == true, "Update was successful");
$t->ok( $user->id == $id, "ID stayed the same");
$t->ok( $user->getRealName()   == "Mary Jane Test", "get RealName after update" );
$t->ok( $user->getUserName()   == "mjane", "get UserName after update" );
$t->ok( $user->getEmail()      == "mary@jane.com", "get Email after update" );
$t->ok( $user->getPermission() == 2, "get Permission after update");

$deleteResult = $user->delete();
$t->ok( $deleteResult == true, "Delete was successful");

test_teardown();
$t->report();

// Clean up our mess before and after
function test_setup() {
connect_db();
$result = mysql_query("DELETE FROM users WHERE real_name like '%test%'");
}

function test_teardown() {
connect_db();
$result = mysql_query("DELETE FROM users WHERE real_name like '%test%'");
}

I did do a test_setup and test_teardown, similar to what you would have in a unit test. I only need to do this at the start of my "suite" and the end.

Of course, the main reason there's a test more (copied from Perl) for php is so you can use Perl to run the test suites. See this presentation by Geoffrey Young and Chris Shiflett on Power PHP Testing(tgz file) which, if you have a site with mixed PHP and Perl, would be a excellent way to test all your code with the same method.

Comments

Test Freak!

My two loyal readers know I like testing. Some say I'm sick.

I'm writing a PHP class for a user, and then think.. oh gee, how do I know if this works?? oh I'll write a Test::Simple for PHP. Yes, I know there exists one already that uses the power of Perl to test PHP files, but I didn't have time to figure out how to set that up and probably won't be able to use perl anyways on the production system. I can't stand to have global variables, so I made a class. Uber simple. There's probably better ways to do it (I can imagine something much more elegant in PHP 5). But, what do you expect on a whim and 40 minutes...

Code:

// Nola's Wanna Be PHP Test Framework

class Test_Simple {
var $tests_to_run;
var $success_count;
var $failure_count;
var $test_count;

function Test_Simple($params) {
if (isset($_SERVER['HTTP_HOST'])) {
// we're on a server use a

$params['eol'] = "\ n "; //take out spaces, had to have them for this blog
}
$this->tests_to_run = $params["tests"];
$this->eol = empty($params['eol']) ? "\n" : $params['eol'] ;
$this->success_count = 0;
$this->failure_count = 0;
$this->test_count = 0;
print "1..{$this->tests_to_run}{$this->eol}";
}

function ok ($expression, $message) {
$this->test_count++;
if ($expression == false) {
print "not ";
$this->failure_count++;
} else {
$this->success_count++;
}
print "ok {$this->test_count} - $message" . $this->eol;
}

function report() {
print $this->eol;
if ($this->test_count tests_to_run) {
print "You ran {$this->success_count} out of "
. "{$this->tests_to_run} tests " . $this->eol;
} else {
print "Opps, looks like you meant to run {$this->tests_to_run} "
. "tests but ran {$this->test_count} instead" .$this->eol;
}
print "Success Rate: "
. (($this->success_count *100)/ $this->test_count)
. "%".$this->eol;
}

}

And.. I made some code to test itself..

$t = new Test_Simple(array("tests" => "4"));

$t->ok($t->test_count == 1, "one test run");
$t->ok($t->tests_to_run == 4, "running 4 tests");
$t->ok($t->eol == "\n", 'end of line is \n');
$t->ok(true, "its true!");
$t->ok(!false, "its not false");

$t->report();

I will probably make improvements upon this... suggestions? .. my test "report" is a bit more verbose than the standard Test::* in Perl.

I made it so you can run it with the php executable ... or with the web browser.

I tried to install Perl's class Apache::TestRunPHP but had a few problems getting it setup on windows. I think I need mod_perl installed on my Apache. I did find out that it takes a different approach than my simple class, and starts apache, runs tests, prints report and shuts down apache. Interesting. My tests I only intended to run individually (actually didn't know how TestRunPHP worked till after I wrote my class).

Comments (2)

Article about Selenium

I posted an article about Selenium over at CodeSnipers.com .. check it out, its a very cool tool :)

Oh and fortune cookie for yesterday was:

He who enjoys doing and enjoys what he has done is happy.

I enjoy programming ... and looking back at some my of code I wrote -- I'm not happy. But I'm getting better everyday. :)

Comments

Got Stress?

TEST!

Reduce Stress, Write a Test

Good article. Right now at work, I've put off some coding because i know it will be a pain to test. I've considered how to write unit tests for this, but since the whole page is procedural, I can't break it up. If I re-write it as a class or at least functions, I might be able to write a unit test for it.

I recently wrote a unit test for a simple logging class. I realized it didn't work the same on different versions of PHP, so I decided to make a unit test so I could see where it failed and where to fix.

Test test test!

Comments