[Pkg-bazaar-commits] ./bzr/unstable r721: - framework for running external commands from unittest suite

Martin Pool mbp at sourcefrog.net
Fri Apr 10 08:20:50 UTC 2009


------------------------------------------------------------
revno: 721
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Tue 2005-06-21 16:05:11 +1000
message:
  - framework for running external commands from unittest suite
  - show better messages when some tests fail
modified:
  bzrlib/blackbox.py
  bzrlib/selftest.py
-------------- next part --------------
=== modified file 'bzrlib/blackbox.py'
--- a/bzrlib/blackbox.py	2005-06-20 05:22:04 +0000
+++ b/bzrlib/blackbox.py	2005-06-21 06:05:11 +0000
@@ -24,14 +24,21 @@
 # this code was previously in testbzr
 
 from unittest import TestCase
-
-
-
-class TestVersion(TestCase):
+from bzrlib.selftest import TestBase
+
+class TestVersion(TestBase):
     def runTest(self):
-        from os import system
-        rc = system('bzr version')
-        if rc != 0:
-            fail("command returned status %d" % rc)
-            
-            
+        # output is intentionally passed through to stdout so that we
+        # can see the version being tested
+        self.runcmd(['bzr', 'version'])
+
+
+# class InTempBranch(TestBase):
+#     """Base class for tests run in a temporary branch."""
+#     def setUp():
+#     def tearDown()
+
+
+# class InitBranch(TestBase):
+#     def runTest(self):
+        

=== modified file 'bzrlib/selftest.py'
--- a/bzrlib/selftest.py	2005-06-20 05:22:04 +0000
+++ b/bzrlib/selftest.py	2005-06-21 06:05:11 +0000
@@ -17,7 +17,55 @@
 
 from unittest import TestResult, TestCase
 
+
+class TestBase(TestCase):
+    """Base class for bzr test cases.
+
+    Just defines some useful helper functions; doesn't actually test
+    anything.
+    """
+    # TODO: Special methods to invoke bzr
+    
+    def runcmd(self, cmd, expected=0):
+        self.log('$ ' + ' '.join(cmd))
+        from os import spawnvp, P_WAIT
+        rc = spawnvp(P_WAIT, cmd[0], cmd)
+        if rc != expected:
+            self.fail("command %r returned status %d" % (cmd, rc))
+
+
+    def backtick(self, cmd):
+        """Run a command and return its output"""
+        from os import popen
+        self.log('$ ' + ' '.join(cmd))
+        pipe = popen(cmd)
+        out = ''
+        while True:
+            buf = pipe.read()
+            if buf:
+                out += buf
+            else:
+                break
+        rc = pipe.close()
+        if rc:
+            self.fail("command %r returned status %d" % (cmd, rc))
+        else:
+            return out
+            
+
+    def log(self, msg):
+        """Log a message to a progress file"""
+        print >>TEST_LOG, msg
+               
+
+
+
 class _MyResult(TestResult):
+    """
+    Custom TestResult.
+
+    No special behaviour for now.
+    """
 #     def startTest(self, test):
 #         print str(test).ljust(50),
 #         TestResult.startTest(self, test)
@@ -33,28 +81,82 @@
 
 
 def selftest():
-     from unittest import TestLoader, TestSuite
-     import bzrlib
-     import bzrlib.whitebox
-     import bzrlib.blackbox
-     from doctest import DocTestSuite
-    
-     suite = TestSuite()
-     tl = TestLoader()
-
-     for m in bzrlib.whitebox, bzrlib.blackbox:
-         suite.addTest(tl.loadTestsFromModule(m))
-
-     for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \
-             bzrlib.commands:
-         suite.addTest(DocTestSuite(m))
-
-     result = _MyResult()
-     suite.run(result)
-
+    from unittest import TestLoader, TestSuite
+    import bzrlib
+    import bzrlib.whitebox
+    import bzrlib.blackbox
+    from doctest import DocTestSuite
+    import os
+    import shutil
+    import time
+
+    _setup_test_log()
+    _setup_test_dir()
+
+    suite = TestSuite()
+    tl = TestLoader()
+
+    for m in bzrlib.whitebox, bzrlib.blackbox:
+        suite.addTest(tl.loadTestsFromModule(m))
+
+    for m in bzrlib.store, bzrlib.inventory, bzrlib.branch, bzrlib.osutils, \
+            bzrlib.commands:
+        suite.addTest(DocTestSuite(m))
+
+    result = _MyResult()
+    suite.run(result)
+
+    _show_results(result)
+
+    return result.wasSuccessful()
+
+
+def _setup_test_log():
+    import time
+    import os
+    
+    global TEST_LOG
+    log_filename = os.path.abspath('testbzr.log')
+    TEST_LOG = open(log_filename, 'wt', buffering=1) # line buffered
+
+    print >>TEST_LOG, "bzr tests run at " + time.ctime()
+    print '%-30s %s' % ('test log', log_filename)
+
+
+def _setup_test_dir():
+    import os
+    import shutil
+    
+    global ORIG_DIR, TEST_DIR
+    ORIG_DIR = os.getcwdu()
+    TEST_DIR = os.path.abspath("testbzr.tmp")
+
+    print '%-30s %s' % ('running tests in', TEST_DIR)
+
+    if os.path.exists(TEST_DIR):
+        shutil.rmtree(TEST_DIR)
+    os.mkdir(TEST_DIR)
+    os.chdir(TEST_DIR)    
+
+    
+
+def _show_results(result):
+     for case, tb in result.errors:
+         _show_test_failure('ERROR', case, tb)
+
+     for case, tb in result.failures:
+         _show_test_failure('FAILURE', case, tb)
+         
+     print
      print '%4d tests run' % result.testsRun
      print '%4d errors' % len(result.errors)
      print '%4d failures' % len(result.failures)
 
-     return result.wasSuccessful()
-
+
+
+def _show_test_failure(kind, case, tb):
+     print (kind + '! ').ljust(60, '-')
+     print case
+     print tb
+     print ''.ljust(60, '-')
+    



More information about the Pkg-bazaar-commits mailing list