        }
  
        if ($numBefore === $this->numTests()) {
-@@ -106,6 +111,7 @@
++        unset($this->order[$className]);
+         throw new NoSuchElementException('No tests found in '.$class->getName());
+       }
+ 
+@@ -106,6 +113,7 @@
       */
      public function clearTests() {
        $this->tests= array();

     }
     
     /**
@@ -279,15 +285,49 @@
      *
      * @param   unittest.TestCase test
      * @return  unittest.TestResult

       return $result;
     }
     
@@ -300,8 +338,40 @@
       $this->notifyListeners('testRunStarted', array($this));
 
       $result= new TestResult();
-      for ($i= 0, $s= sizeof($this->tests); $i < $s; $i++) {
-        $this->runInternal($this->tests[$i], $result);
+      foreach ($this->order as $classname => $tests) {
+        $class= XPClass::forName($classname);
+
+        // Call beforeClass method if present. If it throws an exception,
+        // mark all tests in this class as skipped and continue with tests
+        // from other classes (if available)
+        foreach ($class->getMethods() as $m) {
+          if (!$m->hasAnnotation('beforeClass')) continue;
+          try {
+            $m->invoke(NULL, array());
+          } catch (TargetInvocationException $e) {
+            foreach ($tests as $i) {
+              $this->notifyListeners('testSkipped', array(
+                $result->setSkipped($this->tests[$i], $e->getCause(), 0.0)
+              ));
+            }
+            continue 2;
+          }
+          break;
+        }
+        
+        foreach ($tests as $i) {
+          $this->runInternal($this->tests[$i], $result);
+        }
+
+        // Call afterClass method of the last test's class. Ignore any
+        // exceptions thrown from this method.
+        foreach ($class->getMethods() as $m) {
+          if (!$m->hasAnnotation('afterClass')) continue;
+          try {
+            $m->invoke(NULL, array());
+          } catch (TargetInvocationException $ignored) { }
+          break;
+        }
       }
 
       $this->notifyListeners('testRunFinished', array($this, $result));
Index: ports/classes/net/xp_framework/unittest/tests/SimpleTestCase.class.php
===================================================================
--- ports/classes/net/xp_framework/unittest/tests/SimpleTestCase.class.php	(revision 11673)
